blink 0.1.27 → 0.1.29

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 +1 @@
1
- const e=require(`../chunk-hhQzssFb.cjs`),t=require(`../api-wEN9yX1N.cjs`);exports.StreamResponseFormatHeader=t.StreamResponseFormatHeader,exports.agent=t.agent,exports.withResponseFormat=t.withResponseFormat;
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,2 +1,82 @@
1
- import { Agent, AgentDefaultExport, SendMessagesOptions, SendMessagesRequest, SendMessagesResponse, StreamResponseFormat, StreamResponseFormatHeader, agent, withResponseFormat } from "../index-BACRosVA.cjs";
2
- export { Agent, AgentDefaultExport, SendMessagesOptions, SendMessagesRequest, SendMessagesResponse, StreamResponseFormat, StreamResponseFormatHeader, agent, withResponseFormat };
1
+ import { AsyncIterableStream, InferUIMessageChunk, UIDataTypes, UIMessage, UIMessagePart, UITools } from "ai";
2
+
3
+ //#region src/api/chat.d.ts
4
+ interface Chat {
5
+ readonly id: string;
6
+ }
7
+ interface MessageOptions {
8
+ readonly behavior?: "enqueue" | "interrupt";
9
+ }
10
+ interface Message<METADATA = unknown, DATA_TYPES extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> {
11
+ readonly role: UIMessage["role"];
12
+ readonly parts: UIMessagePart<DATA_TYPES, TOOLS>[];
13
+ readonly metadata?: METADATA;
14
+ }
15
+ declare const chat: Readonly<{
16
+ upsert: (id: string) => Promise<Chat>;
17
+ message: (id: string, message: Message, options?: MessageOptions) => Promise<void>;
18
+ }>;
19
+ //#endregion
20
+ //#region src/api/storage.d.ts
21
+ /**
22
+ * Storage is namespaced by the agent.
23
+ */
24
+ declare const storage: Readonly<{
25
+ get: (key: string) => Promise<string | undefined>;
26
+ set: (key: string, value: string) => Promise<void>;
27
+ }>;
28
+ //#endregion
29
+ //#region src/api/index.d.ts
30
+ /**
31
+ * SendMessagesResponse is the response of the sendMessages function.
32
+ * It can be a stream of messages or an arbitrary response.
33
+ */
34
+ type SendMessagesResponse = {
35
+ toUIMessageStream(): AsyncIterableStream<InferUIMessageChunk<UIMessage>>;
36
+ } | Response | ReadableStream<InferUIMessageChunk<UIMessage>>;
37
+ /**
38
+ * SendMessagesOptions is the options for the sendMessages function.
39
+ */
40
+ interface SendMessagesOptions {
41
+ readonly messages: UIMessage[];
42
+ readonly abortSignal?: AbortSignal;
43
+ readonly request: Request;
44
+ }
45
+ interface Agent {
46
+ sendMessages(options: SendMessagesOptions): Promise<SendMessagesResponse> | SendMessagesResponse;
47
+ webhook?(request: Request): Promise<Response | void>;
48
+ }
49
+ /**
50
+ * AgentDefaultExport is the expected default export of a compiled agent.
51
+ * All routing logic is handled by the agent function.
52
+ *
53
+ * This maximizes flexibility over the agent's behavior -
54
+ * allowing users to add arbitrary routing logic to the agent.
55
+ */
56
+ type AgentDefaultExport = {
57
+ readonly version: "v0";
58
+ fetch: (request: Request) => Promise<Response> | Response;
59
+ readonly supportsWebhooks?: boolean;
60
+ };
61
+ /**
62
+ * agent is the main function to create a Blink agent.
63
+ *
64
+ * @param agent
65
+ * @returns
66
+ */
67
+ declare function agent(agent: Agent): AgentDefaultExport;
68
+ type StreamResponseFormat = "ui-message" | "openai-chat" | "openai-response" | "anthropic" | "google" | "xai";
69
+ declare function withResponseFormat(response: Response, format: StreamResponseFormat): Response;
70
+ declare const _default: {
71
+ agent: typeof agent;
72
+ chat: Readonly<{
73
+ upsert: (id: string) => Promise<Chat>;
74
+ message: (id: string, message: Message, options?: MessageOptions) => Promise<void>;
75
+ }>;
76
+ storage: Readonly<{
77
+ get: (key: string) => Promise<string | undefined>;
78
+ set: (key: string, value: string) => Promise<void>;
79
+ }>;
80
+ };
81
+ //#endregion
82
+ export { Agent, AgentDefaultExport, Chat, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, agent, chat, _default as default, storage, withResponseFormat };
@@ -1,2 +1,82 @@
1
- import { Agent, AgentDefaultExport, SendMessagesOptions, SendMessagesRequest, SendMessagesResponse, StreamResponseFormat, StreamResponseFormatHeader, agent, withResponseFormat } from "../index-CAMDiyg3.js";
2
- export { Agent, AgentDefaultExport, SendMessagesOptions, SendMessagesRequest, SendMessagesResponse, StreamResponseFormat, StreamResponseFormatHeader, agent, withResponseFormat };
1
+ import { AsyncIterableStream, InferUIMessageChunk, UIDataTypes, UIMessage, UIMessagePart, UITools } from "ai";
2
+
3
+ //#region src/api/chat.d.ts
4
+ interface Chat {
5
+ readonly id: string;
6
+ }
7
+ interface MessageOptions {
8
+ readonly behavior?: "enqueue" | "interrupt";
9
+ }
10
+ interface Message<METADATA = unknown, DATA_TYPES extends UIDataTypes = UIDataTypes, TOOLS extends UITools = UITools> {
11
+ readonly role: UIMessage["role"];
12
+ readonly parts: UIMessagePart<DATA_TYPES, TOOLS>[];
13
+ readonly metadata?: METADATA;
14
+ }
15
+ declare const chat: Readonly<{
16
+ upsert: (id: string) => Promise<Chat>;
17
+ message: (id: string, message: Message, options?: MessageOptions) => Promise<void>;
18
+ }>;
19
+ //#endregion
20
+ //#region src/api/storage.d.ts
21
+ /**
22
+ * Storage is namespaced by the agent.
23
+ */
24
+ declare const storage: Readonly<{
25
+ get: (key: string) => Promise<string | undefined>;
26
+ set: (key: string, value: string) => Promise<void>;
27
+ }>;
28
+ //#endregion
29
+ //#region src/api/index.d.ts
30
+ /**
31
+ * SendMessagesResponse is the response of the sendMessages function.
32
+ * It can be a stream of messages or an arbitrary response.
33
+ */
34
+ type SendMessagesResponse = {
35
+ toUIMessageStream(): AsyncIterableStream<InferUIMessageChunk<UIMessage>>;
36
+ } | Response | ReadableStream<InferUIMessageChunk<UIMessage>>;
37
+ /**
38
+ * SendMessagesOptions is the options for the sendMessages function.
39
+ */
40
+ interface SendMessagesOptions {
41
+ readonly messages: UIMessage[];
42
+ readonly abortSignal?: AbortSignal;
43
+ readonly request: Request;
44
+ }
45
+ interface Agent {
46
+ sendMessages(options: SendMessagesOptions): Promise<SendMessagesResponse> | SendMessagesResponse;
47
+ webhook?(request: Request): Promise<Response | void>;
48
+ }
49
+ /**
50
+ * AgentDefaultExport is the expected default export of a compiled agent.
51
+ * All routing logic is handled by the agent function.
52
+ *
53
+ * This maximizes flexibility over the agent's behavior -
54
+ * allowing users to add arbitrary routing logic to the agent.
55
+ */
56
+ type AgentDefaultExport = {
57
+ readonly version: "v0";
58
+ fetch: (request: Request) => Promise<Response> | Response;
59
+ readonly supportsWebhooks?: boolean;
60
+ };
61
+ /**
62
+ * agent is the main function to create a Blink agent.
63
+ *
64
+ * @param agent
65
+ * @returns
66
+ */
67
+ declare function agent(agent: Agent): AgentDefaultExport;
68
+ type StreamResponseFormat = "ui-message" | "openai-chat" | "openai-response" | "anthropic" | "google" | "xai";
69
+ declare function withResponseFormat(response: Response, format: StreamResponseFormat): Response;
70
+ declare const _default: {
71
+ agent: typeof agent;
72
+ chat: Readonly<{
73
+ upsert: (id: string) => Promise<Chat>;
74
+ message: (id: string, message: Message, options?: MessageOptions) => Promise<void>;
75
+ }>;
76
+ storage: Readonly<{
77
+ get: (key: string) => Promise<string | undefined>;
78
+ set: (key: string, value: string) => Promise<void>;
79
+ }>;
80
+ };
81
+ //#endregion
82
+ export { Agent, AgentDefaultExport, Chat, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, agent, chat, _default as default, storage, withResponseFormat };
package/dist/api/index.js CHANGED
@@ -1 +1 @@
1
- import{StreamResponseFormatHeader as e,agent as t,withResponseFormat as n}from"../api-vofIX7P2.js";export{e as StreamResponseFormatHeader,t as agent,n 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`}});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};