@aigur/client
Advanced tools
Comparing version 1.0.3 to 1.1.0
# @aigur/client | ||
## 1.1.0 | ||
### Minor Changes | ||
- a3d208c5: Support Pipeline Memory | ||
## 1.0.3 | ||
@@ -4,0 +10,0 @@ |
import { z } from 'zod'; | ||
declare class FlowBuilder<Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream, NodeDefinitions extends ConcreteNode<any, any>[], PrevNode extends ConcreteNode<any, any> | null> { | ||
declare class FlowBuilder<Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream, MemoryData extends Record<string, unknown>, NodeDefinitions extends ConcreteNode<any, any, any>[], PrevNode extends ConcreteNode<any, any, any> | null> { | ||
private nodes; | ||
constructor(nodes: NodeDefinitions); | ||
static create<Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream>(): FlowBuilder<Input, Output, [], null>; | ||
node<NodeDef extends NodeAction<any, any>>(nodeDefinition: NodeDef, getUserInput: (data: { | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any> ? Awaited<ReturnType<PrevNode['action']>> : Input; | ||
prev: PrevNode extends ConcreteNode<any, any, any> ? Awaited<ReturnType<PrevNode['action']>> : Input; | ||
input: Input; | ||
}) => Parameters<NodeDef>['0']): FlowBuilder<Input, Output, [...NodeDefinitions, ConcreteNode<Parameters<NodeDef>["0"], Awaited<ReturnType<NodeDef>>>], ConcreteNode<Parameters<NodeDef>["0"], Awaited<ReturnType<NodeDef>>>>; | ||
memory: MemoryData; | ||
}) => Parameters<NodeDef>['0'], getMemory?: (data: { | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any, any> ? Awaited<ReturnType<PrevNode['action']>> : Input; | ||
input: Input; | ||
output: Awaited<ReturnType<NodeDef>> extends ReadableStream ? string : Awaited<ReturnType<NodeDef>>; | ||
memory: MemoryData; | ||
}) => Partial<MemoryData>): FlowBuilder<Input, Output, MemoryData, [...NodeDefinitions, ConcreteNode<Parameters<NodeDef>["0"], Awaited<ReturnType<NodeDef>>, MemoryData>], ConcreteNode<Parameters<NodeDef>["0"], Awaited<ReturnType<NodeDef>>, MemoryData>>; | ||
private createDynamicPlaceholders; | ||
output(getUserInput: (data: { | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any> ? Awaited<ReturnType<PrevNode['action']>> : Input; | ||
prev: PrevNode extends ConcreteNode<any, any, any> ? Awaited<ReturnType<PrevNode['action']>> : Input; | ||
input: Input; | ||
}) => Output): FlowBuilder<Input, Output, [...NodeDefinitions, ConcreteNode<Output, Awaited<Output>>], ConcreteNode<Output, Awaited<Output>>>; | ||
memory: MemoryData; | ||
}) => Output, getMemory?: (data: { | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any, any> ? Awaited<ReturnType<PrevNode['action']>> : Input; | ||
input: Input; | ||
output: Output extends ReadableStream ? string : Output; | ||
memory: MemoryData; | ||
}) => Partial<MemoryData>): FlowBuilder<Input, Output, MemoryData, [...NodeDefinitions, ConcreteNode<Output, Awaited<Output>, MemoryData>], ConcreteNode<Output, Awaited<Output>, MemoryData>>; | ||
getNodes(): NodeDefinitions; | ||
@@ -25,6 +38,17 @@ } | ||
eventPublisher?: (pipelineInstanceId: string, event: PipelineEvent) => Promise<any>; | ||
memoryManager?: MemoryManager<any>; | ||
} | ||
interface PipelineConf<Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream> { | ||
type PipelineContext<Input, Output, MemoryData> = { | ||
pipelineInstanceId: string; | ||
input: Input; | ||
output: Output extends ReadableStream ? string : Output; | ||
values: Record<string, NodeContext<any, any>>; | ||
memory: MemoryData | null; | ||
userId: string; | ||
}; | ||
interface PipelineConf<Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream, MemoryData extends Record<string, unknown>> { | ||
id: string; | ||
flow: (builder: FlowBuilder<Input, Output, [], null>) => FlowBuilder<Input, Output, any, ConcreteNode<Output, Output>>; | ||
flow: (builder: FlowBuilder<Input, Output, MemoryData, [], null>) => FlowBuilder<Input, Output, MemoryData, any, ConcreteNode<Output, Output, MemoryData>>; | ||
updateMemory?: (pipelineContext: PipelineContext<Input, Output, MemoryData>) => MemoryData; | ||
memoryManager?: MemoryManager<MemoryData>; | ||
retries?: number; | ||
@@ -42,7 +66,12 @@ stream?: boolean; | ||
type NodeAction<Input extends Record<string, unknown> | ReadableStream, Output extends Record<string, unknown> | ReadableStream> = (input: Input, apiKeys: Record<string, string>) => Promise<Output>; | ||
type ConcreteNode<Input extends Record<string, unknown> | ReadableStream, Output extends Record<string, unknown> | ReadableStream> = { | ||
type ConcreteNode<Input extends Record<string, unknown> | ReadableStream, Output extends Record<string, unknown> | ReadableStream, MemoryData extends Record<string, unknown>> = { | ||
action: NodeAction<Input, Output>; | ||
input: Input; | ||
output: Output; | ||
memoryToSave: Partial<MemoryData> | null; | ||
}; | ||
type NodeContext<Input extends Record<string, unknown> | ReadableStream, Output extends Record<string, unknown> | ReadableStream> = { | ||
input: Input; | ||
output: Output; | ||
}; | ||
type PipelineEvent = { | ||
@@ -69,6 +98,10 @@ type: EventType; | ||
}; | ||
interface MemoryManager<T> { | ||
saveMemory: (id: string, value: any) => Promise<T | null>; | ||
loadMemory: (id: string) => Promise<T | null>; | ||
} | ||
declare class Pipeline<Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream> { | ||
readonly conf: PipelineConf<Input, Output>; | ||
readonly flow: FlowBuilder<Input, Output, any, any>; | ||
declare class Pipeline<Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream, MemoryData extends Record<string, unknown>> { | ||
readonly conf: PipelineConf<Input, Output, MemoryData>; | ||
readonly flow: FlowBuilder<Input, Output, MemoryData, any, any>; | ||
private readonly apiKeys; | ||
@@ -80,9 +113,23 @@ readonly onProgressListeners: Map<string, (event: PipelineProgressEvent) => void>; | ||
private eventIndex; | ||
constructor(conf: PipelineConf<Input, Output>, flow: FlowBuilder<Input, Output, any, any>, apiKeys: APIKeys); | ||
invoke(input: Input, pipelineInstanceId?: string): Promise<{}>; | ||
invokeRemote(endpoint: string, input: Input): Promise<Output>; | ||
invokeStream(endpoint: string, input: Input, cb: (chunk: string) => void): Promise<void>; | ||
constructor(conf: PipelineConf<Input, Output, MemoryData>, flow: FlowBuilder<Input, Output, MemoryData, any, any>, apiKeys: APIKeys); | ||
invoke(input: Input, opts?: { | ||
pipelineInstanceId?: string; | ||
userId?: string; | ||
}): Promise<Output extends ReadableStream<any> ? string : Output>; | ||
private saveMemory; | ||
private getMemoryId; | ||
private loadMemory; | ||
invokeRemote(endpoint: string, input: Input, opts?: { | ||
userId?: string; | ||
}): Promise<Output>; | ||
invokeStream(endpoint: string, input: Input, cb: (chunk: string) => void, opts?: { | ||
userId?: string; | ||
}): Promise<void>; | ||
vercel: { | ||
invoke: (input: Input) => Promise<Output>; | ||
invokeStream: (input: Input, cb: (chunk: string) => void) => Promise<void>; | ||
invoke: (input: Input, opts?: { | ||
userId?: string; | ||
}) => Promise<Output>; | ||
invokeStream: (input: Input, cb: (chunk: string) => void, opts?: { | ||
userId?: string; | ||
}) => Promise<void>; | ||
}; | ||
@@ -102,3 +149,3 @@ onProgress(cb: (args: PipelineProgressEvent) => void): () => void; | ||
pipeline: { | ||
create: <Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream<any>>(conf: PipelineConf<Input, Output>) => Pipeline<Input, Output>; | ||
create: <Input extends Record<string, unknown>, Output extends Record<string, unknown> | ReadableStream<any>, MemoryData extends Record<string, unknown> = {}>(conf: PipelineConf<Input, Output, MemoryData>) => Pipeline<Input, Output, MemoryData>; | ||
}; | ||
@@ -112,3 +159,3 @@ }; | ||
declare const inputSchema$a: z.ZodObject<{ | ||
declare const inputSchema$b: z.ZodObject<{ | ||
image: z.ZodString; | ||
@@ -120,3 +167,3 @@ }, "strip", z.ZodTypeAny, { | ||
}>; | ||
declare const outputSchema$a: z.ZodObject<{ | ||
declare const outputSchema$b: z.ZodObject<{ | ||
labels: z.ZodArray<z.ZodString, "many">; | ||
@@ -128,26 +175,28 @@ }, "strip", z.ZodTypeAny, { | ||
}>; | ||
declare function googleImageLabeling(input: z.input<typeof inputSchema$a>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$a>>; | ||
declare function googleImageLabeling(input: z.input<typeof inputSchema$b>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$b>>; | ||
declare const inputSchema$9: z.ZodObject<{ | ||
declare const inputSchema$a: z.ZodObject<{ | ||
prompt: z.ZodString; | ||
response_format: z.ZodDefault<z.ZodLiteral<"b64_json">>; | ||
size: z.ZodDefault<z.ZodEnum<["256x256", "512x512", "1024x1024"]>>; | ||
}, "strip", z.ZodTypeAny, { | ||
prompt: string; | ||
response_format: "b64_json"; | ||
size: "256x256" | "512x512" | "1024x1024"; | ||
}, { | ||
response_format?: "b64_json" | undefined; | ||
size?: "256x256" | "512x512" | "1024x1024" | undefined; | ||
prompt: string; | ||
}>; | ||
declare const outputSchema$9: z.ZodObject<{ | ||
url: z.ZodString; | ||
declare const outputSchema$a: z.ZodObject<{ | ||
result: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
url: string; | ||
result: string; | ||
}, { | ||
url: string; | ||
result: string; | ||
}>; | ||
declare function dalleUrlTextToImage(input: z.input<typeof inputSchema$9>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$9>>; | ||
declare function dalleBase64TextToImage(input: z.input<typeof inputSchema$a>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$a>>; | ||
declare const inputSchema$8: z.ZodObject<{ | ||
declare const inputSchema$9: z.ZodObject<{ | ||
prompt: z.ZodString; | ||
response_format: z.ZodDefault<z.ZodLiteral<"b64_json">>; | ||
size: z.ZodDefault<z.ZodEnum<["256x256", "512x512", "1024x1024"]>>; | ||
@@ -157,20 +206,18 @@ }, "strip", z.ZodTypeAny, { | ||
size: "256x256" | "512x512" | "1024x1024"; | ||
response_format: "b64_json"; | ||
}, { | ||
size?: "256x256" | "512x512" | "1024x1024" | undefined; | ||
response_format?: "b64_json" | undefined; | ||
prompt: string; | ||
}>; | ||
declare const outputSchema$8: z.ZodObject<{ | ||
result: z.ZodString; | ||
declare const outputSchema$9: z.ZodObject<{ | ||
url: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
result: string; | ||
url: string; | ||
}, { | ||
result: string; | ||
url: string; | ||
}>; | ||
declare function dalleBase64TextToImage(input: z.input<typeof inputSchema$8>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$8>>; | ||
declare function dalleUrlTextToImage(input: z.input<typeof inputSchema$9>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$9>>; | ||
declare const stabilityModel: z.ZodEnum<["stable-diffusion-v1-5", "stable-diffusion-512-v2-0", "stable-diffusion-768-v2-0", "stable-diffusion-512-v2-1", "stable-diffusion-768-v2-1"]>; | ||
declare const stabilityClipGuidancePreset: z.ZodEnum<["NONE", "FAST_BLUE", "FAST_GREEN", "SIMPLE", "SLOW", "SLOWER", "SLOWEST"]>; | ||
declare const inputSchema$7: z.ZodObject<{ | ||
declare const inputSchema$8: z.ZodObject<{ | ||
text_prompts: z.ZodEffects<z.ZodArray<z.ZodObject<{ | ||
@@ -227,3 +274,3 @@ text: z.ZodString; | ||
}>; | ||
declare const outputSchema$7: z.ZodObject<{ | ||
declare const outputSchema$8: z.ZodObject<{ | ||
result: z.ZodType<ArrayBuffer, z.ZodTypeDef, ArrayBuffer>; | ||
@@ -235,7 +282,7 @@ }, "strip", z.ZodTypeAny, { | ||
}>; | ||
declare function stabilityTextToImage(input: z.input<typeof inputSchema$7>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$7>>; | ||
declare function stabilityTextToImage(input: z.input<typeof inputSchema$8>, apiKeys: APIKeys): Promise<z.infer<typeof outputSchema$8>>; | ||
declare function output<PipelineOutput extends Record<string, unknown> | ReadableStream>(input: PipelineOutput): Promise<PipelineOutput>; | ||
declare const inputSchema$6: z.ZodObject<{ | ||
declare const inputSchema$7: z.ZodObject<{ | ||
text: z.ZodString; | ||
@@ -250,2 +297,21 @@ amount: z.ZodDefault<z.ZodOptional<z.ZodNumber>>; | ||
}>; | ||
declare const outputSchema$7: z.ZodObject<{ | ||
text: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
text: string; | ||
}, { | ||
text: string; | ||
}>; | ||
declare function enhanceWithKeywords(input: z.input<typeof inputSchema$7>): Promise<z.infer<typeof outputSchema$7>>; | ||
declare const inputSchema$6: z.ZodObject<{ | ||
strings: z.ZodRecord<z.ZodString, z.ZodString>; | ||
modifier: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
strings: Record<string, string>; | ||
modifier: string; | ||
}, { | ||
strings: Record<string, string>; | ||
modifier: string; | ||
}>; | ||
declare const outputSchema$6: z.ZodObject<{ | ||
@@ -258,3 +324,3 @@ text: z.ZodString; | ||
}>; | ||
declare function enhanceWithKeywords(input: z.input<typeof inputSchema$6>): Promise<z.infer<typeof outputSchema$6>>; | ||
declare function replaceMultipleStrings(input: z.input<typeof inputSchema$6>): Promise<z.infer<typeof outputSchema$6>>; | ||
@@ -445,2 +511,2 @@ declare const inputSchema$5: z.ZodObject<{ | ||
export { Aigur, Pipeline, createClient, dalleBase64TextToImage, dalleUrlTextToImage, enhanceWithKeywords, googleImageLabeling, googleTextToSpeech, gpt3Prediction, gpt3PredictionStream, inputSchema$4 as inputSchema, output, outputSchema$4 as outputSchema, replaceString, stabilityClipGuidancePreset, stabilityModel, stabilityTextToImage, stringToArrayBuffer, vercelGenericEdge, whisperApi }; | ||
export { Aigur, Pipeline, createClient, dalleBase64TextToImage, dalleUrlTextToImage, enhanceWithKeywords, googleImageLabeling, googleTextToSpeech, gpt3Prediction, gpt3PredictionStream, inputSchema$4 as inputSchema, output, outputSchema$4 as outputSchema, replaceMultipleStrings, replaceString, stabilityClipGuidancePreset, stabilityModel, stabilityTextToImage, stringToArrayBuffer, vercelGenericEdge, whisperApi }; |
@@ -1,7 +0,7 @@ | ||
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} | ||
var C=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var F=(n,e)=>{for(var t in e)C(n,t,{get:e[t],enumerable:!0})},J=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of B(e))!W.call(n,r)&&r!==t&&C(n,r,{get:()=>e[r],enumerable:!(o=$(e,r))||o.enumerable});return n};var V=n=>J(C({},"__esModule",{value:!0}),n);var Te={};F(Te,{Pipeline:()=>w,createClient:()=>Q,dalleBase64TextToImage:()=>oe,dalleUrlTextToImage:()=>ie,enhanceWithKeywords:()=>de,googleImageLabeling:()=>te,googleTextToSpeech:()=>we,gpt3Prediction:()=>ge,gpt3PredictionStream:()=>xe,inputSchema:()=>D,output:()=>O,outputSchema:()=>he,replaceMultipleStrings:()=>le,replaceString:()=>ye,stabilityClipGuidancePreset:()=>_,stabilityModel:()=>z,stabilityTextToImage:()=>se,stringToArrayBuffer:()=>ve,vercelGenericEdge:()=>Z,whisperApi:()=>Ae});module.exports=V(Te);function j(n){return new Promise(e=>setTimeout(e,n))}function A(n,e){if(typeof n=="string")return o(n);return t({...n});function t(i){let a={};for(let p in i)a[p]=o(i[p]);return a}function o(i){if(Array.isArray(i))return i.map(s=>t(s));if(typeof i=="object"&&i!==null)return t(i);let a=i,p=E(i);for(let s of p){let d=e[s.nodeId].output;if(d==null)continue;let c=d[s.property];if(c instanceof ArrayBuffer){a=c;continue}a=a?.replace(new RegExp(r(s.value)),c),a==="undefined"?a=void 0:a!==c&&a===c.toString()&&(a=c)}return a}function r(i){return i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}}function E(n){if(typeof n!="string")return[];let e=/\$context\.(\d+|input|memory)\.(\w+)\$/g,t=n.matchAll(e),o=[];for(let r of t)o.push({value:r[0],nodeId:r[1],property:r[2]});return o}function b(n=16){let e="",t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o=t.length;for(let r=0;r<n;r++)e+=t.charAt(Math.floor(Math.random()*o));return e}function U(n){let e={input:{input:n.input,output:n.input},memory:{input:n.memory,output:n.memory}};return{pipelineInstanceId:n.pipelineInstanceId,input:n.input,output:{},values:e,memory:n.memory,userId:n.userId}}async function L(n,e){let{memoryToSave:t}=e;if(!t)return;let o=A(t,n.values);for(let r in t)for(let i in o){let a=o[i];if(a instanceof ReadableStream){let p=E(t[r]),[s,d]=a.tee();n.values[p[0].nodeId].output=s;let c=await G(d);o[i]=c}}return o}function G(n){return new Promise(async e=>{let t=n.getReader(),o=new TextDecoder,r=!1,i="";for(;!r;){let{value:a,done:p}=await t.read();r=p;let s=o.decode(a);i+=s}e(i)})}var H=2,Y=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=b(16);this.eventIndex=0;this.vercel={invoke:(e,t)=>this.invokeRemote(`/api/pipelines/${this.conf.id}`,e,{userId:t?.userId}),invokeStream:(e,t,o)=>this.invokeStream(`/api/pipelines/${this.conf.id}`,e,t,{userId:o?.userId})};this.listenToEvents()}async invoke(e,t){let o=await this.loadMemory(t?.userId),r=U({input:e,pipelineInstanceId:t?.pipelineInstanceId??this.pipelineInstanceId,userId:t?.userId??"",memory:o});return await this.processPipeline(r),r.output}async saveMemory(e,t){let o=await t;!this.conf.memoryManager||!o||this.conf.memoryManager.saveMemory(this.getMemoryId(e.userId),o)}getMemoryId(e){return`${this.conf.id}${e?`-${e}`:""}`}loadMemory(e){return this.conf.memoryManager?this.conf.memoryManager.loadMemory(this.getMemoryId(e)):Promise.resolve(null)}invokeRemote(e,t,o){return fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t,pipelineInstanceId:this.pipelineInstanceId,userId:o?.userId??""})}).then(r=>r.json())}async invokeStream(e,t,o,r){let i=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t,pipelineInstanceId:this.pipelineInstanceId,userId:r?.userId??""})});if(!i.ok)throw new Error(i.statusText);let a=i.body;if(!a)return;let p=a.getReader(),s=new TextDecoder,d=!1;for(;!d;){let{value:c,done:y}=await p.read();d=y;let v=s.decode(c);o(v)}}onProgress(e){let t=b();return this.onProgressListeners.set(t,e),()=>{this.onProgressListeners.delete(t)}}onStart(e){let t=b();return this.onStartListeners.set(t,e),()=>{this.onStartListeners.delete(t)}}onFinish(e){let t=b();return this.onFinishListeners.set(t,e),()=>{this.onFinishListeners.delete(t)}}async processPipeline(e){let t=this.conf.retries??H;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 r=this.flow.getNodes();for(let a=0;a<r.length;a++){let p=this.notifyEvent({type:"node:start",context:e,data:{node:r[a].action.name,index:a}});this.conf.stream||await p;let s=0,d=!1;do{s++;try{let{action:y,input:v,memoryToSave:De}=r[a],R=e.values[a]={input:null,output:null};R.input=A(v,e.values),R.output=await y(R.input,this.apiKeys);let K=L(e,r[a]);this.saveMemory(e,K),d=!0}catch(y){if(s>t)throw y;await j((this.conf.retryDelayInMs??Y)*s)}}while(!d&&s<=t);let c=this.notifyEvent({type:"node:finish",context:e,data:{node:r[a].action.name,index:a}});this.conf.stream||await c}let i=this.notifyEvent({type:"pipeline:finish",context:e});return this.conf.stream||await i,e.output=e.values[r.length-1].output,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})}};async function O(n){return n}var T=class{constructor(e){this.nodes=e}node(e,t,o){let r=this.createDynamicPlaceholders("memory"),i=this.createDynamicPlaceholders("input"),a=this.createDynamicPlaceholders(this.nodes.length),p=this.nodes.length>0?this.nodes[this.nodes.length-1]:i,s={action:e,input:t({nodes:this.nodes,prev:p.output,input:i,memory:r}),output:this.createDynamicPlaceholders(this.nodes.length),memoryToSave:o?o({nodes:this.nodes,prev:p.output,output:a,input:i,memory:r}):null};return this.nodes.push(s),this}createDynamicPlaceholders(e){let t={},o={get:function(i,a){return`$context.${e}.${a}$`}};return new Proxy(t,o)}output(e,t){return this.node(O,e,t)}getNodes(){return this.nodes}};var q=2,X=350,Q=n=>{let{apiKeys:e}=n;return{apiKeys:e,pipeline:{create:t=>{let o={...t,memoryManager:n.memoryManager,retries:t.retries??q,retryDelayInMs:t.retryDelayInMs??X,eventListener:n.eventListener,eventPublisher:n.eventPublisher},r=t.flow(new T([]));return new w(o,r,e)}}}};async function Z(n,e){let{pipelineInstanceId:t,userId:o,input:r}=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"),p=n[a];return{output:await p.invoke(r,{pipelineInstanceId:t,userId:o}),pipeline:p}}var x=require("zod"),ee=x.z.object({image:x.z.string()}),qe=x.z.object({labels:x.z.array(x.z.string())});async function te(n,e){let t=ee.parse(n),o=`https://vision.googleapis.com/v1/images:annotate?key=${e.googleapis}`,r={requests:[{image:{content:t.image},features:[{type:"LABEL_DETECTION"}]}]};return{labels:(await(await fetch(o,{method:"POST",body:JSON.stringify(r)})).json()).responses[0].labelAnnotations.map(p=>p.description)}}var S=require("zod"),ne=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")}),Qe=S.z.object({result:S.z.string()});async function oe(n,e){let t=ne.parse(n),r=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:i}=await r.json();return{result:i[0].b64_json}}var I=require("zod"),re=I.z.object({prompt:I.z.string(),size:I.z.enum(["256x256","512x512","1024x1024"]).default("512x512")}),et=I.z.object({url:I.z.string().url()});async function ie(n,e){let t=re.parse(n),r=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:i}=await r.json();return{url:i[0].url}}var u=require("zod"),z=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"]),_=u.z.enum(["NONE","FAST_BLUE","FAST_GREEN","SIMPLE","SLOW","SLOWER","SLOWEST"]),ae=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:z.default("stable-diffusion-v1-5"),clip_guidance_preset:_.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")}),nt=u.z.object({result:u.z.instanceof(ArrayBuffer)});async function se(n,e){let t=ae.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 P=require("zod"),pe=P.z.object({text:P.z.string(),amount:P.z.number().optional().default(8)}),rt=P.z.object({text:P.z.string()}),ue={"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 ce(n){return Object.entries(ue).map(([e,t])=>`Title: ${e} | ||
Description: ${t.slice(0,n).join(", ")} | ||
`).join(` | ||
`)}async function ae(n){let e=oe.parse(n);return{text:`Write a maximum of ${e.amount} keywords in a csv list that describes the following | ||
${re(e.amount)} | ||
`)}async function de(n){let e=pe.parse(n);return{text:`Write a maximum of ${e.amount} keywords in a csv list that describes the following | ||
${ce(e.amount)} | ||
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()}),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}); | ||
Description:`}}var h=require("zod"),me=h.z.object({strings:h.z.record(h.z.string(),h.z.string()),modifier:h.z.string()}),at=h.z.object({text:h.z.string()});async function le(n){let e=me.parse(n);for(let t in e.strings)e.modifier=e.modifier.replace(new RegExp(`\\$\\(${t}\\)\\$`,"gm"),e.strings[t]);return{text:e.modifier}}var g=require("zod"),fe=g.z.object({text:g.z.string().or(g.z.array(g.z.string())),modifier:g.z.string()}),pt=g.z.object({text:g.z.string()});async function ye(n){let e=fe.parse(n);return{text:e.modifier.replace(/\$\(text\)\$/gm,Array.isArray(e.text)?e.text.join(", "):e.text)}}var m=require("zod"),D=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)}),he=m.z.object({text:m.z.string()});async function ge(n,e){let t=D.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 k=require("eventsource-parser"),N=require("zod");var Se=D.merge(N.z.object({stream:N.z.literal(!0).optional().default(!0)})),ft=N.z.object({stream:N.z.instanceof(globalThis.ReadableStream??Object)});async function xe(n,e){let t=Se.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 Ie(o)}}async function Ie(n){let e=new TextEncoder,t=new TextDecoder,o=0;return new ReadableStream({async start(i){function a(s){if(s.type==="event"){let d=s.data;if(d==="[DONE]"){i.close();return}try{let y=JSON.parse(d).choices[0].text;if(o<2&&(y.match(/\n/)||[]).length)return;let v=e.encode(y);i.enqueue(v),o++}catch(c){i.error(c)}}}let p=(0,k.createParser)(a);for await(let s of n.body)p.feed(t.decode(s))}})}var M=require("zod"),Pe=M.z.object({string:M.z.string()}),ht=M.z.object({arrayBuffer:M.z.instanceof(ArrayBuffer)});async function ve(n){let e=Pe.parse(n);return{arrayBuffer:Uint8Array.from(atob(e.string),o=>o.charCodeAt(0)).buffer}}var l=require("zod"),be=l.z.object({text:l.z.string(),speakingRate:l.z.number().min(.25).max(4).optional().default(1),pitch:l.z.number().min(-20).max(20).optional().default(0),encoding:l.z.enum(["MP3","FLAC","LINEAR16","MULAW","AMR","AMR_WB","OGG_OPUS","SPEEX_WITH_HEADER_BYTE","WEBM_OPUS"]).optional().default("MP3"),voice:l.z.object({language:l.z.string().optional().default("en-US"),name:l.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(l.z.string()).optional().default("en-US-Neural2-C")}).optional().default({language:"en-US",name:"en-US-Neural2-C"})}),St=l.z.object({audio:l.z.string()});async function we(n,e){let t=be.parse(n),o=`https://us-central1-texttospeech.googleapis.com/v1beta1/text:synthesize?key=${e.googleapis}`,r={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(r)})).json()).audioContent}}var f=require("zod"),Ne="https://transcribe.whisperapi.com",Me=f.z.object({audioUrl:f.z.string().url(),language:f.z.string().default("en"),autoDetectLanguage:f.z.boolean().default(!1),fileType:f.z.string().default("mp3"),task:f.z.enum(["transcribe","translate"]).default("transcribe")}),It=f.z.object({text:f.z.string()});async function Ae(n,e){let t=Me.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 r=await fetch(Ne,{method:"POST",headers:{contentType:"application/json",Authorization:"Bearer "+e.whisperapi},body:o});if(!r.ok)throw new Error(r.statusText);return{text:(await r.json()).text.trim()}}0&&(module.exports={Pipeline,createClient,dalleBase64TextToImage,dalleUrlTextToImage,enhanceWithKeywords,googleImageLabeling,googleTextToSpeech,gpt3Prediction,gpt3PredictionStream,inputSchema,output,outputSchema,replaceMultipleStrings,replaceString,stabilityClipGuidancePreset,stabilityModel,stabilityTextToImage,stringToArrayBuffer,vercelGenericEdge,whisperApi}); |
{ | ||
"name": "@aigur/client", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -0,4 +1,4 @@ | ||
import { AigurConfiguration, PipelineConf } from './types'; | ||
import { Pipeline } from './Pipeline'; | ||
import { FlowBuilder } from './builder'; | ||
import { Pipeline } from './Pipeline'; | ||
import { AigurConfiguration, PipelineConf } from './types'; | ||
@@ -17,8 +17,10 @@ const DEFAULT_RETRIES = 2; | ||
Input extends Record<string, unknown>, | ||
Output extends Record<string, unknown> | ReadableStream | ||
Output extends Record<string, unknown> | ReadableStream, | ||
MemoryData extends Record<string, unknown> = {} | ||
>( | ||
conf: PipelineConf<Input, Output> | ||
conf: PipelineConf<Input, Output, MemoryData> | ||
) => { | ||
const pipelineConf: PipelineConf<Input, Output> = { | ||
const pipelineConf: PipelineConf<Input, Output, MemoryData> = { | ||
...conf, | ||
memoryManager: opts.memoryManager, | ||
retries: conf.retries ?? DEFAULT_RETRIES, | ||
@@ -29,4 +31,4 @@ retryDelayInMs: conf.retryDelayInMs ?? RETRY_DELAY_IN_MS, | ||
}; | ||
const flow = conf.flow(new FlowBuilder<Input, Output, [], null>([])); | ||
return new Pipeline(pipelineConf, flow, apiKeys); | ||
const flow = conf.flow(new FlowBuilder<Input, Output, MemoryData, [], null>([])); | ||
return new Pipeline<Input, Output, MemoryData>(pipelineConf, flow, apiKeys); | ||
}, | ||
@@ -33,0 +35,0 @@ }, |
@@ -0,3 +1,3 @@ | ||
import { output } from './nodes/output/output'; | ||
import { ConcreteNode, NodeAction } from './types'; | ||
import { output } from './nodes/output/output'; | ||
@@ -7,14 +7,8 @@ export class FlowBuilder< | ||
Output extends Record<string, unknown> | ReadableStream, | ||
NodeDefinitions extends ConcreteNode<any, any>[], | ||
PrevNode extends ConcreteNode<any, any> | null | ||
MemoryData extends Record<string, unknown>, | ||
NodeDefinitions extends ConcreteNode<any, any, any>[], | ||
PrevNode extends ConcreteNode<any, any, any> | null | ||
> { | ||
constructor(private nodes: NodeDefinitions) {} | ||
static create< | ||
Input extends Record<string, unknown>, | ||
Output extends Record<string, unknown> | ReadableStream | ||
>() { | ||
return new FlowBuilder<Input, Output, [], null>([]); | ||
} | ||
public node<NodeDef extends NodeAction<any, any>>( | ||
@@ -24,10 +18,26 @@ nodeDefinition: NodeDef, | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any> | ||
prev: PrevNode extends ConcreteNode<any, any, any> | ||
? Awaited<ReturnType<PrevNode['action']>> | ||
: Input; | ||
input: Input; | ||
}) => Parameters<NodeDef>['0'] | ||
memory: MemoryData; | ||
}) => Parameters<NodeDef>['0'], | ||
getMemory?: (data: { | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any, any> | ||
? Awaited<ReturnType<PrevNode['action']>> | ||
: Input; | ||
input: Input; | ||
output: Awaited<ReturnType<NodeDef>> extends ReadableStream | ||
? string | ||
: Awaited<ReturnType<NodeDef>>; | ||
memory: MemoryData; | ||
}) => Partial<MemoryData> | ||
) { | ||
type NewNode = ConcreteNode<Parameters<NodeDef>['0'], Awaited<ReturnType<NodeDef>>>; | ||
type NewNode = ConcreteNode<Parameters<NodeDef>['0'], Awaited<ReturnType<NodeDef>>, MemoryData>; | ||
// configure to return a placeholder for any property accessed (e.g. $context.0.url$) | ||
const memory = this.createDynamicPlaceholders('memory'); | ||
const input = this.createDynamicPlaceholders('input'); | ||
// using this.nodes.length and not this.nodes.length - 1 because we want to setup a new node that we didnt add yet to the array | ||
const output = this.createDynamicPlaceholders(this.nodes.length); | ||
const prev = this.nodes.length > 0 ? this.nodes[this.nodes.length - 1] : input; | ||
@@ -40,16 +50,26 @@ const node = { | ||
input, | ||
memory, | ||
}), | ||
// configure output to return a placeholder for any property accessed (e.g. $context.0.url$) | ||
output: this.createDynamicPlaceholders(this.nodes.length), | ||
memoryToSave: getMemory | ||
? getMemory({ nodes: this.nodes, prev: prev.output, output, input, memory }) | ||
: null, | ||
} as NewNode; | ||
this.nodes.push(node); | ||
return this as unknown as FlowBuilder<Input, Output, [...NodeDefinitions, NewNode], NewNode>; | ||
return this as unknown as FlowBuilder< | ||
Input, | ||
Output, | ||
MemoryData, | ||
[...NodeDefinitions, NewNode], | ||
NewNode | ||
>; | ||
} | ||
private createDynamicPlaceholders(nodeIndex: number | 'input') { | ||
private createDynamicPlaceholders(source: number | 'input' | 'memory') { | ||
const output = {}; | ||
const safeNotInstanciatedWarningProxy = { | ||
get: function (object, prop) { | ||
return `$context.${nodeIndex}.${prop}$`; | ||
return `$context.${source}.${prop}$`; | ||
}, | ||
@@ -66,9 +86,19 @@ }; | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any> | ||
prev: PrevNode extends ConcreteNode<any, any, any> | ||
? Awaited<ReturnType<PrevNode['action']>> | ||
: Input; | ||
input: Input; | ||
}) => Output | ||
memory: MemoryData; | ||
}) => Output, | ||
getMemory?: (data: { | ||
nodes: NodeDefinitions; | ||
prev: PrevNode extends ConcreteNode<any, any, any> | ||
? Awaited<ReturnType<PrevNode['action']>> | ||
: Input; | ||
input: Input; | ||
output: Output extends ReadableStream ? string : Output; | ||
memory: MemoryData; | ||
}) => Partial<MemoryData> | ||
) { | ||
return this.node(output<Output>, getUserInput); | ||
return this.node(output<Output>, getUserInput, getMemory); | ||
} | ||
@@ -75,0 +105,0 @@ |
export * from './image/labeling/googleImageLabeling'; | ||
export * from './image/textToImage/dalleBase64'; | ||
export * from './image/textToImage/dalleUrl'; | ||
export * from './image/textToImage/dalleBase64'; | ||
export * from './image/textToImage/stability'; | ||
export * from './output/output'; | ||
export * from './text/modify/enhanceWithKeywords'; | ||
export * from './text/modify/replaceMultipleStrings'; | ||
export * from './text/modify/replaceString'; | ||
@@ -8,0 +9,0 @@ export * from './text/prediction/gpt3'; |
@@ -6,3 +6,3 @@ import { createParser, ParsedEvent, ReconnectInterval } from 'eventsource-parser'; | ||
import type { APIKeys } from '../../../types'; | ||
import type { APIKeys } from '#/types'; | ||
@@ -15,3 +15,5 @@ const inputSchema = gpt3BaseInputSchema.merge( | ||
const outputSchema = z.object({ stream: z.instanceof(globalThis.ReadableStream ?? Object) }); | ||
const outputSchema = z.object({ | ||
stream: z.instanceof(globalThis.ReadableStream ?? Object), | ||
}); | ||
@@ -18,0 +20,0 @@ export async function gpt3PredictionStream( |
import { FlowBuilder } from './builder'; | ||
import { delay } from './delay'; | ||
import { getConcreteNodeInput } from './getInputByContext'; | ||
import { placeholdersToConcreteValues } from './getConcreteNodeInput'; | ||
import { makeid } from './makeid'; | ||
import { createContext, PipelineContext } from './PipelineContext'; | ||
import { createContext } from './PipelineContext'; | ||
import { retrieveConcreteMemoryData } from './retrieveMemoryData'; | ||
import { | ||
APIKeys, | ||
ConcreteNode, | ||
EventType, | ||
PipelineConf, | ||
PipelineContext, | ||
PipelineProgressEvent, | ||
@@ -19,3 +22,4 @@ PipelineStatusEvent, | ||
Input extends Record<string, unknown>, | ||
Output extends Record<string, unknown> | ReadableStream | ||
Output extends Record<string, unknown> | ReadableStream, | ||
MemoryData extends Record<string, unknown> | ||
> { | ||
@@ -30,4 +34,4 @@ public readonly onProgressListeners: Map<string, (event: PipelineProgressEvent) => void> = | ||
constructor( | ||
public readonly conf: PipelineConf<Input, Output>, | ||
public readonly flow: FlowBuilder<Input, Output, any, any>, | ||
public readonly conf: PipelineConf<Input, Output, MemoryData>, | ||
public readonly flow: FlowBuilder<Input, Output, MemoryData, any, any>, | ||
private readonly apiKeys: APIKeys | ||
@@ -38,6 +42,15 @@ ) { | ||
public async invoke(input: Input, pipelineInstanceId: string = this.pipelineInstanceId) { | ||
const context = createContext({ | ||
public async invoke( | ||
input: Input, | ||
opts?: { | ||
pipelineInstanceId?: string; | ||
userId?: string; | ||
} | ||
) { | ||
const memory = await this.loadMemory(opts?.userId); | ||
const context = createContext<Input, Output, MemoryData>({ | ||
input, | ||
pipelineInstanceId, | ||
pipelineInstanceId: opts?.pipelineInstanceId ?? this.pipelineInstanceId, | ||
userId: opts?.userId ?? '', | ||
memory, | ||
}); | ||
@@ -48,3 +61,32 @@ await this.processPipeline(context); | ||
public invokeRemote(endpoint: string, input: Input): Promise<Output> { | ||
private async saveMemory( | ||
context: PipelineContext<Input, Output, MemoryData>, | ||
memoryToSavePromise: Promise<Partial<MemoryData> | null> | ||
) { | ||
const memoryToSave = await memoryToSavePromise; | ||
if (!this.conf.memoryManager || !memoryToSave) { | ||
return; | ||
} | ||
this.conf.memoryManager.saveMemory(this.getMemoryId(context.userId), memoryToSave); | ||
} | ||
private getMemoryId(userId?: string) { | ||
return `${this.conf.id}${userId ? `-${userId}` : ''}`; | ||
} | ||
private loadMemory(userId?: string): Promise<MemoryData | null> { | ||
if (!this.conf.memoryManager) { | ||
return Promise.resolve(null); | ||
} | ||
return this.conf.memoryManager.loadMemory(this.getMemoryId(userId)); | ||
} | ||
public invokeRemote( | ||
endpoint: string, | ||
input: Input, | ||
opts?: { | ||
userId?: string; | ||
} | ||
): Promise<Output> { | ||
return ( | ||
@@ -59,2 +101,3 @@ fetch(endpoint, { | ||
pipelineInstanceId: this.pipelineInstanceId, | ||
userId: opts?.userId ?? '', | ||
}), | ||
@@ -73,3 +116,6 @@ }) | ||
input: Input, | ||
cb: (chunk: string) => void | ||
cb: (chunk: string) => void, | ||
opts?: { | ||
userId?: string; | ||
} | ||
): Promise<void> { | ||
@@ -84,2 +130,3 @@ const response = await fetch(endpoint, { | ||
pipelineInstanceId: this.pipelineInstanceId, | ||
userId: opts?.userId ?? '', | ||
}), | ||
@@ -107,9 +154,22 @@ }); | ||
public vercel = { | ||
invoke: (input: Input): Promise<Output> => { | ||
invoke: ( | ||
input: Input, | ||
opts?: { | ||
userId?: string; | ||
} | ||
): Promise<Output> => { | ||
// TODO: move base url to "create" optional param | ||
return this.invokeRemote(`/api/pipelines/${this.conf.id}`, input); | ||
return this.invokeRemote(`/api/pipelines/${this.conf.id}`, input, { userId: opts?.userId }); | ||
}, | ||
invokeStream: (input: Input, cb: (chunk: string) => void) => { | ||
invokeStream: ( | ||
input: Input, | ||
cb: (chunk: string) => void, | ||
opts?: { | ||
userId?: string; | ||
} | ||
) => { | ||
// TODO: move base url to "create" optional param | ||
return this.invokeStream(`/api/pipelines/${this.conf.id}`, input, cb); | ||
return this.invokeStream(`/api/pipelines/${this.conf.id}`, input, cb, { | ||
userId: opts?.userId, | ||
}); | ||
}, | ||
@@ -142,3 +202,5 @@ }; | ||
private async processPipeline(context: PipelineContext<Input>): Promise<PipelineContext<Input>> { | ||
private async processPipeline( | ||
context: PipelineContext<Input, Output, MemoryData> | ||
): Promise<PipelineContext<Input, Output, MemoryData>> { | ||
const retriesCount = this.conf.retries ?? DEFAULT_RETRIES; | ||
@@ -157,3 +219,3 @@ try { | ||
} | ||
const nodes: any[] = this.flow.getNodes(); | ||
const nodes: ConcreteNode<any, any, any>[] = this.flow.getNodes(); | ||
@@ -165,3 +227,3 @@ for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { | ||
data: { | ||
node: nodes[nodeIndex].name, | ||
node: nodes[nodeIndex].action.name, | ||
index: nodeIndex, | ||
@@ -178,5 +240,8 @@ }, | ||
try { | ||
const { action, input: inputPlaceholders } = nodes[nodeIndex]; | ||
const nodeInput = getConcreteNodeInput(inputPlaceholders, context.values); | ||
context.values[nodeIndex] = await action(nodeInput, this.apiKeys); | ||
const { action, input: inputPlaceholders, memoryToSave } = nodes[nodeIndex]; | ||
const contextNode = (context.values[nodeIndex] = { input: null, output: null }); | ||
contextNode.input = placeholdersToConcreteValues(inputPlaceholders, context.values); | ||
contextNode.output = await action(contextNode.input, this.apiKeys); | ||
const memoryValuesPromise = retrieveConcreteMemoryData(context, nodes[nodeIndex]); | ||
this.saveMemory(context, memoryValuesPromise); | ||
isSuccess = true; | ||
@@ -194,3 +259,3 @@ } catch (e) { | ||
data: { | ||
node: nodes[nodeIndex].name, | ||
node: nodes[nodeIndex].action.name, | ||
index: nodeIndex, | ||
@@ -211,3 +276,3 @@ }, | ||
context.output = context.values[nodes.length - 1]; | ||
context.output = context.values[nodes.length - 1].output; | ||
return context; | ||
@@ -248,3 +313,3 @@ } catch (e) { | ||
type: EventType; | ||
context: PipelineContext<Input>; | ||
context: PipelineContext<Input, Output, MemoryData>; | ||
data?: Record<any, any>; | ||
@@ -251,0 +316,0 @@ }) { |
@@ -1,11 +0,22 @@ | ||
export type PipelineContext<Input> = ReturnType<typeof createContext<Input>>; | ||
import { NodeContext, PipelineContext } from './types'; | ||
export function createContext<Input>(opts: { pipelineInstanceId: string; input: Input }) { | ||
const values = { input: opts.input }; | ||
export function createContext<Input, Output, MemoryData>(opts: { | ||
pipelineInstanceId: string; | ||
input: Input; | ||
userId: string; | ||
memory: MemoryData | null; | ||
}): PipelineContext<Input, Output, MemoryData> { | ||
// setup pipeline input and memory inside values | ||
const values = { | ||
input: { input: opts.input, output: opts.input }, | ||
memory: { input: opts.memory, output: opts.memory }, | ||
} as Record<string, NodeContext<any, any>>; | ||
return { | ||
pipelineInstanceId: opts.pipelineInstanceId, | ||
input: opts.input, | ||
output: {}, | ||
output: {} as Output, | ||
values, | ||
memory: opts.memory, | ||
userId: opts.userId, | ||
}; | ||
} |
@@ -9,12 +9,25 @@ import { z } from 'zod'; | ||
eventPublisher?: (pipelineInstanceId: string, event: PipelineEvent) => Promise<any>; | ||
memoryManager?: MemoryManager<any>; | ||
} | ||
export type PipelineContext<Input, Output, MemoryData> = { | ||
pipelineInstanceId: string; | ||
input: Input; | ||
output: Output extends ReadableStream ? string : Output; | ||
values: Record<string, NodeContext<any, any>>; | ||
memory: MemoryData | null; | ||
userId: string; | ||
}; | ||
export interface PipelineConf< | ||
Input extends Record<string, unknown>, | ||
Output extends Record<string, unknown> | ReadableStream | ||
Output extends Record<string, unknown> | ReadableStream, | ||
MemoryData extends Record<string, unknown> | ||
> { | ||
id: string; | ||
flow: ( | ||
builder: FlowBuilder<Input, Output, [], null> | ||
) => FlowBuilder<Input, Output, any, ConcreteNode<Output, Output>>; | ||
builder: FlowBuilder<Input, Output, MemoryData, [], null> | ||
) => FlowBuilder<Input, Output, MemoryData, any, ConcreteNode<Output, Output, MemoryData>>; | ||
updateMemory?: (pipelineContext: PipelineContext<Input, Output, MemoryData>) => MemoryData; | ||
memoryManager?: MemoryManager<MemoryData>; | ||
retries?: number; | ||
@@ -36,3 +49,4 @@ stream?: boolean; | ||
Input extends Record<string, unknown> | ReadableStream, | ||
Output extends Record<string, unknown> | ReadableStream | ||
Output extends Record<string, unknown> | ReadableStream, | ||
MemoryData extends Record<string, unknown> | ||
> = { | ||
@@ -42,2 +56,3 @@ action: NodeAction<Input, Output>; | ||
output: Output; | ||
memoryToSave: Partial<MemoryData> | null; | ||
}; | ||
@@ -81,1 +96,6 @@ export type NodeContext< | ||
}; | ||
export interface MemoryManager<T> { | ||
saveMemory: (id: string, value: any) => Promise<T | null>; | ||
loadMemory: (id: string) => Promise<T | null>; | ||
} |
export async function vercelGenericEdge(pipelines, req) { | ||
const { pipelineInstanceId, input } = await req.json(); | ||
const { pipelineInstanceId, userId, input } = await req.json(); | ||
const { searchParams } = new URL(req.url); | ||
@@ -9,4 +9,5 @@ if (!searchParams.has('id')) { | ||
const pipeline = (pipelines as any)[id]; | ||
const output = await pipeline.invoke(input, pipelineInstanceId); | ||
const output = await pipeline.invoke(input, { pipelineInstanceId, userId }); | ||
return { output, pipeline }; | ||
} |
Sorry, the diff of this file is not supported yet
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
80652
36
1864