blink 0.1.40 → 0.1.41

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.
@@ -1,3 +1,3 @@
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 y(t,e);case`/webhook`:return b(t,e);default:return new Response(`Not found`,{status:404})}}}}async function y(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;let a;if(i instanceof ReadableStream)a=i;else{if(typeof i!=`object`||!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);a=i.toUIMessageStream()}return new Response(a.pipeThrough(new TransformStream({transform(e,t){t.enqueue(`data: ${JSON.stringify(e)}\n\n`)},flush(e){e.enqueue(`data: [DONE]
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)}}}),v={with(e,t){let n={};for(let[r,i]of Object.entries(e))n[r]=i.with(t);return n},prefix(e,t){let n={};for(let[r,i]of Object.entries(e))n[`${t}${r}`]=i;return n}};function y(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})}}}}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;let a;if(i instanceof ReadableStream)a=i;else{if(typeof i!=`object`||!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);a=i.toUIMessageStream()}return new Response(a.pipeThrough(new TransformStream({transform(e,t){t.enqueue(`data: ${JSON.stringify(e)}\n\n`)},flush(e){e.enqueue(`data: [DONE]
2
2
 
3
- `)}})).pipeThrough(new TextEncoderStream),{headers:{"content-type":`text/event-stream`,"cache-control":`no-cache, no-transform`,connection:`keep-alive`,"x-vercel-ai-ui-message-stream":`v1`,"x-accel-buffering":`no`}})}async function b(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 x(e,t){return e.headers.set(`x-blink-stream-response-format`,t),e}var S={agent:v,chat:g,storage:_};exports.agent=v,exports.chat=g,exports.default=S,exports.storage=_,exports.withResponseFormat=x;
3
+ `)}})).pipeThrough(new TextEncoderStream),{headers:{"content-type":`text/event-stream`,"cache-control":`no-cache, no-transform`,connection:`keep-alive`,"x-vercel-ai-ui-message-stream":`v1`,"x-accel-buffering":`no`}})}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:y,chat:g,storage:_,tools:v};exports.agent=y,exports.chat=g,exports.default=C,exports.storage=_,exports.tools=v,exports.withResponseFormat=S;
@@ -1,4 +1,5 @@
1
- import { AsyncIterableStream, InferUIMessageChunk, UIDataTypes, UIMessage, UIMessagePart, UITools } from "ai";
1
+ import * as ai0 from "ai";
2
+ import { AsyncIterableStream, InferUIMessageChunk, Tool, ToolSet, UIDataTypes, UIMessage, UIMessagePart, UITools } from "ai";
2
3
 
3
4
  //#region src/api/chat.d.ts
