import * as hono_utils_http_status0 from "hono/utils/http-status"; import * as hono_hono_base0 from "hono/hono-base"; import { InferToolInput, InferToolOutput, InferUIMessageChunk, StreamTextResult, Tool, ToolSet, UIMessage } from "ai"; import * as http from "http"; //#region src/agent/internal/types.d.ts type Promisable = T | Promise; //#endregion //#region src/agent/types.d.ts /** * ID is a UUID. * This is a special type to ensure that keys are not passed as IDs. */ type ID$1 = `${string}-${string}-${string}-${string}-${string}`; interface NewMessage { /** * ID must be a UUID. If not set, a random UUID will be generated. * * String is allowed for nice types - users may define messages as UIMessage, * which this type fulfills. */ readonly id?: ID$1 | string; /** * createdAt is the timestamp of the message. * * If not set, the message will be created at the current time. */ readonly createdAt?: Date; readonly role: MESSAGE["role"]; readonly parts: MESSAGE["parts"]; readonly metadata?: MESSAGE["metadata"]; } interface Chat { readonly id: ID$1; readonly createdAt: string; } interface UpsertedChat extends Chat { readonly created: boolean; } type SendBehavior = "enqueue" | "interrupt" | "append"; interface SendOptions { /** * behavior of the chat when sending these messages. * * - "enqueue" will add messages to the chat and start the chat eventually. * - "interrupt" will interrupt the chat if running and send messages. * - "append" will add messages to the chat. */ readonly behavior?: SendBehavior; /** * upsert will replace messages in the chat if they already exist. */ readonly upsert?: boolean; } interface ChatEvent { readonly id: ID$1; readonly messages: MESSAGE[]; readonly abortSignal?: AbortSignal; } type ChatResponse = { toUIMessageStream: StreamTextResult["toUIMessageStream"]; } | Response | ReadableStream> | void; type ChatHandler = (event: ChatEvent) => Promisable>; interface AgentChat { /** * Upsert a chat by a stable key. * This will create a new chat if it doesn't exist. * * @param key the key of the chat. */ upsert(key: JSONValue): Promise; /** * Get a chat by ID. * * @param id the ID of the chat. * @returns the chat. */ get(id: ID$1): Promise; /** * Get messages from a chat. * * @param id the ID of the chat. * @returns the messages in the chat. */ getMessages(id: ID$1): Promise; /** * Send messages to a chat. * * @param id the ID of the chat. * @param messages the messages to send. * @param options the options for the messages. */ sendMessages(id: ID$1, messages: NewMessage[], options?: SendOptions): Promise; /** * Delete messages from a chat. * * @param id the ID of the chat. * @param messages the messages to delete. */ deleteMessages(id: ID$1, messages: string[]): Promise; /** * Start a chat. * * @param id the ID of the chat. */ start(id: ID$1): Promise; /** * Stop a chat. * * @param id the ID of the chat. */ stop(id: ID$1): Promise; /** * Delete a chat. * * @param id the ID of the chat. */ delete(id: ID$1): Promise; } interface AgentStore { /** * get retrieves a value from storage. * * @param key the key of the value. * @returns the value. */ get(key: string): Promise; /** * set a value. * * @param key the key of the value. * @param value the value to set. */ set(key: string, value: string, options?: { /** * ttl is the number of seconds to keep the value. * * If not set, the value will never expire. */ ttl?: number; }): Promise; /** * delete a value. * * @param key the key of the value. */ delete(key: string): Promise; /** * list all values by prefix. * * @param prefix the prefix of the keys. * @returns the values. */ list(prefix?: string, options?: { /** * limit is the maximum number of values to return. * * Defaults to 100. Limit is 1000. */ limit?: number; /** * cursor is the cursor to start from. * * If not set, the list will start from the beginning. */ cursor?: string; }): Promise<{ entries: Array<{ key: string; ttl?: number; }>; cursor?: string; }>; } interface AgentOtel { /** * traces forwards OpenTelemetry traces. */ traces: (request: Request) => Promise; } /** A JSON value can be a string, number, boolean, object, array, or null. JSON values can be serialized and deserialized by the JSON.stringify and JSON.parse methods. */ type JSONValue = null | string | number | boolean | JSONObject | JSONArray; type JSONObject = { [key: string]: JSONValue; }; type JSONArray = Array; //#endregion //#region src/agent/ui.d.ts type UIOptionIcon = `lucide:${string}` | `simple-icons:${string}`; type UIOptionSelectValue = { readonly id: ID; readonly label: string; /** * description will provide additional context to the user in the UI. */ readonly description?: string | Array<{ readonly text: string; readonly color?: "primary" | "muted" | "warning"; }>; /** * icon is a slug of a Lucide or SimpleIcons icon. * This will only be rendered in a web-based UI. * * Find icons: * - https://simpleicons.org/ * - https://lucide.dev/icons/ */ readonly icon?: UIOptionIcon; }; type UIOptionSelect = { readonly type: "select"; /** * label indicates the purpose of the option. * If omitted, it will not be displayed in the UI. */ readonly label?: string; /** * icon is a slug of a Lucide or SimpleIcons icon. * This will only be rendered in a web-based UI. * * Find icons: * - https://simpleicons.org/ * - https://lucide.dev/icons/ */ readonly icon?: UIOptionIcon; /** * defaultValue is the default value for the option. * If omitted, the option will not be selected by default. */ readonly defaultValue: Values[number]["id"]; readonly values: Values; }; type UIOptions = Record; type WithUIOptions = MESSAGE & { readonly role: "user"; readonly metadata: MESSAGE["metadata"] & { readonly options?: OPTIONS; }; }; type ExtractUIOptions = M extends WithUIOptions ? O : UIOptions; type UIOptionsSchema = { [K in keyof OPTIONS]: UIOptionSelect>> }; interface UIEvent { readonly selectedOptions?: ExtractUIOptions; } type UIHandler = (event: UIEvent) => Promisable> | void>; /** * lastUIOptions finds the last user message with options. * Options are stored in message metadata to preserve the history * of changing options. * * @param messages - The messages to search. * @returns The last user message with options, or undefined if no such message exists. */ declare function lastUIOptions(messages: MESSAGE[]): ExtractUIOptions | undefined; //#endregion //#region src/agent/agent.d.ts interface ServeOptions { /** * apiUrl is the URL of the Blink API server which the agent * uses to create chats, send messages, and manage storage. * * Defaults `BLINK_API_URL`. If not set, the agent will warn * and throw an error if a request is attempted to the API. */ apiUrl?: string; /** * host is the host to serve the agent on. * * Defaults to `HOST` or `127.0.0.1. */ host?: string; /** * port is the port to serve the agent on. * * Defaults to `PORT` or `3000`. */ port?: number; } type RequestHandler = (request: Request) => Promisable; type ErrorHandler = (error: Error) => Promisable; type EventHandlerMap = { chat: ChatHandler; ui: UIHandler; request: RequestHandler; error: ErrorHandler; }; type EventName = keyof EventHandlerMap; type HandlerForEvent> = EventHandlerMap[E]; type Listeners = { [E in EventName]: Array> }; declare class Agent { private client; private listeners; constructor(); readonly chat: AgentChat; readonly store: AgentStore; on>(event: E, handler: HandlerForEvent): Agent; /** * serve starts the agent as an HTTP server. * @param options * @returns */ serve(options?: ServeOptions): http.Server; /** * fetch fetches from the agent. * @param request * @returns */ fetch(request: Request): Response | Promise; } /** * agent constructs a new agent. * * @deprecated Use `new Agent()` instead. * @param options * @returns */ declare function agent(): Agent; declare const api: hono_hono_base0.HonoBase<{ Bindings: { listeners: Listeners; }; }, { "/_agent/chat": { $post: { input: { json: { messages: UIMessage[]; id: ID$1; }; }; output: {}; outputFormat: string; status: hono_utils_http_status0.StatusCode; }; }; } & { "/_agent/capabilities": { $get: { input: {}; output: { ui: boolean; chat: boolean; request: boolean; error: boolean; }; outputFormat: "json"; status: 200; }; }; } & { "/_agent/health": { $get: { input: {}; output: "OK"; outputFormat: "body"; status: 200; }; }; } & { "/_agent/ui": { $get: { input: {}; output: { error: string; }; outputFormat: "json"; status: 400; } | { input: {}; output: { [x: string]: { readonly type: "select"; readonly label?: string | undefined; readonly icon?: (`lucide:${string}` | `simple-icons:${string}`) | undefined; readonly defaultValue: string; readonly values: { readonly id: string; readonly label: string; readonly description?: string | { readonly text: string; readonly color?: "primary" | "muted" | "warning" | undefined; }[] | undefined; readonly icon?: (`lucide:${string}` | `simple-icons:${string}`) | undefined; }[]; }; }; outputFormat: "json"; status: 200; } | { input: {}; output: { error: string; }; outputFormat: "json"; status: 404; }; }; } & { "/_agent/flush-otel": { $post: { input: {}; output: null; outputFormat: "body"; status: 204; }; }; } & { "*": { $all: { input: {}; output: {}; outputFormat: string; status: hono_utils_http_status0.StatusCode; }; }; }, "/">; /** * waitUntil waits until the promise is resolved. * This is useful for responding quickly in webhooks, but * allow processing to continue in the background. */ declare function waitUntil(promise: Promise): void; //#endregion //#region src/agent/tools.d.ts /** * ToolWithContext is a tool that supports the "withContext" method. * * @param CONTEXT The context type. * @param TOOL The tool type. * @returns The tool with the given context. */ type ToolWithContext = TOOL & { withContext(context: CONTEXT): TOOL; }; /** * ToolWithApproval is a tool that supports the "autoApprove" method. * * @param TOOL The tool type. * @returns The tool with the given approval. */ type ToolWithApproval = Tool & { /** * autoApprove is a function that can be used to automatically approve * an approval tool call based on the input. * * @param input The input to the tool. * @returns Whether the tool call should be approved. */ autoApprove?: (input: INPUT) => Promise | boolean; }; type ToolSetWithApproval = { [K in keyof TOOLS]: ToolWithApproval, InferToolOutput> }; /** * toolWithApproval is a helper for inferring the execute and autoApprove * arguments of a tool. * * @param tool The tool to wrap. * @returns The wrapped tool. */ declare function toolWithApproval(tool: ToolWithApproval): ToolWithApproval; type ToolSetWithPrefix = { [K in keyof TOOLS as `${PREFIX}${K & string}`]: K extends string ? TOOLS[K] : never }; /** * Tools are helpers for managing tools. */ declare const tools: Readonly<{ /** * withContext adds context to a set of tools that supports the "withContext" method. * * @param context * @param tools * @returns */ withContext(tools: TOOLS, context: ContextFromTools): { [K in keyof TOOLS]: Tool }; /** * @internal * @deprecated Use withContext instead - it's the same thing. */ with(tools: TOOLS, context: ContextFromTools): { [K in keyof TOOLS]: Tool }; /** * withApproval ensures a set of tools need explicit user approval * before they are executed. * * This works by replacing the execution of all provided tools with * special output that interfaces must handle. * * On approval, the tool will be executed with the verbatim input. * * @returns Tools that should be sent in `streamText`. */ withApproval, MESSAGE extends UIMessage>(options: { messages: MESSAGE[]; tools: TOOLS; abortSignal?: AbortSignal; }): Promise; /** * prefix adds a prefix to all the tools in a tool set. * * @param tools The tool set to prefix. * @param prefix The prefix to add to the tools. * @returns The prefixed tool set. */ prefix(tools: TOOLS, prefix: PREFIX): ToolSetWithPrefix; }>; type ToolsWithContext = Record; type ContextFromTools = TOOLS[keyof TOOLS] extends { withContext(context: infer C): any; } ? C : never; /** * ToolApprovalOutput is the output of a tool that requires approval. * * This should be consumed by the UI to display an approval prompt. */ interface ToolApprovalOutput { type: "tool-approval"; outcome: "pending" | "approved" | "rejected"; reason?: string; } /** * isToolApprovalOutput checks if an output is a tool approval output. */ declare function isToolApprovalOutput(output: unknown): output is ToolApprovalOutput; //#endregion //#region src/agent/index.browser.d.ts type StreamResponseFormat = "ui-message" | "openai-chat" | "openai-response" | "anthropic" | "google" | "xai"; /** * StreamResponseFormatHeader indicates to a client the stream response * format that the agent is using. */ declare const StreamResponseFormatHeader = "x-blink-stream-response-format"; declare function withResponseFormat(response: Response, format: StreamResponseFormat): Response; //#endregion export { Agent, AgentChat, AgentOtel, AgentStore, Chat, ChatEvent, ChatHandler, ChatResponse, ContextFromTools, ErrorHandler, ExtractUIOptions, ID$1 as ID, NewMessage, RequestHandler, SendBehavior, SendOptions, ServeOptions, StreamResponseFormat, StreamResponseFormatHeader, ToolApprovalOutput, ToolSetWithApproval, ToolWithApproval, ToolWithContext, UIEvent, UIHandler, UIOptionSelect, UIOptionSelectValue, UIOptions, UIOptionsSchema, UpsertedChat, WithUIOptions, agent, api, isToolApprovalOutput, lastUIOptions, toolWithApproval, tools, waitUntil, withResponseFormat };