cencori 1.9.0 → 1.11.0
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 +121 -0
- package/dist/agents/index.d.mts +497 -0
- package/dist/agents/index.d.ts +497 -0
- package/dist/agents/index.js +1421 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/index.mjs +1389 -0
- package/dist/agents/index.mjs.map +1 -0
- package/dist/protocol-T2jhccPz.d.mts +321 -0
- package/dist/protocol-T2jhccPz.d.ts +321 -0
- package/dist/react/index.d.mts +208 -8
- package/dist/react/index.d.ts +208 -8
- package/dist/react/index.js +1604 -171
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +1599 -174
- package/dist/react/index.mjs.map +1 -1
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -34,8 +34,129 @@ console.log(response.content);
|
|
|
34
34
|
| **Web** | ✅ Available | First-party fetch, extraction, crawl, and evidence-bearing search |
|
|
35
35
|
| **Billing** | ✅ Available | End-user usage monetization and Stripe integration |
|
|
36
36
|
| **Integration** | ✅ Available | SDKs, Vercel AI, TanStack |
|
|
37
|
+
| **Agents** | ✅ Available | Code-first agents (`cencori/agents`): tools, handoffs, approvals, guardrails, any model |
|
|
37
38
|
| **Embedded Agents** | ✅ Available | Multi-tenant agent backend: tenants, versions, runs, approvals, metering |
|
|
38
39
|
|
|
40
|
+
## Agents
|
|
41
|
+
|
|
42
|
+
`cencori/agents` builds agents in code and runs them on Cencori. Your tools run in your process; the agent loop, sessions, approvals, memory, guardrails, billing and traces run on Cencori, with any model in the catalog.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install cencori zod
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { Agent, run, tool } from 'cencori/agents';
|
|
50
|
+
import { z } from 'zod';
|
|
51
|
+
|
|
52
|
+
const getOrder = tool({
|
|
53
|
+
name: 'get_order',
|
|
54
|
+
description: 'Look up an order by id',
|
|
55
|
+
parameters: z.object({ id: z.string() }),
|
|
56
|
+
execute: async ({ id }) => db.orders.find(id),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const support = new Agent({
|
|
60
|
+
name: 'Support',
|
|
61
|
+
instructions: 'Answer order questions. Be brief.',
|
|
62
|
+
tools: [getOrder],
|
|
63
|
+
// model: 'claude-sonnet-4-5', // any catalog model; default gpt-oss-120b (fast)
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const result = await run(support, 'Where is order 1042?'); // reads CENCORI_API_KEY
|
|
67
|
+
console.log(result.finalOutput);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The agent calls as many tools, over as many rounds, as it needs. Pass `result.sessionId` back as `{ sessionId }` to continue the conversation.
|
|
71
|
+
|
|
72
|
+
**Stream**
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const stream = run(support, 'Where is order 1042?', { stream: true });
|
|
76
|
+
for await (const text of stream.toTextStream()) process.stdout.write(text);
|
|
77
|
+
const result = await stream.completed;
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Handoffs and agents as tools**
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const billing = new Agent({ name: 'Billing', instructions: 'You handle refunds.', handoffDescription: 'refunds and invoices' });
|
|
84
|
+
const triage = new Agent({ name: 'Triage', instructions: 'Route billing questions to Billing.', handoffs: [billing] });
|
|
85
|
+
// Or keep the conversation and just ask another agent: tools: [billing.asTool()]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Human approval**
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { RunState } from 'cencori/agents';
|
|
92
|
+
|
|
93
|
+
const refund = tool({
|
|
94
|
+
name: 'issue_refund',
|
|
95
|
+
description: 'Refund an order',
|
|
96
|
+
parameters: z.object({ id: z.string() }),
|
|
97
|
+
needsApproval: true, // recorded in your project's governance ledger
|
|
98
|
+
execute: async ({ id }) => payments.refund(id),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const first = await run(billingAgent, 'Refund order 1042');
|
|
102
|
+
if (first.interruptions.length) {
|
|
103
|
+
const saved = JSON.stringify(first.state); // store it; decide later, anywhere
|
|
104
|
+
const state = RunState.fromJSON(saved);
|
|
105
|
+
state.approve(first.interruptions[0]); // or state.reject(item, { message })
|
|
106
|
+
const done = await run(billingAgent, state);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
`needsApproval` can also be a function, `(ctx, args) => args.amount > 100`, decided per call.
|
|
111
|
+
|
|
112
|
+
**Structured output, guardrails, memory**
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const extractor = new Agent({
|
|
116
|
+
name: 'Extractor',
|
|
117
|
+
outputType: z.object({ city: z.string(), days: z.number() }), // finalOutput is typed
|
|
118
|
+
inputGuardrails: [{ name: 'on_topic', execute: ({ input }) => ({ tripwireTriggered: input.includes('homework') }) }],
|
|
119
|
+
modelSettings: { reasoningEffort: 'low' },
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
await run(extractor, 'Trip to Lagos for 3 days', { memory: { userId: 'user_123' }, user: 'user_123' });
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Tool parameters accept zod 3 or 4, any [Standard Schema](https://standardschema.dev) library, or a plain JSON Schema object.
|
|
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
|
+
|
|
39
160
|
## Embedded Agents
|
|
40
161
|
|
|
41
162
|
```typescript
|
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { W as WidgetNode } from '../protocol-T2jhccPz.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-T2jhccPz.mjs';
|
|
3
|
+
|
|
4
|
+
/** What tools, guardrails and dynamic instructions receive during a run. */
|
|
5
|
+
interface RunContext<Ctx = unknown> {
|
|
6
|
+
/** Your own value from run(..., { context }): a user, a db handle, anything. Never sent to Cencori. */
|
|
7
|
+
context: Ctx;
|
|
8
|
+
/**
|
|
9
|
+
* The Cencori session this run lives in. Empty in input guardrails and
|
|
10
|
+
* instructions of a new run: the session is created with its first turn.
|
|
11
|
+
*/
|
|
12
|
+
sessionId: string;
|
|
13
|
+
/** Name of the agent currently answering. */
|
|
14
|
+
agentName: string;
|
|
15
|
+
/** Aborts with the run. */
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Schemas for tool parameters and structured output, without depending on a
|
|
21
|
+
* validation library. Anything implementing Standard Schema works (zod 3.24+,
|
|
22
|
+
* zod 4, valibot, arktype); JSON Schema is read from Standard JSON Schema
|
|
23
|
+
* (zod 4.2+), a schema's own toJSONSchema(), or given directly as an object.
|
|
24
|
+
*/
|
|
25
|
+
/** The Standard Schema interface (https://standardschema.dev), inlined. */
|
|
26
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
27
|
+
readonly '~standard': {
|
|
28
|
+
readonly version: 1;
|
|
29
|
+
readonly vendor: string;
|
|
30
|
+
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
31
|
+
readonly types?: {
|
|
32
|
+
readonly input: Input;
|
|
33
|
+
readonly output: Output;
|
|
34
|
+
} | undefined;
|
|
35
|
+
readonly jsonSchema?: {
|
|
36
|
+
readonly input: (options: {
|
|
37
|
+
target: string;
|
|
38
|
+
}) => Record<string, unknown>;
|
|
39
|
+
readonly output: (options: {
|
|
40
|
+
target: string;
|
|
41
|
+
}) => Record<string, unknown>;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
type StandardResult<Output> = {
|
|
46
|
+
readonly value: Output;
|
|
47
|
+
readonly issues?: undefined;
|
|
48
|
+
} | {
|
|
49
|
+
readonly issues: ReadonlyArray<{
|
|
50
|
+
readonly message: string;
|
|
51
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
52
|
+
key: PropertyKey;
|
|
53
|
+
}>;
|
|
54
|
+
}>;
|
|
55
|
+
};
|
|
56
|
+
/** A JSON Schema object, passed through as is. */
|
|
57
|
+
type JsonSchema = Record<string, unknown>;
|
|
58
|
+
/** What tool parameters and outputType accept. */
|
|
59
|
+
type SchemaLike<T = unknown> = StandardSchemaV1<unknown, T> | JsonSchema;
|
|
60
|
+
/** The type a schema validates to; `unknown` for plain JSON Schema. */
|
|
61
|
+
type InferSchema<S> = S extends StandardSchemaV1<unknown, infer O> ? O : unknown;
|
|
62
|
+
declare class SchemaValidationError extends Error {
|
|
63
|
+
readonly issues: ReadonlyArray<{
|
|
64
|
+
message: string;
|
|
65
|
+
path?: string;
|
|
66
|
+
}>;
|
|
67
|
+
constructor(what: string, issues: ReadonlyArray<{
|
|
68
|
+
message: string;
|
|
69
|
+
path?: string;
|
|
70
|
+
}>);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Whether a call needs a human's approval before it runs. */
|
|
74
|
+
type NeedsApproval<Args, Ctx> = boolean | ((runContext: RunContext<Ctx>, args: Args, callId: string) => boolean | Promise<boolean>);
|
|
75
|
+
interface ToolOptions<S extends SchemaLike, Ctx = unknown, Out = unknown> {
|
|
76
|
+
/** Name the model calls it by: letters, digits, _ and -, at most 64. */
|
|
77
|
+
name: string;
|
|
78
|
+
/** What it does and when to use it. The model reads this. */
|
|
79
|
+
description: string;
|
|
80
|
+
/** Parameters: a zod (or any Standard Schema) object, or a JSON Schema object. */
|
|
81
|
+
parameters?: S;
|
|
82
|
+
/** Runs the tool. Return anything; non-strings are sent to the model as JSON. */
|
|
83
|
+
execute: (args: InferSchema<S>, runContext: RunContext<Ctx>, details: {
|
|
84
|
+
callId: string;
|
|
85
|
+
}) => Out | Promise<Out>;
|
|
86
|
+
/**
|
|
87
|
+
* Ask a human before running it. `true` also records the request and the
|
|
88
|
+
* decision in your project's governance ledger; a function decides per call.
|
|
89
|
+
*/
|
|
90
|
+
needsApproval?: NeedsApproval<InferSchema<S>, Ctx>;
|
|
91
|
+
/** What the model sees when execute throws. Default: the error's message. */
|
|
92
|
+
errorFunction?: (error: unknown, runContext: RunContext<Ctx>) => string | Promise<string>;
|
|
93
|
+
}
|
|
94
|
+
interface FunctionTool<Ctx = unknown> {
|
|
95
|
+
readonly type: 'function';
|
|
96
|
+
readonly name: string;
|
|
97
|
+
readonly description: string;
|
|
98
|
+
readonly parameters: JsonSchema;
|
|
99
|
+
readonly needsApproval: NeedsApproval<unknown, Ctx>;
|
|
100
|
+
/** Validate the model's arguments, run the tool, and return what the model sees. */
|
|
101
|
+
invoke(runContext: RunContext<Ctx>, rawArguments: string, callId: string): Promise<string>;
|
|
102
|
+
/** Parse and validate arguments (used to decide on approval before running). */
|
|
103
|
+
parseArguments(rawArguments: string): Promise<unknown>;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Define a tool the agent can call. It runs in your process, with your
|
|
107
|
+
* credentials and data; Cencori only sees its name, arguments and result.
|
|
108
|
+
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* const getOrder = tool({
|
|
111
|
+
* name: 'get_order',
|
|
112
|
+
* description: 'Look up an order by id',
|
|
113
|
+
* parameters: z.object({ id: z.string() }),
|
|
114
|
+
* execute: async ({ id }) => db.orders.find(id),
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare function tool<S extends SchemaLike, Ctx = unknown, Out = unknown>(options: ToolOptions<S, Ctx, Out>): FunctionTool<Ctx>;
|
|
119
|
+
|
|
120
|
+
interface TransportOptions {
|
|
121
|
+
/** Secret project key (csk_...). Defaults to the CENCORI_API_KEY environment variable. */
|
|
122
|
+
apiKey?: string;
|
|
123
|
+
/** Defaults to CENCORI_BASE_URL, then https://cencori.com. */
|
|
124
|
+
baseUrl?: string;
|
|
125
|
+
/** Custom fetch (tests, proxies, edge runtimes). */
|
|
126
|
+
fetch?: typeof fetch;
|
|
127
|
+
signal?: AbortSignal;
|
|
128
|
+
}
|
|
129
|
+
/** A tool call the turn paused on. */
|
|
130
|
+
interface PausedAction {
|
|
131
|
+
action_id: string;
|
|
132
|
+
tool: string;
|
|
133
|
+
arguments: string;
|
|
134
|
+
needs_approval?: boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
interface RunOptions<Ctx = unknown> extends TransportOptions {
|
|
138
|
+
/** Your value for tools, guardrails and dynamic instructions. Stays in your process. */
|
|
139
|
+
context?: Ctx;
|
|
140
|
+
/** Continue an earlier conversation: pass the sessionId from a previous result. */
|
|
141
|
+
sessionId?: string;
|
|
142
|
+
/** Most model calls this run may make (each tool round is one). Default 10. */
|
|
143
|
+
maxTurns?: number;
|
|
144
|
+
/** Stream events as they happen; see StreamedRunResult. */
|
|
145
|
+
stream?: boolean;
|
|
146
|
+
/** Turn on Cencori memory for this user: recalled before, written after. */
|
|
147
|
+
memory?: {
|
|
148
|
+
userId: string;
|
|
149
|
+
[key: string]: unknown;
|
|
150
|
+
};
|
|
151
|
+
/** Your end user's id: per-user quotas, billing and memory. */
|
|
152
|
+
user?: string;
|
|
153
|
+
/** Extra metadata on the session (strings). */
|
|
154
|
+
metadata?: Record<string, string>;
|
|
155
|
+
}
|
|
156
|
+
/** A tool call waiting for a human. Approve or reject it on result.state, then run again. */
|
|
157
|
+
interface ToolApprovalItem {
|
|
158
|
+
type: 'tool_approval';
|
|
159
|
+
callId: string;
|
|
160
|
+
toolName: string;
|
|
161
|
+
/** The model's arguments, as JSON text. */
|
|
162
|
+
arguments: string;
|
|
163
|
+
agentName: string;
|
|
164
|
+
}
|
|
165
|
+
type RunItem = {
|
|
166
|
+
type: 'message';
|
|
167
|
+
agentName: string;
|
|
168
|
+
text: string;
|
|
169
|
+
} | {
|
|
170
|
+
type: 'tool_call';
|
|
171
|
+
agentName: string;
|
|
172
|
+
callId: string;
|
|
173
|
+
toolName: string;
|
|
174
|
+
arguments: string;
|
|
175
|
+
} | {
|
|
176
|
+
type: 'tool_output';
|
|
177
|
+
agentName: string;
|
|
178
|
+
callId: string;
|
|
179
|
+
toolName: string;
|
|
180
|
+
output: string;
|
|
181
|
+
} | {
|
|
182
|
+
type: 'handoff';
|
|
183
|
+
from: string;
|
|
184
|
+
to: string;
|
|
185
|
+
} | {
|
|
186
|
+
type: 'widget';
|
|
187
|
+
agentName: string;
|
|
188
|
+
callId: string;
|
|
189
|
+
widgetId: string;
|
|
190
|
+
widget: WidgetNode;
|
|
191
|
+
};
|
|
192
|
+
interface Usage {
|
|
193
|
+
inputTokens: number;
|
|
194
|
+
outputTokens: number;
|
|
195
|
+
totalTokens: number;
|
|
196
|
+
}
|
|
197
|
+
type RunStreamEvent = {
|
|
198
|
+
type: 'agent_updated';
|
|
199
|
+
agent: Agent<any>;
|
|
200
|
+
} | {
|
|
201
|
+
type: 'text_delta';
|
|
202
|
+
delta: string;
|
|
203
|
+
agentName: string;
|
|
204
|
+
} | {
|
|
205
|
+
type: 'tool_call';
|
|
206
|
+
callId: string;
|
|
207
|
+
toolName: string;
|
|
208
|
+
arguments: string;
|
|
209
|
+
agentName: string;
|
|
210
|
+
} | {
|
|
211
|
+
type: 'tool_output';
|
|
212
|
+
callId: string;
|
|
213
|
+
toolName: string;
|
|
214
|
+
output: string;
|
|
215
|
+
agentName: string;
|
|
216
|
+
} | {
|
|
217
|
+
type: 'handoff';
|
|
218
|
+
from: string;
|
|
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
|
+
type: 'interruption';
|
|
230
|
+
interruptions: ToolApprovalItem[];
|
|
231
|
+
};
|
|
232
|
+
interface PendingPause {
|
|
233
|
+
actionId: string;
|
|
234
|
+
actions: PausedAction[];
|
|
235
|
+
text: string;
|
|
236
|
+
}
|
|
237
|
+
interface SerializedRunState {
|
|
238
|
+
v: 1;
|
|
239
|
+
sessionId: string;
|
|
240
|
+
agentName: string;
|
|
241
|
+
turnsUsed: number;
|
|
242
|
+
usage: Usage;
|
|
243
|
+
pending: PendingPause;
|
|
244
|
+
decisions: Record<string, {
|
|
245
|
+
approved: boolean;
|
|
246
|
+
message?: string;
|
|
247
|
+
}>;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* A paused run: tool calls are waiting for a human. Approve or reject each,
|
|
251
|
+
* then pass the state back to run(). Serializable with JSON.stringify, so the
|
|
252
|
+
* decision can come minutes or hours later from another process (Cencori
|
|
253
|
+
* keeps a paused session for 24 hours).
|
|
254
|
+
*/
|
|
255
|
+
declare class RunState {
|
|
256
|
+
readonly sessionId: string;
|
|
257
|
+
readonly agentName: string;
|
|
258
|
+
readonly turnsUsed: number;
|
|
259
|
+
readonly usage: Usage;
|
|
260
|
+
/** @internal */
|
|
261
|
+
readonly pending: PendingPause;
|
|
262
|
+
private readonly decisions;
|
|
263
|
+
/** @internal */
|
|
264
|
+
constructor(init: Omit<SerializedRunState, 'v' | 'decisions'> & {
|
|
265
|
+
decisions?: SerializedRunState['decisions'];
|
|
266
|
+
});
|
|
267
|
+
approve(item: ToolApprovalItem | string): void;
|
|
268
|
+
/** The model is told the call was rejected, with your message if given. */
|
|
269
|
+
reject(item: ToolApprovalItem | string, options?: {
|
|
270
|
+
message?: string;
|
|
271
|
+
}): void;
|
|
272
|
+
/** @internal */
|
|
273
|
+
decision(callId: string): {
|
|
274
|
+
approved: boolean;
|
|
275
|
+
message?: string;
|
|
276
|
+
} | undefined;
|
|
277
|
+
toJSON(): SerializedRunState;
|
|
278
|
+
static fromJSON(json: string | SerializedRunState): RunState;
|
|
279
|
+
}
|
|
280
|
+
interface RunResult<Output = string> {
|
|
281
|
+
/** The final answer: text, or the parsed outputType. Undefined while interrupted. */
|
|
282
|
+
finalOutput: Output | undefined;
|
|
283
|
+
/** The Cencori session: pass it as sessionId to continue the conversation. */
|
|
284
|
+
sessionId: string;
|
|
285
|
+
/** The agent that gave the final answer (after any handoffs). */
|
|
286
|
+
lastAgent: Agent<any>;
|
|
287
|
+
/** Tool calls waiting for approval; empty when the run finished. */
|
|
288
|
+
interruptions: ToolApprovalItem[];
|
|
289
|
+
/** Set when interrupted: approve or reject, then run(agent, state). */
|
|
290
|
+
state: RunState | undefined;
|
|
291
|
+
/** Messages, tool calls and handoffs in this run, in order. */
|
|
292
|
+
newItems: RunItem[];
|
|
293
|
+
usage: Usage;
|
|
294
|
+
}
|
|
295
|
+
/** run(..., { stream: true }): iterate events as they happen, then await completed. */
|
|
296
|
+
interface StreamedRunResult<Output = string> extends AsyncIterable<RunStreamEvent> {
|
|
297
|
+
/** Resolves with the result once the run ends (after you finish iterating, or on its own). */
|
|
298
|
+
readonly completed: Promise<RunResult<Output>>;
|
|
299
|
+
/** Only the text deltas. */
|
|
300
|
+
toTextStream(): AsyncIterable<string>;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Run an agent until it answers, pausing tool calls run in your process.
|
|
304
|
+
*
|
|
305
|
+
* ```ts
|
|
306
|
+
* const result = await run(support, 'Where is order 1042?');
|
|
307
|
+
* result.finalOutput; // "It shipped yesterday..."
|
|
308
|
+
* ```
|
|
309
|
+
*
|
|
310
|
+
* Pass `{ stream: true }` for events as they happen, `{ sessionId }` to
|
|
311
|
+
* continue a conversation, or a RunState to resume after approvals.
|
|
312
|
+
*/
|
|
313
|
+
declare function run<Ctx, Out>(agent: Agent<Ctx, Out>, input: string | RunState, options: RunOptions<Ctx> & {
|
|
314
|
+
stream: true;
|
|
315
|
+
}): StreamedRunResult<Out>;
|
|
316
|
+
declare function run<Ctx, Out>(agent: Agent<Ctx, Out>, input: string | RunState, options?: RunOptions<Ctx> & {
|
|
317
|
+
stream?: false;
|
|
318
|
+
}): Promise<RunResult<Out>>;
|
|
319
|
+
|
|
320
|
+
/** Checks the user's input before the first model call. */
|
|
321
|
+
interface InputGuardrail<Ctx = unknown> {
|
|
322
|
+
name: string;
|
|
323
|
+
execute: (args: {
|
|
324
|
+
input: string;
|
|
325
|
+
agent: Agent<Ctx, any>;
|
|
326
|
+
runContext: RunContext<Ctx>;
|
|
327
|
+
}) => GuardrailResult | Promise<GuardrailResult>;
|
|
328
|
+
}
|
|
329
|
+
/** Checks the final answer before the run returns it. */
|
|
330
|
+
interface OutputGuardrail<Ctx = unknown> {
|
|
331
|
+
name: string;
|
|
332
|
+
execute: (args: {
|
|
333
|
+
output: unknown;
|
|
334
|
+
agent: Agent<Ctx, any>;
|
|
335
|
+
runContext: RunContext<Ctx>;
|
|
336
|
+
}) => GuardrailResult | Promise<GuardrailResult>;
|
|
337
|
+
}
|
|
338
|
+
interface GuardrailResult {
|
|
339
|
+
/** true stops the run with a GuardrailTripwireTriggered error. */
|
|
340
|
+
tripwireTriggered: boolean;
|
|
341
|
+
/** Anything you want back on the error. */
|
|
342
|
+
outputInfo?: unknown;
|
|
343
|
+
}
|
|
344
|
+
interface ModelSettings {
|
|
345
|
+
temperature?: number;
|
|
346
|
+
maxTokens?: number;
|
|
347
|
+
/**
|
|
348
|
+
* How hard a reasoning model thinks before answering. Lower is faster;
|
|
349
|
+
* 'none' skips thinking where the model allows it. Default: the model's own.
|
|
350
|
+
*/
|
|
351
|
+
reasoningEffort?: 'none' | 'low' | 'medium' | 'high';
|
|
352
|
+
}
|
|
353
|
+
interface AgentOptions<Ctx = unknown, Out = string> {
|
|
354
|
+
/** Shown in traces, and used for handoff tool names (transfer_to_<name>). */
|
|
355
|
+
name: string;
|
|
356
|
+
/** System prompt. A function gets the run context, for per-user instructions. */
|
|
357
|
+
instructions?: string | ((runContext: RunContext<Ctx>) => string | Promise<string>);
|
|
358
|
+
/**
|
|
359
|
+
* Any model in the Cencori catalog: 'claude-sonnet-4-5', 'gemini-2.5-flash'...
|
|
360
|
+
* Default: DEFAULT_MODEL, GPT OSS 120B on Cerebras (fast, calls tools).
|
|
361
|
+
*/
|
|
362
|
+
model?: string;
|
|
363
|
+
tools?: FunctionTool<Ctx>[];
|
|
364
|
+
/** Agents this one can hand the conversation to. */
|
|
365
|
+
handoffs?: Agent<Ctx, any>[];
|
|
366
|
+
/** Tells other agents when to hand off to this one. */
|
|
367
|
+
handoffDescription?: string;
|
|
368
|
+
/** Structured final output: a zod (or Standard Schema) object, or a JSON Schema. */
|
|
369
|
+
outputType?: StandardSchemaV1<unknown, Out> | Record<string, unknown>;
|
|
370
|
+
modelSettings?: ModelSettings;
|
|
371
|
+
inputGuardrails?: InputGuardrail<Ctx>[];
|
|
372
|
+
outputGuardrails?: OutputGuardrail<Ctx>[];
|
|
373
|
+
/**
|
|
374
|
+
* Let the agent answer with UI: it gets the built-in render_ui tool and can
|
|
375
|
+
* show cards, tables, charts, stats, buttons and forms in a chat
|
|
376
|
+
* (cencori/react's <AgentChat> renders them). Tools can also return
|
|
377
|
+
* `{ widget }` whether or not this is on.
|
|
378
|
+
*/
|
|
379
|
+
widgets?: boolean;
|
|
380
|
+
}
|
|
381
|
+
/** The model an agent uses when none is given: GPT OSS 120B on Cerebras, chosen for speed. */
|
|
382
|
+
declare const DEFAULT_MODEL = "gpt-oss-120b";
|
|
383
|
+
/**
|
|
384
|
+
* An agent: a model, instructions and tools. It holds no state; run() drives
|
|
385
|
+
* it in a Cencori session.
|
|
386
|
+
*
|
|
387
|
+
* ```ts
|
|
388
|
+
* const support = new Agent({
|
|
389
|
+
* name: 'Support',
|
|
390
|
+
* instructions: 'Answer order questions. Be brief.',
|
|
391
|
+
* model: 'claude-sonnet-4-5',
|
|
392
|
+
* tools: [getOrder],
|
|
393
|
+
* });
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
declare class Agent<Ctx = unknown, Out = string> {
|
|
397
|
+
readonly name: string;
|
|
398
|
+
readonly instructions: AgentOptions<Ctx, Out>['instructions'];
|
|
399
|
+
readonly model: string;
|
|
400
|
+
readonly tools: FunctionTool<Ctx>[];
|
|
401
|
+
readonly handoffs: Agent<Ctx, any>[];
|
|
402
|
+
readonly handoffDescription?: string;
|
|
403
|
+
readonly outputType?: SchemaLike;
|
|
404
|
+
readonly modelSettings: ModelSettings;
|
|
405
|
+
readonly inputGuardrails: InputGuardrail<Ctx>[];
|
|
406
|
+
readonly outputGuardrails: OutputGuardrail<Ctx>[];
|
|
407
|
+
readonly widgets: boolean;
|
|
408
|
+
constructor(options: AgentOptions<Ctx, Out>);
|
|
409
|
+
/** A copy with some options changed. */
|
|
410
|
+
clone(overrides: Partial<AgentOptions<Ctx, Out>>): Agent<Ctx, Out>;
|
|
411
|
+
/** The tool name a handoff to this agent goes by. */
|
|
412
|
+
static handoffToolName(agent: Agent<any>): string;
|
|
413
|
+
/** Instructions for this run, resolving a function if given. */
|
|
414
|
+
resolveInstructions(runContext: RunContext<Ctx>): Promise<string | undefined>;
|
|
415
|
+
/**
|
|
416
|
+
* Use this agent as a tool of another: it runs on its own (in its own
|
|
417
|
+
* session) and returns its final answer, while the calling agent keeps the
|
|
418
|
+
* conversation. Compare handoffs, where this agent would take it over.
|
|
419
|
+
*/
|
|
420
|
+
asTool(options?: {
|
|
421
|
+
toolName?: string;
|
|
422
|
+
toolDescription?: string;
|
|
423
|
+
runOptions?: Pick<RunOptions<Ctx>, 'maxTurns' | 'apiKey' | 'baseUrl'>;
|
|
424
|
+
}): FunctionTool<Ctx>;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** Base class for errors thrown by cencori/agents. */
|
|
428
|
+
declare class AgentsError extends Error {
|
|
429
|
+
constructor(message: string);
|
|
430
|
+
}
|
|
431
|
+
/** The run used up maxTurns model calls without a final answer. */
|
|
432
|
+
declare class MaxTurnsExceededError extends AgentsError {
|
|
433
|
+
readonly sessionId: string;
|
|
434
|
+
constructor(maxTurns: number, sessionId: string);
|
|
435
|
+
}
|
|
436
|
+
/** A guardrail's tripwire fired; the run stopped. */
|
|
437
|
+
declare class GuardrailTripwireTriggered extends AgentsError {
|
|
438
|
+
readonly guardrail: string;
|
|
439
|
+
readonly kind: 'input' | 'output';
|
|
440
|
+
readonly outputInfo: unknown;
|
|
441
|
+
constructor(kind: 'input' | 'output', guardrail: string, outputInfo: unknown);
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Cencori refused or failed a request, or the turn failed (a project policy
|
|
445
|
+
* blocked it, the model provider errored, credits ran out...).
|
|
446
|
+
*/
|
|
447
|
+
declare class CencoriRunError extends AgentsError {
|
|
448
|
+
readonly status?: number;
|
|
449
|
+
readonly code?: string;
|
|
450
|
+
readonly sessionId?: string;
|
|
451
|
+
constructor(message: string, details?: {
|
|
452
|
+
status?: number;
|
|
453
|
+
code?: string;
|
|
454
|
+
sessionId?: string;
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Agent UI on your backend: serve an agent to <AgentChat> from your own route. Your tools run here,
|
|
460
|
+
* with your database and secrets; the browser only sees the conversation.
|
|
461
|
+
*
|
|
462
|
+
* ```ts
|
|
463
|
+
* // app/api/chat/route.ts
|
|
464
|
+
* import { Agent, createAgentChatHandler } from 'cencori/agents';
|
|
465
|
+
*
|
|
466
|
+
* const support = new Agent({ name: 'Support', instructions: '...', tools: [lookupOrder], widgets: true });
|
|
467
|
+
* export const POST = createAgentChatHandler(support, {
|
|
468
|
+
* context: async (req) => ({ userId: await userIdFrom(req) }),
|
|
469
|
+
* });
|
|
470
|
+
*
|
|
471
|
+
* // In the page: <AgentChat api="/api/chat" />
|
|
472
|
+
* ```
|
|
473
|
+
*
|
|
474
|
+
* Approvals: when a tool needs one, the chat gets the paused run's state,
|
|
475
|
+
* signed, and sends it back with the user's decisions. The signature (HMAC
|
|
476
|
+
* with your secret key, or `secret`) means a browser can't alter what runs.
|
|
477
|
+
*/
|
|
478
|
+
interface AgentChatHandlerOptions<Ctx> extends Omit<RunOptions<Ctx>, 'context' | 'sessionId' | 'stream' | 'signal' | 'user'> {
|
|
479
|
+
/** Your context for this request (who the user is, their permissions...). */
|
|
480
|
+
context?: (req: Request) => Ctx | Promise<Ctx>;
|
|
481
|
+
/** A stable id for the end user, for Cencori's per-user analytics and limits. */
|
|
482
|
+
user?: (req: Request) => string | undefined | Promise<string | undefined>;
|
|
483
|
+
/**
|
|
484
|
+
* May this request continue this session? Check it belongs to the user.
|
|
485
|
+
* Without it, any session id the browser sends is continued.
|
|
486
|
+
*/
|
|
487
|
+
authorizeSession?: (sessionId: string, req: Request) => boolean | Promise<boolean>;
|
|
488
|
+
/** Signs paused-run state. Default: CENCORI_AGENT_CHAT_SECRET, then your API key. */
|
|
489
|
+
secret?: string;
|
|
490
|
+
}
|
|
491
|
+
declare function createAgentChatHandler<Ctx>(agent: Agent<Ctx, any>, options?: AgentChatHandlerOptions<Ctx>): (req: Request) => Promise<Response>;
|
|
492
|
+
/** A streamed run as the agent chat event stream (for your own route handler). */
|
|
493
|
+
declare function toAgentChatStream(streamed: StreamedRunResult<unknown>, options: {
|
|
494
|
+
secret: string;
|
|
495
|
+
}): ReadableStream<Uint8Array>;
|
|
496
|
+
|
|
497
|
+
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 };
|