blink 0.1.34 → 0.1.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/index.cjs +1 -1
- package/dist/api/index.d.cts +8 -1
- package/dist/api/index.d.ts +8 -1
- package/dist/api/index.js +1 -1
- package/dist/cli/index.js +76 -76
- package/dist/test.cjs +1 -1
- package/dist/test.js +1 -1
- package/package.json +1 -1
package/dist/api/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,`__esModule`,{value:!0});const e=require(`../cookie-B3SvZDri.cjs`);let t=require(`util`);t=e.__toESM(t);const n=`x-blink-stream-response-format`,r=`BLINK_API_SERVER_URL`;var i=(e,t)=>(e=e.replace(/\/+$/,``),e+=`/`,t=t.replace(/^\/+/,``),e+t),a=(e,t)=>{for(let[n,r]of Object.entries(t)){let t=RegExp(`/:`+n+`(?:{[^/]+})?\\??`);e=e.replace(t,r?`/${r}`:``)}return e},o=e=>{let t=new URLSearchParams;for(let[n,r]of Object.entries(e)){if(r===void 0)continue;if(Array.isArray(r))for(let e of r)t.append(n,e);else t.set(n,r)}return t},s=(e,t)=>{switch(t){case`ws`:return e.replace(/^http/,`ws`);case`http`:return e.replace(/^ws/,`http`)}},c=e=>/^https?:\/\/[^\/]+?\/index(?=\?|$)/.test(e)?e.replace(/\/index(?=\?|$)/,`/`):e.replace(/\/index(?=\?|$)/,``);function l(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function u(e,t){if(!l(e)&&!l(t))return t;let n={...e};for(let e in t){let r=t[e];l(n[e])&&l(r)?n[e]=u(n[e],r):n[e]=r}return n}var d=(e,t)=>{let n=new Proxy(()=>{},{get(n,r){if(!(typeof r!=`string`||r===`then`))return d(e,[...t,r])},apply(n,r,i){return e({path:t,args:i})}});return n},f=class{url;method;queryParams=void 0;pathParams={};rBody;cType=void 0;constructor(e,t){this.url=e,this.method=t}fetch=async(t,n)=>{if(t){if(t.query&&(this.queryParams=o(t.query)),t.form){let e=new FormData;for(let[n,r]of Object.entries(t.form))if(Array.isArray(r))for(let t of r)e.append(n,t);else e.append(n,r);this.rBody=e}t.json&&(this.rBody=JSON.stringify(t.json),this.cType=`application/json`),t.param&&(this.pathParams=t.param)}let r=this.method.toUpperCase(),i={...t?.header,...typeof n?.headers==`function`?await n.headers():n?.headers};if(t?.cookie){let n=[];for(let[r,i]of Object.entries(t.cookie))n.push(e.serialize(r,i,{path:`/`}));i.Cookie=n.join(`,`)}this.cType&&(i[`Content-Type`]=this.cType);let s=new Headers(i??void 0),l=this.url;l=c(l),l=a(l,this.pathParams),this.queryParams&&(l=l+`?`+this.queryParams.toString()),r=this.method.toUpperCase();let u=!(r===`GET`||r===`HEAD`);return(n?.fetch||fetch)(l,{body:u?this.rBody:void 0,method:r,headers:s,...n?.init})}},p=(e,t)=>d(function n(r){let l=[...r.path],d=l.slice(-3).reverse();if(d[0]===`toString`)return d[1]===`name`?d[2]||``:n.toString();if(d[0]===`valueOf`)return d[1]===`name`?d[2]||``:n;let p=``;if(/^\$/.test(d[0])){let e=l.pop();e&&(p=e.replace(/^\$/,``))}let m=l.join(`/`),h=i(e,m);if(p===`url`){let e=h;return r.args[0]&&(r.args[0].param&&(e=a(h,r.args[0].param)),r.args[0].query&&(e=e+`?`+o(r.args[0].query).toString())),e=c(e),new URL(e)}if(p===`ws`){let e=s(r.args[0]&&r.args[0].param?a(h,r.args[0].param):h,`ws`),n=new URL(e),i=r.args[0]?.query;i&&Object.entries(i).forEach(([e,t])=>{Array.isArray(t)?t.forEach(t=>n.searchParams.append(e,t)):n.searchParams.set(e,t)});let o=(...e)=>t?.webSocket!==void 0&&typeof t.webSocket==`function`?t.webSocket(...e):new WebSocket(...e);return o(n.toString())}let g=new f(h,p);if(p){t??={};let e=u(t,{...r.args[1]});return g.fetch(r.args[0],e)}return g},[]);const m=process.env.BLINK_API_SERVER_URL,h=p(m??``,{fetch:m?void 0:async()=>{throw console.warn(`Your code is attempting to use the Blink API server, but no API server is configured for this Blink agent.`),Error(`No API server is configured for this Blink agent. External APIs are not available.`)}}),g=Object.freeze({upsert:async e=>{let t=await h.chat[`:id`].$post({param:{id:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}return{id:e}},message:async(e,t,n)=>{let r=await h.chat[`:id`].sendMessages.$post({param:{id:e},json:{messages:[t],behavior:n?.behavior??`enqueue`}});if(r.status!==204){let e=await r.json();throw Error(e.error)}}}),_=Object.freeze({get:async e=>{let t=await h.storage[`:key`].$get({param:{key:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}let n=await t.json();return n.value},set:async(e,t)=>{let n=await h.storage[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}}});function v(e){return{version:`v0`,supportsWebhooks:e.webhook!==void 0,fetch:async t=>{let n=new URL(t.url);switch(n.pathname){case`/sendMessages`:return b(t,e);case`/webhook`:return x(t,e);default:return new Response(`Not found`,{status:404})}}}}let y;async function b(e,n){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});let r;try{r=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await n.sendMessages({messages:r.messages,request:e})}catch(e){return new Response(JSON.stringify({error:t.default.inspect(e)}),{status:500})}if(i instanceof Response)return i;if(i instanceof ReadableStream)return new Response(i,{headers:{"Content-Type":`text/event-stream`}});if(typeof i!=`object`||typeof i==`object`&&!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);y||=new TextEncoder;let a=new TransformStream({transform(e,t){t.enqueue(y.encode(`data: ${JSON.stringify(e)}\n\n`))}}),o=i.toUIMessageStream();return o.pipeTo(a.writable),new Response(a.readable,{headers:{"Content-Type":`text/event-stream`}})}async function x(e,n){if(!n.webhook)return new Response(`No webhook function provided`,{status:501});try{let t=await n.webhook(e);return t||new Response(`OK`,{status:200})}catch(e){return new Response(JSON.stringify({error:t.default.inspect(e)}),{status:500})}}function S(e,t){return e.headers.set(`x-blink-stream-response-format`,t),e}var C={agent:v,chat:g,storage:_};exports.agent=v,exports.chat=g,exports.default=C,exports.storage=_,exports.withResponseFormat=S;
|
|
1
|
+
Object.defineProperty(exports,`__esModule`,{value:!0});const e=require(`../cookie-B3SvZDri.cjs`);let t=require(`util`);t=e.__toESM(t);const n=`x-blink-stream-response-format`,r=`BLINK_API_SERVER_URL`;var i=(e,t)=>(e=e.replace(/\/+$/,``),e+=`/`,t=t.replace(/^\/+/,``),e+t),a=(e,t)=>{for(let[n,r]of Object.entries(t)){let t=RegExp(`/:`+n+`(?:{[^/]+})?\\??`);e=e.replace(t,r?`/${r}`:``)}return e},o=e=>{let t=new URLSearchParams;for(let[n,r]of Object.entries(e)){if(r===void 0)continue;if(Array.isArray(r))for(let e of r)t.append(n,e);else t.set(n,r)}return t},s=(e,t)=>{switch(t){case`ws`:return e.replace(/^http/,`ws`);case`http`:return e.replace(/^ws/,`http`)}},c=e=>/^https?:\/\/[^\/]+?\/index(?=\?|$)/.test(e)?e.replace(/\/index(?=\?|$)/,`/`):e.replace(/\/index(?=\?|$)/,``);function l(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function u(e,t){if(!l(e)&&!l(t))return t;let n={...e};for(let e in t){let r=t[e];l(n[e])&&l(r)?n[e]=u(n[e],r):n[e]=r}return n}var d=(e,t)=>{let n=new Proxy(()=>{},{get(n,r){if(!(typeof r!=`string`||r===`then`))return d(e,[...t,r])},apply(n,r,i){return e({path:t,args:i})}});return n},f=class{url;method;queryParams=void 0;pathParams={};rBody;cType=void 0;constructor(e,t){this.url=e,this.method=t}fetch=async(t,n)=>{if(t){if(t.query&&(this.queryParams=o(t.query)),t.form){let e=new FormData;for(let[n,r]of Object.entries(t.form))if(Array.isArray(r))for(let t of r)e.append(n,t);else e.append(n,r);this.rBody=e}t.json&&(this.rBody=JSON.stringify(t.json),this.cType=`application/json`),t.param&&(this.pathParams=t.param)}let r=this.method.toUpperCase(),i={...t?.header,...typeof n?.headers==`function`?await n.headers():n?.headers};if(t?.cookie){let n=[];for(let[r,i]of Object.entries(t.cookie))n.push(e.serialize(r,i,{path:`/`}));i.Cookie=n.join(`,`)}this.cType&&(i[`Content-Type`]=this.cType);let s=new Headers(i??void 0),l=this.url;l=c(l),l=a(l,this.pathParams),this.queryParams&&(l=l+`?`+this.queryParams.toString()),r=this.method.toUpperCase();let u=!(r===`GET`||r===`HEAD`);return(n?.fetch||fetch)(l,{body:u?this.rBody:void 0,method:r,headers:s,...n?.init})}},p=(e,t)=>d(function n(r){let l=[...r.path],d=l.slice(-3).reverse();if(d[0]===`toString`)return d[1]===`name`?d[2]||``:n.toString();if(d[0]===`valueOf`)return d[1]===`name`?d[2]||``:n;let p=``;if(/^\$/.test(d[0])){let e=l.pop();e&&(p=e.replace(/^\$/,``))}let m=l.join(`/`),h=i(e,m);if(p===`url`){let e=h;return r.args[0]&&(r.args[0].param&&(e=a(h,r.args[0].param)),r.args[0].query&&(e=e+`?`+o(r.args[0].query).toString())),e=c(e),new URL(e)}if(p===`ws`){let e=s(r.args[0]&&r.args[0].param?a(h,r.args[0].param):h,`ws`),n=new URL(e),i=r.args[0]?.query;i&&Object.entries(i).forEach(([e,t])=>{Array.isArray(t)?t.forEach(t=>n.searchParams.append(e,t)):n.searchParams.set(e,t)});let o=(...e)=>t?.webSocket!==void 0&&typeof t.webSocket==`function`?t.webSocket(...e):new WebSocket(...e);return o(n.toString())}let g=new f(h,p);if(p){t??={};let e=u(t,{...r.args[1]});return g.fetch(r.args[0],e)}return g},[]);const m=process.env.BLINK_API_SERVER_URL,h=p(m??``,{fetch:m?void 0:async()=>{throw console.warn(`Your code is attempting to use the Blink API server, but no API server is configured for this Blink agent.`),Error(`No API server is configured for this Blink agent. External APIs are not available.`)}}),g=Object.freeze({upsert:async e=>{let t=await h.chat[`:id`].$post({param:{id:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}return{id:e}},message:async(e,t,n)=>{let r=await h.chat[`:id`].sendMessages.$post({param:{id:e},json:{messages:[t],behavior:n?.behavior??`enqueue`}});if(r.status!==204){let e=await r.json();throw Error(e.error)}}}),_=Object.freeze({get:async e=>{let t=await h.storage[`:key`].$get({param:{key:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}let n=await t.json();return n.value},set:async(e,t)=>{let n=await h.storage[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}}});function v(e){return{version:`v0`,supportsWebhooks:e.webhook!==void 0,fetch:async t=>{let n=new URL(t.url);switch(n.pathname){case`/sendMessages`:return b(t,e);case`/webhook`:return x(t,e);default:return new Response(`Not found`,{status:404})}}}}let y;async function b(e,n){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});let r;try{r=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await n.sendMessages({messages:r.messages,request:e})}catch(e){return new Response(JSON.stringify({error:t.default.inspect(e)}),{status:500})}if(i instanceof Response)return i;if(i instanceof ReadableStream)return new Response(i,{headers:{"Content-Type":`text/event-stream`,"Cache-Control":`no-cache, no-transform`,Connection:`keep-alive`}});if(typeof i!=`object`||typeof i==`object`&&!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);y||=new TextEncoder;let a=new TransformStream({transform(e,t){t.enqueue(y.encode(`data: ${JSON.stringify(e)}\n\n`))}}),o=i.toUIMessageStream();return o.pipeTo(a.writable),new Response(a.readable,{headers:{"Content-Type":`text/event-stream`,"Cache-Control":`no-cache, no-transform`,Connection:`keep-alive`}})}async function x(e,n){if(!n.webhook)return new Response(`No webhook function provided`,{status:501});try{let t=await n.webhook(e);return t||new Response(`OK`,{status:200})}catch(e){return new Response(JSON.stringify({error:t.default.inspect(e)}),{status:500})}}function S(e,t){return e.headers.set(`x-blink-stream-response-format`,t),e}var C={agent:v,chat:g,storage:_};exports.agent=v,exports.chat=g,exports.default=C,exports.storage=_,exports.withResponseFormat=S;
|
package/dist/api/index.d.cts
CHANGED
|
@@ -5,7 +5,14 @@ interface Chat {
|
|
|
5
5
|
readonly id: string;
|
|
6
6
|
}
|
|
7
7
|
interface MessageOptions {
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* behavior of the chat when sending this message.
|
|
10
|
+
*
|
|
11
|
+
* - "enqueue" will add messages to the chat and start the chat eventually.
|
|
12
|
+
* - "interrupt" will interrupt the chat if running and send messages.
|
|
13
|
+
* - "append" will add messages to the chat.
|
|
14
|
+
*/
|
|
15
|
+
readonly behavior?: "enqueue" | "interrupt" | "append";
|
|
9
16
|
}
|
|
10
17
|
interface Message<METADATA = unknown, DATA_TYPES extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> {
|
|
11
18
|
readonly role: UIMessage["role"];
|
package/dist/api/index.d.ts
CHANGED
|
@@ -5,7 +5,14 @@ interface Chat {
|
|
|
5
5
|
readonly id: string;
|
|
6
6
|
}
|
|
7
7
|
interface MessageOptions {
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* behavior of the chat when sending this message.
|
|
10
|
+
*
|
|
11
|
+
* - "enqueue" will add messages to the chat and start the chat eventually.
|
|
12
|
+
* - "interrupt" will interrupt the chat if running and send messages.
|
|
13
|
+
* - "append" will add messages to the chat.
|
|
14
|
+
*/
|
|
15
|
+
readonly behavior?: "enqueue" | "interrupt" | "append";
|
|
9
16
|
}
|
|
10
17
|
interface Message<METADATA = unknown, DATA_TYPES extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> {
|
|
11
18
|
readonly role: UIMessage["role"];
|
package/dist/api/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{serialize as e}from"../cookie-BiKY7-P1.js";import t from"util";var n=(e,t)=>(e=e.replace(/\/+$/,``),e+=`/`,t=t.replace(/^\/+/,``),e+t),r=(e,t)=>{for(let[n,r]of Object.entries(t)){let t=RegExp(`/:`+n+`(?:{[^/]+})?\\??`);e=e.replace(t,r?`/${r}`:``)}return e},i=e=>{let t=new URLSearchParams;for(let[n,r]of Object.entries(e)){if(r===void 0)continue;if(Array.isArray(r))for(let e of r)t.append(n,e);else t.set(n,r)}return t},a=(e,t)=>{switch(t){case`ws`:return e.replace(/^http/,`ws`);case`http`:return e.replace(/^ws/,`http`)}},o=e=>/^https?:\/\/[^\/]+?\/index(?=\?|$)/.test(e)?e.replace(/\/index(?=\?|$)/,`/`):e.replace(/\/index(?=\?|$)/,``);function s(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function c(e,t){if(!s(e)&&!s(t))return t;let n={...e};for(let e in t){let r=t[e];s(n[e])&&s(r)?n[e]=c(n[e],r):n[e]=r}return n}var l=(e,t)=>{let n=new Proxy(()=>{},{get(n,r){if(!(typeof r!=`string`||r===`then`))return l(e,[...t,r])},apply(n,r,i){return e({path:t,args:i})}});return n},u=class{url;method;queryParams=void 0;pathParams={};rBody;cType=void 0;constructor(e,t){this.url=e,this.method=t}fetch=async(t,n)=>{if(t){if(t.query&&(this.queryParams=i(t.query)),t.form){let e=new FormData;for(let[n,r]of Object.entries(t.form))if(Array.isArray(r))for(let t of r)e.append(n,t);else e.append(n,r);this.rBody=e}t.json&&(this.rBody=JSON.stringify(t.json),this.cType=`application/json`),t.param&&(this.pathParams=t.param)}let a=this.method.toUpperCase(),s={...t?.header,...typeof n?.headers==`function`?await n.headers():n?.headers};if(t?.cookie){let n=[];for(let[r,i]of Object.entries(t.cookie))n.push(e(r,i,{path:`/`}));s.Cookie=n.join(`,`)}this.cType&&(s[`Content-Type`]=this.cType);let c=new Headers(s??void 0),l=this.url;l=o(l),l=r(l,this.pathParams),this.queryParams&&(l=l+`?`+this.queryParams.toString()),a=this.method.toUpperCase();let u=!(a===`GET`||a===`HEAD`);return(n?.fetch||fetch)(l,{body:u?this.rBody:void 0,method:a,headers:c,...n?.init})}},d=(e,t)=>l(function s(l){let d=[...l.path],f=d.slice(-3).reverse();if(f[0]===`toString`)return f[1]===`name`?f[2]||``:s.toString();if(f[0]===`valueOf`)return f[1]===`name`?f[2]||``:s;let p=``;if(/^\$/.test(f[0])){let e=d.pop();e&&(p=e.replace(/^\$/,``))}let m=d.join(`/`),h=n(e,m);if(p===`url`){let e=h;return l.args[0]&&(l.args[0].param&&(e=r(h,l.args[0].param)),l.args[0].query&&(e=e+`?`+i(l.args[0].query).toString())),e=o(e),new URL(e)}if(p===`ws`){let e=a(l.args[0]&&l.args[0].param?r(h,l.args[0].param):h,`ws`),n=new URL(e),i=l.args[0]?.query;i&&Object.entries(i).forEach(([e,t])=>{Array.isArray(t)?t.forEach(t=>n.searchParams.append(e,t)):n.searchParams.set(e,t)});let o=(...e)=>t?.webSocket!==void 0&&typeof t.webSocket==`function`?t.webSocket(...e):new WebSocket(...e);return o(n.toString())}let g=new u(h,p);if(p){t??={};let e=c(t,{...l.args[1]});return g.fetch(l.args[0],e)}return g},[]);const f=process.env.BLINK_API_SERVER_URL,p=d(f??``,{fetch:f?void 0:async()=>{throw console.warn(`Your code is attempting to use the Blink API server, but no API server is configured for this Blink agent.`),Error(`No API server is configured for this Blink agent. External APIs are not available.`)}}),m=Object.freeze({upsert:async e=>{let t=await p.chat[`:id`].$post({param:{id:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}return{id:e}},message:async(e,t,n)=>{let r=await p.chat[`:id`].sendMessages.$post({param:{id:e},json:{messages:[t],behavior:n?.behavior??`enqueue`}});if(r.status!==204){let e=await r.json();throw Error(e.error)}}}),h=Object.freeze({get:async e=>{let t=await p.storage[`:key`].$get({param:{key:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}let n=await t.json();return n.value},set:async(e,t)=>{let n=await p.storage[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}}});function g(e){return{version:`v0`,supportsWebhooks:e.webhook!==void 0,fetch:async t=>{let n=new URL(t.url);switch(n.pathname){case`/sendMessages`:return v(t,e);case`/webhook`:return y(t,e);default:return new Response(`Not found`,{status:404})}}}}let _;async function v(e,n){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});let r;try{r=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await n.sendMessages({messages:r.messages,request:e})}catch(e){return new Response(JSON.stringify({error:t.inspect(e)}),{status:500})}if(i instanceof Response)return i;if(i instanceof ReadableStream)return new Response(i,{headers:{"Content-Type":`text/event-stream`}});if(typeof i!=`object`||typeof i==`object`&&!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);_||=new TextEncoder;let a=new TransformStream({transform(e,t){t.enqueue(_.encode(`data: ${JSON.stringify(e)}\n\n`))}}),o=i.toUIMessageStream();return o.pipeTo(a.writable),new Response(a.readable,{headers:{"Content-Type":`text/event-stream`}})}async function y(e,n){if(!n.webhook)return new Response(`No webhook function provided`,{status:501});try{let t=await n.webhook(e);return t||new Response(`OK`,{status:200})}catch(e){return new Response(JSON.stringify({error:t.inspect(e)}),{status:500})}}function b(e,t){return e.headers.set(`x-blink-stream-response-format`,t),e}var x={agent:g,chat:m,storage:h};export{g as agent,m as chat,x as default,h as storage,b as withResponseFormat};
|
|
1
|
+
import{serialize as e}from"../cookie-BiKY7-P1.js";import t from"util";var n=(e,t)=>(e=e.replace(/\/+$/,``),e+=`/`,t=t.replace(/^\/+/,``),e+t),r=(e,t)=>{for(let[n,r]of Object.entries(t)){let t=RegExp(`/:`+n+`(?:{[^/]+})?\\??`);e=e.replace(t,r?`/${r}`:``)}return e},i=e=>{let t=new URLSearchParams;for(let[n,r]of Object.entries(e)){if(r===void 0)continue;if(Array.isArray(r))for(let e of r)t.append(n,e);else t.set(n,r)}return t},a=(e,t)=>{switch(t){case`ws`:return e.replace(/^http/,`ws`);case`http`:return e.replace(/^ws/,`http`)}},o=e=>/^https?:\/\/[^\/]+?\/index(?=\?|$)/.test(e)?e.replace(/\/index(?=\?|$)/,`/`):e.replace(/\/index(?=\?|$)/,``);function s(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function c(e,t){if(!s(e)&&!s(t))return t;let n={...e};for(let e in t){let r=t[e];s(n[e])&&s(r)?n[e]=c(n[e],r):n[e]=r}return n}var l=(e,t)=>{let n=new Proxy(()=>{},{get(n,r){if(!(typeof r!=`string`||r===`then`))return l(e,[...t,r])},apply(n,r,i){return e({path:t,args:i})}});return n},u=class{url;method;queryParams=void 0;pathParams={};rBody;cType=void 0;constructor(e,t){this.url=e,this.method=t}fetch=async(t,n)=>{if(t){if(t.query&&(this.queryParams=i(t.query)),t.form){let e=new FormData;for(let[n,r]of Object.entries(t.form))if(Array.isArray(r))for(let t of r)e.append(n,t);else e.append(n,r);this.rBody=e}t.json&&(this.rBody=JSON.stringify(t.json),this.cType=`application/json`),t.param&&(this.pathParams=t.param)}let a=this.method.toUpperCase(),s={...t?.header,...typeof n?.headers==`function`?await n.headers():n?.headers};if(t?.cookie){let n=[];for(let[r,i]of Object.entries(t.cookie))n.push(e(r,i,{path:`/`}));s.Cookie=n.join(`,`)}this.cType&&(s[`Content-Type`]=this.cType);let c=new Headers(s??void 0),l=this.url;l=o(l),l=r(l,this.pathParams),this.queryParams&&(l=l+`?`+this.queryParams.toString()),a=this.method.toUpperCase();let u=!(a===`GET`||a===`HEAD`);return(n?.fetch||fetch)(l,{body:u?this.rBody:void 0,method:a,headers:c,...n?.init})}},d=(e,t)=>l(function s(l){let d=[...l.path],f=d.slice(-3).reverse();if(f[0]===`toString`)return f[1]===`name`?f[2]||``:s.toString();if(f[0]===`valueOf`)return f[1]===`name`?f[2]||``:s;let p=``;if(/^\$/.test(f[0])){let e=d.pop();e&&(p=e.replace(/^\$/,``))}let m=d.join(`/`),h=n(e,m);if(p===`url`){let e=h;return l.args[0]&&(l.args[0].param&&(e=r(h,l.args[0].param)),l.args[0].query&&(e=e+`?`+i(l.args[0].query).toString())),e=o(e),new URL(e)}if(p===`ws`){let e=a(l.args[0]&&l.args[0].param?r(h,l.args[0].param):h,`ws`),n=new URL(e),i=l.args[0]?.query;i&&Object.entries(i).forEach(([e,t])=>{Array.isArray(t)?t.forEach(t=>n.searchParams.append(e,t)):n.searchParams.set(e,t)});let o=(...e)=>t?.webSocket!==void 0&&typeof t.webSocket==`function`?t.webSocket(...e):new WebSocket(...e);return o(n.toString())}let g=new u(h,p);if(p){t??={};let e=c(t,{...l.args[1]});return g.fetch(l.args[0],e)}return g},[]);const f=process.env.BLINK_API_SERVER_URL,p=d(f??``,{fetch:f?void 0:async()=>{throw console.warn(`Your code is attempting to use the Blink API server, but no API server is configured for this Blink agent.`),Error(`No API server is configured for this Blink agent. External APIs are not available.`)}}),m=Object.freeze({upsert:async e=>{let t=await p.chat[`:id`].$post({param:{id:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}return{id:e}},message:async(e,t,n)=>{let r=await p.chat[`:id`].sendMessages.$post({param:{id:e},json:{messages:[t],behavior:n?.behavior??`enqueue`}});if(r.status!==204){let e=await r.json();throw Error(e.error)}}}),h=Object.freeze({get:async e=>{let t=await p.storage[`:key`].$get({param:{key:e}});if(t.status!==200){let e=await t.json();throw Error(e.error)}let n=await t.json();return n.value},set:async(e,t)=>{let n=await p.storage[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}}});function g(e){return{version:`v0`,supportsWebhooks:e.webhook!==void 0,fetch:async t=>{let n=new URL(t.url);switch(n.pathname){case`/sendMessages`:return v(t,e);case`/webhook`:return y(t,e);default:return new Response(`Not found`,{status:404})}}}}let _;async function v(e,n){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});let r;try{r=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await n.sendMessages({messages:r.messages,request:e})}catch(e){return new Response(JSON.stringify({error:t.inspect(e)}),{status:500})}if(i instanceof Response)return i;if(i instanceof ReadableStream)return new Response(i,{headers:{"Content-Type":`text/event-stream`,"Cache-Control":`no-cache, no-transform`,Connection:`keep-alive`}});if(typeof i!=`object`||typeof i==`object`&&!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);_||=new TextEncoder;let a=new TransformStream({transform(e,t){t.enqueue(_.encode(`data: ${JSON.stringify(e)}\n\n`))}}),o=i.toUIMessageStream();return o.pipeTo(a.writable),new Response(a.readable,{headers:{"Content-Type":`text/event-stream`,"Cache-Control":`no-cache, no-transform`,Connection:`keep-alive`}})}async function y(e,n){if(!n.webhook)return new Response(`No webhook function provided`,{status:501});try{let t=await n.webhook(e);return t||new Response(`OK`,{status:200})}catch(e){return new Response(JSON.stringify({error:t.inspect(e)}),{status:500})}}function b(e,t){return e.headers.set(`x-blink-stream-response-format`,t),e}var x={agent:g,chat:m,storage:h};export{g as agent,m as chat,x as default,h as storage,b as withResponseFormat};
|