cencori 1.10.0 → 1.11.1

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/README.md CHANGED
@@ -124,6 +124,39 @@ await run(extractor, 'Trip to Lagos for 3 days', { memory: { userId: 'user_123'
124
124
 
125
125
  Tool parameters accept zod 3 or 4, any [Standard Schema](https://standardschema.dev) library, or a plain JSON Schema object.
126
126
 
127
+ ## Agent UI
128
+
129
+ A chat for your agent where it can answer with UI: charts, tables, stats, lists, buttons and forms, generated on the fly. Works with any model in the catalog.
130
+
131
+ ```ts
132
+ // app/api/chat/route.ts: your tools run on your server
133
+ import { Agent, createAgentChatHandler, tool } from 'cencori/agents';
134
+ import { z } from 'zod';
135
+
136
+ const getSales = tool({
137
+ name: 'get_sales',
138
+ description: 'Monthly revenue by product',
139
+ parameters: z.object({}),
140
+ execute: async () => db.sales.monthly(),
141
+ });
142
+
143
+ const analyst = new Agent({ name: 'Analyst', instructions: 'Answer with charts and tables when it helps.', tools: [getSales], widgets: true });
144
+ export const POST = createAgentChatHandler(analyst);
145
+ ```
146
+
147
+ ```tsx
148
+ // In your page
149
+ import { AgentChat } from 'cencori/react';
150
+
151
+ <AgentChat api="/api/chat" greeting="Ask about your sales" prompts={['Revenue by product', 'Top customers']} />
152
+ ```
153
+
154
+ - `widgets: true` gives the model the built-in `render_ui` tool. Cencori checks every widget before it's shown.
155
+ - A tool can also return `{ widget }` to show a design you control, filled with real data.
156
+ - Buttons and forms send their action back to the agent (or to `onAction` with `handler: 'client'`).
157
+ - Approvals (`needsApproval`) show an approval card; the paused run is signed so the browser can't change it.
158
+ - Theme with `theme={{ colorScheme, accent, radius }}` or CSS variables. Build your own layout with `useAgentChat()`, `<Transcript>`, `<Composer>` and `<Widget>`.
159
+
127
160
  ## Embedded Agents
128
161
 
129
162
  ```typescript
@@ -1,3 +1,6 @@
1
+ import { W as WidgetNode } from '../protocol-Dq7rZbRX.mjs';
2
+ export { b as AgentChatApprovalItem, c as AgentChatDecision, A as AgentChatEvent, a as AgentChatRequest, B as BadgeNode, r as ButtonNode, C as CardNode, o as ChartNode, n as ChartSeries, w as CheckboxNode, k as ColNode, D as DividerNode, F as FormNode, I as ImageNode, s as InputNode, L as ListViewItem, j as ListViewNode, M as MarkdownNode, y as RenderedWidget, R as RowNode, u as SelectNode, t as SelectOption, S as SpacerNode, m as StatNode, p as TableColumn, q as TableNode, T as TextNode, l as TitleNode, z as WIDGET_LIMITS, d as WIDGET_TYPES, h as WidgetAction, e as WidgetColor, f as WidgetFormat, g as WidgetGap, x as WidgetType, E as WidgetValidation, G as describeWidgetSpec, i as isWidgetResult, v as validateWidget } from '../protocol-Dq7rZbRX.mjs';
3
+
1
4
  /** What tools, guardrails and dynamic instructions receive during a run. */
2
5
  interface RunContext<Ctx = unknown> {
3
6
  /** Your own value from run(..., { context }): a user, a db handle, anything. Never sent to Cencori. */
@@ -179,6 +182,12 @@ type RunItem = {
179
182
  type: 'handoff';
180
183
  from: string;
181
184
  to: string;
185
+ } | {
186
+ type: 'widget';
187
+ agentName: string;
188
+ callId: string;
189
+ widgetId: string;
190
+ widget: WidgetNode;
182
191
  };
183
192
  interface Usage {
184
193
  inputTokens: number;
@@ -208,6 +217,21 @@ type RunStreamEvent = {
208
217
  type: 'handoff';
209
218
  from: string;
210
219
  to: string;
220
+ }
221
+ /** UI to show: from the model (render_ui) or from a tool that returned { widget }. */
222
+ | {
223
+ type: 'widget';
224
+ callId: string;
225
+ widgetId: string;
226
+ widget: WidgetNode;
227
+ agentName: string;
228
+ }
229
+ /** A widget the model is still writing (render_ui): draw it, then replace it with the 'widget' for the same callId. */
230
+ | {
231
+ type: 'widget_partial';
232
+ callId: string;
233
+ widget: WidgetNode;
234
+ agentName: string;
211
235
  } | {
212
236
  type: 'interruption';
213
237
  interruptions: ToolApprovalItem[];
@@ -353,6 +377,13 @@ interface AgentOptions<Ctx = unknown, Out = string> {
353
377
  modelSettings?: ModelSettings;
354
378
  inputGuardrails?: InputGuardrail<Ctx>[];
355
379
  outputGuardrails?: OutputGuardrail<Ctx>[];
380
+ /**
381
+ * Let the agent answer with UI: it gets the built-in render_ui tool and can
382
+ * show cards, tables, charts, stats, buttons and forms in a chat
383
+ * (cencori/react's <AgentChat> renders them). Tools can also return
384
+ * `{ widget }` whether or not this is on.
385
+ */
386
+ widgets?: boolean;
356
387
  }
357
388
  /** The model an agent uses when none is given: GPT OSS 120B on Cerebras, chosen for speed. */
358
389
  declare const DEFAULT_MODEL = "gpt-oss-120b";
@@ -380,6 +411,7 @@ declare class Agent<Ctx = unknown, Out = string> {
380
411
  readonly modelSettings: ModelSettings;
381
412
  readonly inputGuardrails: InputGuardrail<Ctx>[];
382
413
  readonly outputGuardrails: OutputGuardrail<Ctx>[];
414
+ readonly widgets: boolean;
383
415
  constructor(options: AgentOptions<Ctx, Out>);
384
416
  /** A copy with some options changed. */
385
417
  clone(overrides: Partial<AgentOptions<Ctx, Out>>): Agent<Ctx, Out>;
@@ -430,4 +462,43 @@ declare class CencoriRunError extends AgentsError {
430
462
  });
431
463
  }
432
464
 
433
- export { Agent, type AgentOptions, AgentsError, CencoriRunError, DEFAULT_MODEL, type FunctionTool, type GuardrailResult, GuardrailTripwireTriggered, type InputGuardrail, type JsonSchema, MaxTurnsExceededError, type ModelSettings, type NeedsApproval, type OutputGuardrail, type RunContext, type RunItem, type RunOptions, type RunResult, RunState, type RunStreamEvent, type SchemaLike, SchemaValidationError, type StandardSchemaV1, type StreamedRunResult, type ToolApprovalItem, type ToolOptions, type Usage, run, tool };
465
+ /**
466
+ * Agent UI on your backend: serve an agent to <AgentChat> from your own route. Your tools run here,
467
+ * with your database and secrets; the browser only sees the conversation.
468
+ *
469
+ * ```ts
470
+ * // app/api/chat/route.ts
471
+ * import { Agent, createAgentChatHandler } from 'cencori/agents';
472
+ *
473
+ * const support = new Agent({ name: 'Support', instructions: '...', tools: [lookupOrder], widgets: true });
474
+ * export const POST = createAgentChatHandler(support, {
475
+ * context: async (req) => ({ userId: await userIdFrom(req) }),
476
+ * });
477
+ *
478
+ * // In the page: <AgentChat api="/api/chat" />
479
+ * ```
480
+ *
481
+ * Approvals: when a tool needs one, the chat gets the paused run's state,
482
+ * signed, and sends it back with the user's decisions. The signature (HMAC
483
+ * with your secret key, or `secret`) means a browser can't alter what runs.
484
+ */
485
+ interface AgentChatHandlerOptions<Ctx> extends Omit<RunOptions<Ctx>, 'context' | 'sessionId' | 'stream' | 'signal' | 'user'> {
486
+ /** Your context for this request (who the user is, their permissions...). */
487
+ context?: (req: Request) => Ctx | Promise<Ctx>;
488
+ /** A stable id for the end user, for Cencori's per-user analytics and limits. */
489
+ user?: (req: Request) => string | undefined | Promise<string | undefined>;
490
+ /**
491
+ * May this request continue this session? Check it belongs to the user.
492
+ * Without it, any session id the browser sends is continued.
493
+ */
494
+ authorizeSession?: (sessionId: string, req: Request) => boolean | Promise<boolean>;
495
+ /** Signs paused-run state. Default: CENCORI_AGENT_CHAT_SECRET, then your API key. */
496
+ secret?: string;
497
+ }
498
+ declare function createAgentChatHandler<Ctx>(agent: Agent<Ctx, any>, options?: AgentChatHandlerOptions<Ctx>): (req: Request) => Promise<Response>;
499
+ /** A streamed run as the agent chat event stream (for your own route handler). */
500
+ declare function toAgentChatStream(streamed: StreamedRunResult<unknown>, options: {
501
+ secret: string;
502
+ }): ReadableStream<Uint8Array>;
503
+
504
+ export { Agent, type AgentChatHandlerOptions, type AgentOptions, AgentsError, CencoriRunError, DEFAULT_MODEL, type FunctionTool, type GuardrailResult, GuardrailTripwireTriggered, type InputGuardrail, type JsonSchema, MaxTurnsExceededError, type ModelSettings, type NeedsApproval, type OutputGuardrail, type RunContext, type RunItem, type RunOptions, type RunResult, RunState, type RunStreamEvent, type SchemaLike, SchemaValidationError, type StandardSchemaV1, type StreamedRunResult, type ToolApprovalItem, type ToolOptions, type Usage, WidgetNode, createAgentChatHandler, run, toAgentChatStream, tool };
@@ -1,3 +1,6 @@
1
+ import { W as WidgetNode } from '../protocol-Dq7rZbRX.js';
2
+ export { b as AgentChatApprovalItem, c as AgentChatDecision, A as AgentChatEvent, a as AgentChatRequest, B as BadgeNode, r as ButtonNode, C as CardNode, o as ChartNode, n as ChartSeries, w as CheckboxNode, k as ColNode, D as DividerNode, F as FormNode, I as ImageNode, s as InputNode, L as ListViewItem, j as ListViewNode, M as MarkdownNode, y as RenderedWidget, R as RowNode, u as SelectNode, t as SelectOption, S as SpacerNode, m as StatNode, p as TableColumn, q as TableNode, T as TextNode, l as TitleNode, z as WIDGET_LIMITS, d as WIDGET_TYPES, h as WidgetAction, e as WidgetColor, f as WidgetFormat, g as WidgetGap, x as WidgetType, E as WidgetValidation, G as describeWidgetSpec, i as isWidgetResult, v as validateWidget } from '../protocol-Dq7rZbRX.js';
3
+
1
4
  /** What tools, guardrails and dynamic instructions receive during a run. */
2
5
  interface RunContext<Ctx = unknown> {
3
6
  /** Your own value from run(..., { context }): a user, a db handle, anything. Never sent to Cencori. */
@@ -179,6 +182,12 @@ type RunItem = {
179
182
  type: 'handoff';
180
183
  from: string;
181
184
  to: string;
185
+ } | {
186
+ type: 'widget';
187
+ agentName: string;
188
+ callId: string;
189
+ widgetId: string;
190
+ widget: WidgetNode;
182
191
  };
183
192
  interface Usage {
184
193
  inputTokens: number;
@@ -208,6 +217,21 @@ type RunStreamEvent = {
208
217
  type: 'handoff';
209
218
  from: string;
210
219
  to: string;
220
+ }
221
+ /** UI to show: from the model (render_ui) or from a tool that returned { widget }. */
222
+ | {
223
+ type: 'widget';
224
+ callId: string;
225
+ widgetId: string;
226
+ widget: WidgetNode;
227
+ agentName: string;
228
+ }
229
+ /** A widget the model is still writing (render_ui): draw it, then replace it with the 'widget' for the same callId. */
230
+ | {
231
+ type: 'widget_partial';
232
+ callId: string;
233
+ widget: WidgetNode;
234
+ agentName: string;
211
235
  } | {
212
236
  type: 'interruption';
213
237
  interruptions: ToolApprovalItem[];
@@ -353,6 +377,13 @@ interface AgentOptions<Ctx = unknown, Out = string> {
353
377
  modelSettings?: ModelSettings;
354
378
  inputGuardrails?: InputGuardrail<Ctx>[];
355
379
  outputGuardrails?: OutputGuardrail<Ctx>[];
380
+ /**
381
+ * Let the agent answer with UI: it gets the built-in render_ui tool and can
382
+ * show cards, tables, charts, stats, buttons and forms in a chat
383
+ * (cencori/react's <AgentChat> renders them). Tools can also return
384
+ * `{ widget }` whether or not this is on.
385
+ */
386
+ widgets?: boolean;
356
387
  }
357
388
  /** The model an agent uses when none is given: GPT OSS 120B on Cerebras, chosen for speed. */
358
389
  declare const DEFAULT_MODEL = "gpt-oss-120b";
@@ -380,6 +411,7 @@ declare class Agent<Ctx = unknown, Out = string> {
380
411
  readonly modelSettings: ModelSettings;
381
412
  readonly inputGuardrails: InputGuardrail<Ctx>[];
382
413
  readonly outputGuardrails: OutputGuardrail<Ctx>[];
414
+ readonly widgets: boolean;
383
415
  constructor(options: AgentOptions<Ctx, Out>);
384
416
  /** A copy with some options changed. */
385
417
  clone(overrides: Partial<AgentOptions<Ctx, Out>>): Agent<Ctx, Out>;
@@ -430,4 +462,43 @@ declare class CencoriRunError extends AgentsError {
430
462
  });
431
463
  }
432
464
 
433
- export { Agent, type AgentOptions, AgentsError, CencoriRunError, DEFAULT_MODEL, type FunctionTool, type GuardrailResult, GuardrailTripwireTriggered, type InputGuardrail, type JsonSchema, MaxTurnsExceededError, type ModelSettings, type NeedsApproval, type OutputGuardrail, type RunContext, type RunItem, type RunOptions, type RunResult, RunState, type RunStreamEvent, type SchemaLike, SchemaValidationError, type StandardSchemaV1, type StreamedRunResult, type ToolApprovalItem, type ToolOptions, type Usage, run, tool };
465
+ /**
466
+ * Agent UI on your backend: serve an agent to <AgentChat> from your own route. Your tools run here,
467
+ * with your database and secrets; the browser only sees the conversation.
468
+ *
469
+ * ```ts
470
+ * // app/api/chat/route.ts
471
+ * import { Agent, createAgentChatHandler } from 'cencori/agents';
472
+ *
473
+ * const support = new Agent({ name: 'Support', instructions: '...', tools: [lookupOrder], widgets: true });
474
+ * export const POST = createAgentChatHandler(support, {
475
+ * context: async (req) => ({ userId: await userIdFrom(req) }),
476
+ * });
477
+ *
478
+ * // In the page: <AgentChat api="/api/chat" />
479
+ * ```
480
+ *
481
+ * Approvals: when a tool needs one, the chat gets the paused run's state,
482
+ * signed, and sends it back with the user's decisions. The signature (HMAC
483
+ * with your secret key, or `secret`) means a browser can't alter what runs.
484
+ */
485
+ interface AgentChatHandlerOptions<Ctx> extends Omit<RunOptions<Ctx>, 'context' | 'sessionId' | 'stream' | 'signal' | 'user'> {
486
+ /** Your context for this request (who the user is, their permissions...). */
487
+ context?: (req: Request) => Ctx | Promise<Ctx>;
488
+ /** A stable id for the end user, for Cencori's per-user analytics and limits. */
489
+ user?: (req: Request) => string | undefined | Promise<string | undefined>;
490
+ /**
491
+ * May this request continue this session? Check it belongs to the user.
492
+ * Without it, any session id the browser sends is continued.
493
+ */
494
+ authorizeSession?: (sessionId: string, req: Request) => boolean | Promise<boolean>;
495
+ /** Signs paused-run state. Default: CENCORI_AGENT_CHAT_SECRET, then your API key. */
496
+ secret?: string;
497
+ }
498
+ declare function createAgentChatHandler<Ctx>(agent: Agent<Ctx, any>, options?: AgentChatHandlerOptions<Ctx>): (req: Request) => Promise<Response>;
499
+ /** A streamed run as the agent chat event stream (for your own route handler). */
500
+ declare function toAgentChatStream(streamed: StreamedRunResult<unknown>, options: {
501
+ secret: string;
502
+ }): ReadableStream<Uint8Array>;
503
+
504
+ export { Agent, type AgentChatHandlerOptions, type AgentOptions, AgentsError, CencoriRunError, DEFAULT_MODEL, type FunctionTool, type GuardrailResult, GuardrailTripwireTriggered, type InputGuardrail, type JsonSchema, MaxTurnsExceededError, type ModelSettings, type NeedsApproval, type OutputGuardrail, type RunContext, type RunItem, type RunOptions, type RunResult, RunState, type RunStreamEvent, type SchemaLike, SchemaValidationError, type StandardSchemaV1, type StreamedRunResult, type ToolApprovalItem, type ToolOptions, type Usage, WidgetNode, createAgentChatHandler, run, toAgentChatStream, tool };