4
5
  interface Chat {
@@ -24,6 +25,39 @@ declare const chat: Readonly<{
24
25
  message: (id: string, message: Message, options?: MessageOptions) => Promise<void>;
25
26
  }>;
26
27
  //#endregion
28
+ //#region src/api/tools.d.ts
29
+ /**
30
+ * ToolWith is a tool that supports the "with" method.
31
+ *
32
+ * @param CONTEXT The context type.
33
+ * @param TOOL The tool type.
34
+ * @returns The tool with the given context.
35
+ */
36
+ type ToolWith<CONTEXT, TOOL extends Tool> = TOOL & {
37
+ with: (context: CONTEXT) => TOOL;
38
+ };
39
+ /**
40
+ * Tools are helpers for managing tools.
41
+ */
42
+ declare const tools: {
43
+ /**
44
+ * with adds context to a set of tools that supports the "with" method.
45
+ *
46
+ * @param context
47
+ * @param tools
48
+ * @returns
49
+ */
50
+ with<TOOLS extends Record<string, ToolWith<CONTEXT, Tool>>, CONTEXT>(tools: TOOLS, context: CONTEXT): Record<string, Tool>;
51
+ /**
52
+ * prefix adds a prefix to all the tools in a tool set.
53
+ *
54
+ * @param tools The tool set to prefix.
55
+ * @param prefix The prefix to add to the tools.
56
+ * @returns The prefixed tool set.
57
+ */
58
+ prefix(tools: ToolSet, prefix: string): ToolSet;
59
+ };
60
+ //#endregion
27
61
  //#region src/api/storage.d.ts
28
62
  /**
29
63
  * Storage is namespaced by the agent.
@@ -84,6 +118,10 @@ declare const _default: {
84
118
  get: (key: string) => Promise<string | undefined>;
85
119
  set: (key: string, value: string) => Promise<void>;
86
120
  }>;
121
+ tools: {
122
+ with<TOOLS extends Record<string, ToolWith<CONTEXT, ai0.Tool>>, CONTEXT>(tools: TOOLS, context: CONTEXT): Record<string, ai0.Tool>;
123
+ prefix(tools: ai0.ToolSet, prefix: string): ai0.ToolSet;
124
+ };
87
125
  };
88
126
  //#endregion
89
- export { Agent, AgentDefaultExport, Chat, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, agent, chat, _default as default, storage, withResponseFormat };
127
+ export { Agent, AgentDefaultExport, Chat, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, ToolWith, agent, chat, _default as default, storage, tools, withResponseFormat };
@@ -1,4 +1,5 @@
1
- import { AsyncIterableStream, InferUIMessageChunk, UIDataTypes, UIMessage, UIMessagePart, UITools } from "ai";
1
+ import * as ai0 from "ai";
2
+ import { AsyncIterableStream, InferUIMessageChunk, Tool, ToolSet, UIDataTypes, UIMessage, UIMessagePart, UITools } from "ai";
2
3
 
3
4
  //#region src/api/chat.d.ts
4
5
  interface Chat {
@@ -24,6 +25,39 @@ declare const chat: Readonly<{
24
25
  message: (id: string, message: Message, options?: MessageOptions) => Promise<void>;
25
26
  }>;
26
27
  //#endregion
28
+ //#region src/api/tools.d.ts
29
+ /**
30
+ * ToolWith is a tool that supports the "with" method.
31
+ *
32
+ * @param CONTEXT The context type.
33
+ * @param TOOL The tool type.
34
+ * @returns The tool with the given context.
35
+ */
36
+ type ToolWith<CONTEXT, TOOL extends Tool> = TOOL & {
37
+ with: (context: CONTEXT) => TOOL;
38
+ };
39
+ /**
40
+ * Tools are helpers for managing tools.
41
+ */
42
+ declare const tools: {
43
+ /**
44
+ * with adds context to a set of tools that supports the "with" method.
45
+ *
46
+ * @param context
47
+ * @param tools
48
+ * @returns
49
+ */
50
+ with<TOOLS extends Record<string, ToolWith<CONTEXT, Tool>>, CONTEXT>(tools: TOOLS, context: CONTEXT): Record<string, Tool>;
51
+ /**
52
+ * prefix adds a prefix to all the tools in a tool set.
53
+ *
54
+ * @param tools The tool set to prefix.
55
+ * @param prefix The prefix to add to the tools.
56
+ * @returns The prefixed tool set.
57
+ */
58
+ prefix(tools: ToolSet, prefix: string): ToolSet;
59
+ };
60
+ //#endregion
27
61
  //#region src/api/storage.d.ts
28
62
  /**
29
63
  * Storage is namespaced by the agent.
@@ -84,6 +118,10 @@ declare const _default: {
84
118
  get: (key: string) => Promise<string | undefined>;
85
119
  set: (key: string, value: string) => Promise<void>;
86
120
  }>;
121
+ tools: {
122
+ with<TOOLS extends Record<string, ToolWith<CONTEXT, ai0.Tool>>, CONTEXT>(tools: TOOLS, context: CONTEXT): Record<string, ai0.Tool>;
123
+ prefix(tools: ai0.ToolSet, prefix: string): ai0.ToolSet;
124
+ };
87
125
  };
88
126
  //#endregion
89
- export { Agent, AgentDefaultExport, Chat, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, agent, chat, _default as default, storage, withResponseFormat };
127
+ export { Agent, AgentDefaultExport, Chat, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, ToolWith, agent, chat, _default as default, storage, tools, withResponseFormat };
package/dist/api/index.js CHANGED
@@ -1,3 +1,3 @@
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 _(t,e);case`/webhook`:return v(t,e);default:return new Response(`Not found`,{status:404})}}}}async function _(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;let a;if(i instanceof ReadableStream)a=i;else{if(typeof i!=`object`||!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);a=i.toUIMessageStream()}return new Response(a.pipeThrough(new TransformStream({transform(e,t){t.enqueue(`data: ${JSON.stringify(e)}\n\n`)},flush(e){e.enqueue(`data: [DONE]
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)}}}),g={with(e,t){let n={};for(let[r,i]of Object.entries(e))n[r]=i.with(t);return n},prefix(e,t){let n={};for(let[r,i]of Object.entries(e))n[`${t}${r}`]=i;return n}};function _(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})}}}}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;let a;if(i instanceof ReadableStream)a=i;else{if(typeof i!=`object`||!(`toUIMessageStream`in i))throw Error(`The agent must return a "Response", "ReadableStream", or "toUIMessageStream" function.`);a=i.toUIMessageStream()}return new Response(a.pipeThrough(new TransformStream({transform(e,t){t.enqueue(`data: ${JSON.stringify(e)}\n\n`)},flush(e){e.enqueue(`data: [DONE]
2
2
 
3
- `)}})).pipeThrough(new TextEncoderStream),{headers:{"content-type":`text/event-stream`,"cache-control":`no-cache, no-transform`,connection:`keep-alive`,"x-vercel-ai-ui-message-stream":`v1`,"x-accel-buffering":`no`}})}async function v(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 y(e,t){return e.headers.set(`x-blink-stream-response-format`,t),e}var b={agent:g,chat:m,storage:h};export{g as agent,m as chat,b as default,h as storage,y as withResponseFormat};
3
+ `)}})).pipeThrough(new TextEncoderStream),{headers:{"content-type":`text/event-stream`,"cache-control":`no-cache, no-transform`,connection:`keep-alive`,"x-vercel-ai-ui-message-stream":`v1`,"x-accel-buffering":`no`}})}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:_,chat:m,storage:h,tools:g};export{_ as agent,m as chat,x as default,h as storage,g as tools,b as withResponseFormat};