blink 0.1.50 → 0.1.52
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 +4 -2
- package/dist/api/index.d.cts +50 -1
- package/dist/api/index.d.ts +50 -1
- package/dist/api/index.js +4 -2
- package/dist/cli/chunk-ByIbJXPc.js +1 -0
- package/dist/cli/dev-DD1tf2AT.js +1625 -0
- package/dist/cli/devtools-D6beCbGn.js +91 -0
- package/dist/cli/esm-DiE8Xvft.js +1 -0
- package/dist/cli/execAsync-Cr3GNTA4.js +1 -0
- package/dist/cli/getMachineId-bsd-CY-GIKzh.js +1 -0
- package/dist/cli/getMachineId-darwin-BvUyLisY.js +2 -0
- package/dist/cli/getMachineId-linux-CaBhu4UD.js +1 -0
- package/dist/cli/getMachineId-unsupported-DsibGvjs.js +1 -0
- package/dist/cli/getMachineId-win-_QXKq1ge.js +1 -0
- package/dist/cli/index.js +36 -2040
- package/dist/cli/main-CiEwxfsi.js +138 -0
- package/dist/cli/undici-0ADe8h0l.js +94 -0
- package/dist/cli/undici-DjSrZtb9.js +1 -0
- package/dist/cli/wrapper-B4vDwpOq.js +6 -0
- package/package.json +2 -1
package/dist/api/index.cjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
Object.defineProperty(exports,`__esModule`,{value:!0});const e=require(`../cookie-BtKXrFr6.cjs`);let t=require(`util`);t=e.__toESM(t);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.serialize(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[e.APIServerURLEnvironmentVariable],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({kv:Object.freeze({get:async e=>{let t=await p.storage.kv[`: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.kv[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}},del:async e=>{let t=await p.storage.kv[`:key`].$delete({param:{key:e}});if(t.status!==204){let e=await t.json();throw Error(e.error)}}})}),g={with(e,t){let n={};for(let r of Object.keys(e)){let i=e[r];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`,supportsRequests:e.onRequest!==void 0,fetch:async n=>{let r=new URL(n.url);if(r.pathname.startsWith(`/_agent/`))switch(r.pathname){case`/_agent/send-messages`:return v(n,e);case`/_agent/health`:return new Response(`OK`,{status:200});default:return new Response(`Not found`,{status:404})}if(e.onRequest){let r;try{r=await e.onRequest(n)}catch(e){return new Response(JSON.stringify({error:t.default.inspect(e)}),{status:500})}if(r)return r}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,chat:r.chat})}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-BtKXrFr6.cjs`);let t=require(`util`);t=e.__toESM(t);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.serialize(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[e.APIServerURLEnvironmentVariable],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({kv:Object.freeze({get:async e=>{let t=await p.storage.kv[`: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.kv[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}},del:async e=>{let t=await p.storage.kv[`:key`].$delete({param:{key:e}});if(t.status!==204){let e=await t.json();throw Error(e.error)}}})}),g={with(e,t){let n={};for(let r of Object.keys(e)){let i=e[r];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`,supportsRequests:e.onRequest!==void 0,supportsCompletions:e.experimental_provideCompletions!==void 0,fetch:async n=>{let r=new URL(n.url);if(r.pathname.startsWith(`/_agent/`))switch(r.pathname){case`/_agent/send-messages`:return v(n,e);case`/_agent/completions`:return y(n,e);case`/_agent/health`:return new Response(`OK`,{status:200});default:return new Response(`Not found`,{status:404})}if(e.onRequest){let r;try{r=await e.onRequest(n)}catch(e){return new Response(JSON.stringify({error:t.default.inspect(e)}),{status:500})}if(r)return r}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,chat:r.chat})}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:
|
|
3
|
+
`)}})).pipeThrough(new TextEncoderStream),{headers:x})}async function y(e,n){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});if(!n.experimental_provideCompletions)return new Response(`Completions not supported`,{status:404});let r;try{r=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await n.experimental_provideCompletions({messages:r.messages,input:r.input,caret:r.caret,selection:r.selection})}catch(e){return new Response(JSON.stringify({error:t.default.inspect(e)}),{status:500})}let a=await i;return a instanceof ReadableStream?new Response(a.pipeThrough(new TransformStream({transform(e,t){t.enqueue(`data: ${JSON.stringify(e)}\n\n`)},flush(e){e.enqueue(`data: [DONE]
|
|
4
|
+
|
|
5
|
+
`)}})).pipeThrough(new TextEncoderStream),{headers:x}):new Response(JSON.stringify(a),{headers:{"content-type":`application/json`}})}function b(t,n){return t.headers.set(e.StreamResponseFormatHeader,n),t}const x={"content-type":`text/event-stream`,"cache-control":`no-cache, no-transform`,connection:`keep-alive`,"x-accel-buffering":`no`};var S={agent:_,chat:m,storage:h,tools:g};exports.agent=_,exports.chat=m,exports.default=S,exports.storage=h,exports.tools=g,exports.withResponseFormat=b;
|
package/dist/api/index.d.cts
CHANGED
|
@@ -104,6 +104,33 @@ interface SendMessagesOptions<MESSAGE extends UIMessage = UIMessage> {
|
|
|
104
104
|
*/
|
|
105
105
|
readonly abortSignal?: AbortSignal;
|
|
106
106
|
}
|
|
107
|
+
interface ExperimentalProvideCompletionsOptions<MESSAGE extends UIMessage = UIMessage> {
|
|
108
|
+
/**
|
|
109
|
+
* The chat for the completions.
|
|
110
|
+
* Omitted if there is no chat.
|
|
111
|
+
*/
|
|
112
|
+
readonly chat?: Chat;
|
|
113
|
+
/**
|
|
114
|
+
* Messages that are part of the current chat.
|
|
115
|
+
* Omitted if there is no chat.
|
|
116
|
+
*/
|
|
117
|
+
readonly messages?: MESSAGE[];
|
|
118
|
+
readonly input: string;
|
|
119
|
+
readonly caret: number;
|
|
120
|
+
readonly selection?: [number, number];
|
|
121
|
+
readonly abortSignal?: AbortSignal;
|
|
122
|
+
}
|
|
123
|
+
type ExperimentalCompletion = {
|
|
124
|
+
text: string;
|
|
125
|
+
replace?: [number, number];
|
|
126
|
+
} | {
|
|
127
|
+
id: string;
|
|
128
|
+
label: string;
|
|
129
|
+
detail?: string;
|
|
130
|
+
insertText?: string;
|
|
131
|
+
replace?: [number, number];
|
|
132
|
+
};
|
|
133
|
+
type ExperimentalProvideCompletionsResponse = ReadableStream<ExperimentalCompletion> | Promise<ReadableStream<ExperimentalCompletion>> | Promise<ExperimentalCompletion> | ExperimentalCompletion;
|
|
107
134
|
interface Agent<MESSAGE extends UIMessage = UIMessage> {
|
|
108
135
|
/**
|
|
109
136
|
* sendMessages is called when the agent is streaming chat messages.
|
|
@@ -121,6 +148,16 @@ interface Agent<MESSAGE extends UIMessage = UIMessage> {
|
|
|
121
148
|
* respond with a 404.
|
|
122
149
|
*/
|
|
123
150
|
onRequest?(request: Request): Promise<Response | void>;
|
|
151
|
+
/**
|
|
152
|
+
* experimental_provideCompletions is called when the user is typing in the chat input.
|
|
153
|
+
* This is used to provide completions to the user.
|
|
154
|
+
*
|
|
155
|
+
* *NOTE*: This *only* works in the browser at the moment.
|
|
156
|
+
*
|
|
157
|
+
* @param options The options for the completions.
|
|
158
|
+
* @returns A stream of completions, or a single completion.
|
|
159
|
+
*/
|
|
160
|
+
experimental_provideCompletions?(options: ExperimentalProvideCompletionsOptions<MESSAGE>): ExperimentalProvideCompletionsResponse;
|
|
124
161
|
}
|
|
125
162
|
/**
|
|
126
163
|
* AgentDefaultExport is the expected default export of a compiled agent.
|
|
@@ -133,6 +170,7 @@ type AgentDefaultExport = {
|
|
|
133
170
|
readonly version: "v0";
|
|
134
171
|
fetch: (request: Request) => Promise<Response> | Response;
|
|
135
172
|
readonly supportsRequests?: boolean;
|
|
173
|
+
readonly supportsCompletions?: boolean;
|
|
136
174
|
};
|
|
137
175
|
/**
|
|
138
176
|
* agent is the main function to create a Blink agent.
|
|
@@ -143,6 +181,17 @@ type AgentDefaultExport = {
|
|
|
143
181
|
declare function agent<MESSAGE extends UIMessage = UIMessage>(agent: Agent<MESSAGE>): AgentDefaultExport;
|
|
144
182
|
type StreamResponseFormat = "ui-message" | "openai-chat" | "openai-response" | "anthropic" | "google" | "xai";
|
|
145
183
|
declare function withResponseFormat(response: Response, format: StreamResponseFormat): Response;
|
|
184
|
+
/**
|
|
185
|
+
* model returns an AI SDK model that is authenticated through Blink.
|
|
186
|
+
*
|
|
187
|
+
* Blink's AI Gateway uses Vercel's AI Gateway under-the-hood, and passes
|
|
188
|
+
* through billing cost without any upcharge.
|
|
189
|
+
*
|
|
190
|
+
* Feel free to use any provide you'd like.
|
|
191
|
+
*
|
|
192
|
+
* @param modelName - See: https://vercel.com/ai-gateway/models
|
|
193
|
+
* @returns An AI SDK model that is authenticated through Blink.
|
|
194
|
+
*/
|
|
146
195
|
declare const _default: {
|
|
147
196
|
agent: typeof agent;
|
|
148
197
|
chat: Readonly<{
|
|
@@ -166,4 +215,4 @@ declare const _default: {
|
|
|
166
215
|
};
|
|
167
216
|
};
|
|
168
217
|
//#endregion
|
|
169
|
-
export { Agent, AgentDefaultExport, Chat, ContextFromTools, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, ToolWith, agent, chat, _default as default, storage, tools, withResponseFormat };
|
|
218
|
+
export { Agent, AgentDefaultExport, Chat, ContextFromTools, ExperimentalCompletion, ExperimentalProvideCompletionsOptions, ExperimentalProvideCompletionsResponse, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, ToolWith, agent, chat, _default as default, storage, tools, withResponseFormat };
|
package/dist/api/index.d.ts
CHANGED
|
@@ -104,6 +104,33 @@ interface SendMessagesOptions<MESSAGE extends UIMessage = UIMessage> {
|
|
|
104
104
|
*/
|
|
105
105
|
readonly abortSignal?: AbortSignal;
|
|
106
106
|
}
|
|
107
|
+
interface ExperimentalProvideCompletionsOptions<MESSAGE extends UIMessage = UIMessage> {
|
|
108
|
+
/**
|
|
109
|
+
* The chat for the completions.
|
|
110
|
+
* Omitted if there is no chat.
|
|
111
|
+
*/
|
|
112
|
+
readonly chat?: Chat;
|
|
113
|
+
/**
|
|
114
|
+
* Messages that are part of the current chat.
|
|
115
|
+
* Omitted if there is no chat.
|
|
116
|
+
*/
|
|
117
|
+
readonly messages?: MESSAGE[];
|
|
118
|
+
readonly input: string;
|
|
119
|
+
readonly caret: number;
|
|
120
|
+
readonly selection?: [number, number];
|
|
121
|
+
readonly abortSignal?: AbortSignal;
|
|
122
|
+
}
|
|
123
|
+
type ExperimentalCompletion = {
|
|
124
|
+
text: string;
|
|
125
|
+
replace?: [number, number];
|
|
126
|
+
} | {
|
|
127
|
+
id: string;
|
|
128
|
+
label: string;
|
|
129
|
+
detail?: string;
|
|
130
|
+
insertText?: string;
|
|
131
|
+
replace?: [number, number];
|
|
132
|
+
};
|
|
133
|
+
type ExperimentalProvideCompletionsResponse = ReadableStream<ExperimentalCompletion> | Promise<ReadableStream<ExperimentalCompletion>> | Promise<ExperimentalCompletion> | ExperimentalCompletion;
|
|
107
134
|
interface Agent<MESSAGE extends UIMessage = UIMessage> {
|
|
108
135
|
/**
|
|
109
136
|
* sendMessages is called when the agent is streaming chat messages.
|
|
@@ -121,6 +148,16 @@ interface Agent<MESSAGE extends UIMessage = UIMessage> {
|
|
|
121
148
|
* respond with a 404.
|
|
122
149
|
*/
|
|
123
150
|
onRequest?(request: Request): Promise<Response | void>;
|
|
151
|
+
/**
|
|
152
|
+
* experimental_provideCompletions is called when the user is typing in the chat input.
|
|
153
|
+
* This is used to provide completions to the user.
|
|
154
|
+
*
|
|
155
|
+
* *NOTE*: This *only* works in the browser at the moment.
|
|
156
|
+
*
|
|
157
|
+
* @param options The options for the completions.
|
|
158
|
+
* @returns A stream of completions, or a single completion.
|
|
159
|
+
*/
|
|
160
|
+
experimental_provideCompletions?(options: ExperimentalProvideCompletionsOptions<MESSAGE>): ExperimentalProvideCompletionsResponse;
|
|
124
161
|
}
|
|
125
162
|
/**
|
|
126
163
|
* AgentDefaultExport is the expected default export of a compiled agent.
|
|
@@ -133,6 +170,7 @@ type AgentDefaultExport = {
|
|
|
133
170
|
readonly version: "v0";
|
|
134
171
|
fetch: (request: Request) => Promise<Response> | Response;
|
|
135
172
|
readonly supportsRequests?: boolean;
|
|
173
|
+
readonly supportsCompletions?: boolean;
|
|
136
174
|
};
|
|
137
175
|
/**
|
|
138
176
|
* agent is the main function to create a Blink agent.
|
|
@@ -143,6 +181,17 @@ type AgentDefaultExport = {
|
|
|
143
181
|
declare function agent<MESSAGE extends UIMessage = UIMessage>(agent: Agent<MESSAGE>): AgentDefaultExport;
|
|
144
182
|
type StreamResponseFormat = "ui-message" | "openai-chat" | "openai-response" | "anthropic" | "google" | "xai";
|
|
145
183
|
declare function withResponseFormat(response: Response, format: StreamResponseFormat): Response;
|
|
184
|
+
/**
|
|
185
|
+
* model returns an AI SDK model that is authenticated through Blink.
|
|
186
|
+
*
|
|
187
|
+
* Blink's AI Gateway uses Vercel's AI Gateway under-the-hood, and passes
|
|
188
|
+
* through billing cost without any upcharge.
|
|
189
|
+
*
|
|
190
|
+
* Feel free to use any provide you'd like.
|
|
191
|
+
*
|
|
192
|
+
* @param modelName - See: https://vercel.com/ai-gateway/models
|
|
193
|
+
* @returns An AI SDK model that is authenticated through Blink.
|
|
194
|
+
*/
|
|
146
195
|
declare const _default: {
|
|
147
196
|
agent: typeof agent;
|
|
148
197
|
chat: Readonly<{
|
|
@@ -166,4 +215,4 @@ declare const _default: {
|
|
|
166
215
|
};
|
|
167
216
|
};
|
|
168
217
|
//#endregion
|
|
169
|
-
export { Agent, AgentDefaultExport, Chat, ContextFromTools, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, ToolWith, agent, chat, _default as default, storage, tools, withResponseFormat };
|
|
218
|
+
export { Agent, AgentDefaultExport, Chat, ContextFromTools, ExperimentalCompletion, ExperimentalProvideCompletionsOptions, ExperimentalProvideCompletionsResponse, Message, MessageOptions, SendMessagesOptions, SendMessagesResponse, StreamResponseFormat, ToolWith, agent, chat, _default as default, storage, tools, withResponseFormat };
|
package/dist/api/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import{APIServerURLEnvironmentVariable as e,StreamResponseFormatHeader as t,serialize as n}from"../cookie-CyZUsiHM.js";import r from"util";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(e,t)=>{if(e){if(e.query&&(this.queryParams=o(e.query)),e.form){let t=new FormData;for(let[n,r]of Object.entries(e.form))if(Array.isArray(r))for(let e of r)t.append(n,e);else t.append(n,r);this.rBody=t}e.json&&(this.rBody=JSON.stringify(e.json),this.cType=`application/json`),e.param&&(this.pathParams=e.param)}let r=this.method.toUpperCase(),i={...e?.header,...typeof t?.headers==`function`?await t.headers():t?.headers};if(e?.cookie){let t=[];for(let[r,i]of Object.entries(e.cookie))t.push(n(r,i,{path:`/`}));i.Cookie=t.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(t?.fetch||fetch)(l,{body:u?this.rBody:void 0,method:r,headers:s,...t?.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[e],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({kv:Object.freeze({get:async e=>{let t=await h.storage.kv[`: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.kv[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}},del:async e=>{let t=await h.storage.kv[`:key`].$delete({param:{key:e}});if(t.status!==204){let e=await t.json();throw Error(e.error)}}})}),v={with(e,t){let n={};for(let r of Object.keys(e)){let i=e[r];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`,supportsRequests:e.onRequest!==void 0,fetch:async t=>{let n=new URL(t.url);if(n.pathname.startsWith(`/_agent/`))switch(n.pathname){case`/_agent/send-messages`:return b(t,e);case`/_agent/health`:return new Response(`OK`,{status:200});default:return new Response(`Not found`,{status:404})}if(e.onRequest){let n;try{n=await e.onRequest(t)}catch(e){return new Response(JSON.stringify({error:r.inspect(e)}),{status:500})}if(n)return n}return new Response(`Not found`,{status:404})}}}async function b(e,t){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});let n;try{n=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await t.sendMessages({messages:n.messages,chat:n.chat})}catch(e){return new Response(JSON.stringify({error:r.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{APIServerURLEnvironmentVariable as e,StreamResponseFormatHeader as t,serialize as n}from"../cookie-CyZUsiHM.js";import r from"util";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(e,t)=>{if(e){if(e.query&&(this.queryParams=o(e.query)),e.form){let t=new FormData;for(let[n,r]of Object.entries(e.form))if(Array.isArray(r))for(let e of r)t.append(n,e);else t.append(n,r);this.rBody=t}e.json&&(this.rBody=JSON.stringify(e.json),this.cType=`application/json`),e.param&&(this.pathParams=e.param)}let r=this.method.toUpperCase(),i={...e?.header,...typeof t?.headers==`function`?await t.headers():t?.headers};if(e?.cookie){let t=[];for(let[r,i]of Object.entries(e.cookie))t.push(n(r,i,{path:`/`}));i.Cookie=t.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(t?.fetch||fetch)(l,{body:u?this.rBody:void 0,method:r,headers:s,...t?.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[e],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({kv:Object.freeze({get:async e=>{let t=await h.storage.kv[`: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.kv[`:key`].$post({param:{key:e},json:{value:t}});if(n.status!==204){let e=await n.json();throw Error(e.error)}},del:async e=>{let t=await h.storage.kv[`:key`].$delete({param:{key:e}});if(t.status!==204){let e=await t.json();throw Error(e.error)}}})}),v={with(e,t){let n={};for(let r of Object.keys(e)){let i=e[r];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`,supportsRequests:e.onRequest!==void 0,supportsCompletions:e.experimental_provideCompletions!==void 0,fetch:async t=>{let n=new URL(t.url);if(n.pathname.startsWith(`/_agent/`))switch(n.pathname){case`/_agent/send-messages`:return b(t,e);case`/_agent/completions`:return x(t,e);case`/_agent/health`:return new Response(`OK`,{status:200});default:return new Response(`Not found`,{status:404})}if(e.onRequest){let n;try{n=await e.onRequest(t)}catch(e){return new Response(JSON.stringify({error:r.inspect(e)}),{status:500})}if(n)return n}return new Response(`Not found`,{status:404})}}}async function b(e,t){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});let n;try{n=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await t.sendMessages({messages:n.messages,chat:n.chat})}catch(e){return new Response(JSON.stringify({error:r.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:
|
|
3
|
+
`)}})).pipeThrough(new TextEncoderStream),{headers:C})}async function x(e,t){if(e.method!==`POST`)return new Response(`Method not allowed`,{status:405});if(!t.experimental_provideCompletions)return new Response(`Completions not supported`,{status:404});let n;try{n=await e.json()}catch{return new Response(`Invalid request`,{status:400})}let i;try{i=await t.experimental_provideCompletions({messages:n.messages,input:n.input,caret:n.caret,selection:n.selection})}catch(e){return new Response(JSON.stringify({error:r.inspect(e)}),{status:500})}let a=await i;return a instanceof ReadableStream?new Response(a.pipeThrough(new TransformStream({transform(e,t){t.enqueue(`data: ${JSON.stringify(e)}\n\n`)},flush(e){e.enqueue(`data: [DONE]
|
|
4
|
+
|
|
5
|
+
`)}})).pipeThrough(new TextEncoderStream),{headers:C}):new Response(JSON.stringify(a),{headers:{"content-type":`application/json`}})}function S(e,n){return e.headers.set(t,n),e}const C={"content-type":`text/event-stream`,"cache-control":`no-cache, no-transform`,connection:`keep-alive`,"x-accel-buffering":`no`};var w={agent:y,chat:g,storage:_,tools:v};export{y as agent,g as chat,w as default,_ as storage,v as tools,S as withResponseFormat};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createRequire as e}from"node:module";var t=Object.create,n=Object.defineProperty,r=Object.getOwnPropertyDescriptor,i=Object.getOwnPropertyNames,a=Object.getPrototypeOf,o=Object.prototype.hasOwnProperty,s=(e,t)=>()=>(e&&(t=e(e=0)),t),c=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),l=(e,t)=>{for(var r in t)n(e,r,{get:t[r],enumerable:!0})},u=(e,t,a,s)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var c=i(t),l=0,u=c.length,d;l<u;l++)d=c[l],!o.call(e,d)&&d!==a&&n(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(s=r(t,d))||s.enumerable});return e},d=(e,t,n)=>(u(e,t,`default`),n&&u(n,t,`default`)),f=(e,r,i)=>(i=e==null?{}:t(a(e)),u(r||!e||!e.__esModule?n(i,`default`,{value:e,enumerable:!0}):i,e)),p=e=>u(n({},`__esModule`,{value:!0}),e),m=e=>t=>f(t.default,e),h=e(import.meta.url);export{c as __commonJSMin,s as __esmMin,l as __export,d as __reExport,h as __require,p as __toCommonJS,m as __toDynamicImportESM,f as __toESM};
|