llmz 0.0.28 → 0.0.30

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
@@ -1,24 +1,97 @@
1
- # 🚀 LLMz: Next-Gen TypeScript AI Agent Framework
1
+ # LLMz
2
2
 
3
- **Build AI agents that think, code, and execute like humans—powered by TypeScript and battle-tested at massive scale.**
3
+ **Stop chaining tools. Start generating code.**
4
+
5
+ LLMz is a TypeScript AI agent framework that replaces traditional JSON tool calling with executable code generation. Instead of orchestrating tools through multiple LLM roundtrips, agents write and execute TypeScript directly—enabling complex logic, loops, and multi-tool coordination in a single pass.
6
+
7
+ Powers millions of production agents at [Botpress](https://botpress.com).
8
+
9
+ | | |
10
+ | --- | ----------------------------------------------------------------------------------------- |
11
+ | 📚 | [**Examples →**](https://github.com/botpress/botpress/tree/master/packages/llmz/examples) |
4
12
 
5
13
  ---
6
14
 
7
- LLMz is the revolutionary framework behind [Botpress](https://botpress.com), empowering millions of AI agents across thousands of use cases worldwide. Unlike traditional tool-calling frameworks, LLMz leverages the massive body of TypeScript knowledge within LLMs, enabling agents to handle complex logic, loops, conditionals, and multi-tool orchestration seamlessly.
15
+ ## The Problem with Tool Calling
8
16
 
9
- **Stop chaining tools. Start generating real code.**
17
+ Traditional agentic frameworks (LangChain, CrewAI, MCP servers) rely on JSON tool calling:
18
+
19
+ ```json
20
+ {
21
+ "tool": "getTicketPrice",
22
+ "parameters": { "from": "quebec", "to": "new york" }
23
+ }
24
+ ```
25
+
26
+ This breaks down quickly:
27
+
28
+ - **Verbose schemas**: LLMs struggle with complex JSON structures
29
+ - **No logic**: Can't express conditionals, loops, or error handling
30
+ - **Multiple roundtrips**: Each tool call requires another LLM inference ($$$)
31
+ - **Fragile composition**: Chaining tools is error-prone and expensive
32
+
33
+ You end up with brittle agents that cost 10-100x more than they should.
10
34
 
11
35
  ---
12
36
 
13
- ## 📦 Quick Start
37
+ ## The LLMz Solution
38
+
39
+ LLMz generates and executes **real TypeScript code** in a secure sandbox:
14
40
 
15
- Install and start building agents in seconds:
41
+ ```typescript
42
+ const price = await getTicketPrice({ from: 'quebec', to: 'new york' })
43
+
44
+ if (price > 500) {
45
+ throw new Error('Price too high')
46
+ }
47
+
48
+ const ticketId = await buyTicket({ from: 'quebec', to: 'new york' })
49
+ return { action: 'done', result: ticketId }
50
+ ```
51
+
52
+ **Why this works:**
53
+
54
+ - LLMs have seen billions of lines of TypeScript—they're exceptionally good at it
55
+ - Complex logic (loops, conditionals, error handling) happens in one inference
56
+ - Multiple tool calls execute synchronously without LLM roundtrips
57
+ - Full type safety via Zui schemas and TypeScript inference
58
+
59
+ **Real-world impact**: Anthropic reduced agent costs by 98.7% using code execution patterns ([source](https://www.anthropic.com/engineering/code-execution-with-mcp)).
60
+
61
+ ---
62
+
63
+ ## Quick Start
64
+
65
+ **Requirements:** Node.js 20+
16
66
 
17
67
  ```bash
18
68
  npm install @botpress/client llmz
19
69
  ```
20
70
 
21
- ### Example
71
+ ### Platform Support
72
+
73
+ | Platform | Support |
74
+ | ------------------ | ------- |
75
+ | Node.js 20+ | ✅ Full |
76
+ | Browser | ✅ Full |
77
+ | AWS Lambda | ✅ Full |
78
+ | Cloudflare Workers | ✅ Full |
79
+ | Bun | ✅ Full |
80
+ | Deno | ✅ Full |
81
+
82
+ #### Sandbox Execution
83
+
84
+ LLMz uses **QuickJS** (a lightweight JavaScript engine compiled to WebAssembly) to execute generated code in a secure, isolated sandbox. This provides:
85
+
86
+ - **Complete isolation**: No access to filesystem, network, or host environment
87
+ - **Memory limits**: Configurable heap size to prevent resource exhaustion
88
+ - **Execution timeouts**: Automatic termination of runaway code
89
+ - **Abort signals**: Support for programmatic execution cancellation
90
+ - **Universal compatibility**: Works everywhere WebAssembly is supported
91
+
92
+ The QuickJS sandbox is bundled as a singlefile variant with WASM inlined as base64, so it works out-of-the-box with any bundler (esbuild, webpack, vite, rollup) without configuration.
93
+
94
+ ### Worker Mode: Autonomous Execution
22
95
 
23
96
  ```typescript
24
97
  import { Client } from '@botpress/client'
@@ -27,67 +100,401 @@ import { execute } from 'llmz'
27
100
  const client = new Client({ botId: '...', token: '...' })
28
101
 
29
102
  const result = await execute({
30
- instructions: 'What is the sum of integers between 14 and 1078 divisible by 3, 9, or 5?',
103
+ instructions: 'Calculate sum of integers 14-1078 divisible by 3, 9, or 5',
31
104
  client,
32
105
  })
33
106
 
34
107
  console.log(result.output) // 271575
35
108
  ```
36
109
 
37
- **Instruction:**
110
+ **Generated code:**
38
111
 
39
112
  ```typescript
40
- 'What is the sum of integers between 14 and 1078 divisible by 3, 9, or 5?'
113
+ let sum = 0
114
+ for (let i = 14; i <= 1078; i++) {
115
+ if (i % 3 === 0 || i % 9 === 0 || i % 5 === 0) {
116
+ sum += i
117
+ }
118
+ }
119
+ return { action: 'done', value: { success: true, result: sum } }
41
120
  ```
42
121
 
43
- **LLMz generates and safely executes real TypeScript:**
122
+ ### Chat Mode: Interactive Agents
44
123
 
45
124
  ```typescript
46
- // Calculating the sum of all integers between 14 and 1078 divisible by 3, 9 or 5
47
- let sum = 0
125
+ import { execute } from 'llmz'
126
+ import { Text, Button } from './components'
48
127
 
49
- // Loop through numbers between 14 and 1078 (inclusive)
50
- for (let i = 14; i <= 1078; i++) {
51
- if (i % 3 === 0 || i % 9 === 0 || i % 5 === 0) {
52
- sum += i // Add to sum if divisible by 3, 9, or 5
128
+ const tools = [searchFlights, bookTicket, cancelBooking]
129
+
130
+ let state = { transcript: [] }
131
+
132
+ while (true) {
133
+ const result = await execute({
134
+ client,
135
+ tools,
136
+ chat: {
137
+ transcript: state.transcript,
138
+ components: { Text, Button },
139
+ },
140
+ })
141
+
142
+ if (result.is('listen')) {
143
+ // Agent yielded UI and is waiting for user input
144
+ console.log('Agent:', result.value.components)
145
+ const userInput = await getUserInput()
146
+ state.transcript.push({ role: 'user', content: userInput })
147
+ } else {
148
+ // Agent completed the task
149
+ break
53
150
  }
54
151
  }
152
+ ```
55
153
 
56
- // Return the final result
57
- return { action: 'done', value: { success: true, result: sum } }
154
+ **Generated code:**
155
+
156
+ ```typescript
157
+ const flights = await searchFlights({ from: 'SFO', to: 'NYC' })
158
+
159
+ yield (
160
+ <Text>
161
+ Found {flights.length} flights. Cheapest is ${flights[0].price}. Book it?
162
+ </Text>
163
+ )
164
+ yield <Button>Book Flight</Button>
165
+ yield <Button>Cancel</Button>
166
+
167
+ return { action: 'listen' }
58
168
  ```
59
169
 
60
- **Result:**
170
+ ---
61
171
 
62
- ```json
63
- { "success": true, "result": 271575 }
172
+ ## Core Concepts
173
+
174
+ ### Execution Modes
175
+
176
+ **Worker Mode**: Autonomous agents that execute to completion
177
+
178
+ ```typescript
179
+ const result = await execute({
180
+ instructions: 'Analyze Q4 sales data and generate report',
181
+ client,
182
+ tools: [fetchSales, calculateMetrics, generatePDF],
183
+ })
184
+ ```
185
+
186
+ **Chat Mode**: Interactive conversations with user input
187
+
188
+ ```typescript
189
+ const result = await execute({
190
+ client,
191
+ tools,
192
+ chat: {
193
+ transcript: conversationHistory,
194
+ components: { Text, Button, Form },
195
+ },
196
+ })
197
+ ```
198
+
199
+ ### Tools: Type-Safe Functions
200
+
201
+ ```typescript
202
+ import { Tool } from 'llmz'
203
+ import { z } from '@bpinternal/zui'
204
+
205
+ const searchFlights = new Tool({
206
+ name: 'searchFlights',
207
+ description: 'Search for available flights',
208
+ input: z.object({
209
+ from: z.string(),
210
+ to: z.string(),
211
+ date: z.string(),
212
+ }),
213
+ output: z.array(
214
+ z.object({
215
+ id: z.string(),
216
+ price: z.number(),
217
+ departure: z.string(),
218
+ })
219
+ ),
220
+ handler: async ({ from, to, date }) => {
221
+ // Your implementation
222
+ return flights
223
+ },
224
+ })
225
+ ```
226
+
227
+ Tools are exposed to agents with full TypeScript signatures. Agents call them like regular async functions.
228
+
229
+ ### Objects: Namespaced State
230
+
231
+ Group related tools and variables:
232
+
233
+ ```typescript
234
+ import { ObjectInstance } from 'llmz'
235
+ import { z } from '@bpinternal/zui'
236
+
237
+ const database = new ObjectInstance({
238
+ name: 'db',
239
+ description: 'Database operations',
240
+ tools: [queryUsers, updateRecord, deleteRecord],
241
+ properties: [
242
+ {
243
+ name: 'connectionString',
244
+ value: process.env.DB_URL,
245
+ writable: false,
246
+ },
247
+ {
248
+ name: 'lastQueryTime',
249
+ value: null,
250
+ type: z.string().nullable(),
251
+ writable: true,
252
+ },
253
+ ],
254
+ })
255
+ ```
256
+
257
+ Agents access via namespaces:
258
+
259
+ ```typescript
260
+ const users = await db.queryUsers({ active: true })
261
+ db.lastQueryTime = new Date().toISOString()
262
+ ```
263
+
264
+ ### Exits: Structured Termination
265
+
266
+ Define how agents can complete:
267
+
268
+ ```typescript
269
+ import { Exit } from 'llmz'
270
+ import { z } from '@bpinternal/zui'
271
+
272
+ const TicketBooked = new Exit({
273
+ name: 'ticket_booked',
274
+ description: 'Successfully booked a ticket',
275
+ schema: z.object({
276
+ ticketId: z.string(),
277
+ price: z.number(),
278
+ confirmation: z.string(),
279
+ }),
280
+ })
281
+
282
+ const result = await execute({
283
+ client,
284
+ tools,
285
+ exits: [TicketBooked],
286
+ })
287
+
288
+ if (result.is(TicketBooked)) {
289
+ console.log('Booked:', result.value.ticketId) // Fully typed
290
+ }
291
+ ```
292
+
293
+ Agents use exits via return statements:
294
+
295
+ ```typescript
296
+ return {
297
+ action: 'ticket_booked',
298
+ ticketId: 'TKT-12345',
299
+ price: 299,
300
+ confirmation: 'ABC123',
301
+ }
302
+ ```
303
+
304
+ ---
305
+
306
+ ## Advanced Features
307
+
308
+ ### Thinking: Forced Reflection
309
+
310
+ Prevent agents from rushing to conclusions:
311
+
312
+ ```typescript
313
+ import { ThinkSignal } from 'llmz'
314
+
315
+ const complexAnalysis = new Tool({
316
+ name: 'analyze',
317
+ handler: async (data) => {
318
+ const result = performComplexCalculation(data)
319
+ // Force agent to reflect on results before proceeding
320
+ throw new ThinkSignal('Analysis complete. Review data before next step.')
321
+ },
322
+ })
323
+ ```
324
+
325
+ Agents can also self-initiate thinking:
326
+
327
+ ```typescript
328
+ // Agent-generated code
329
+ const data = await fetchLargeDataset()
330
+ return { action: 'think' } // Pause to process information
331
+ ```
332
+
333
+ ### Snapshots: Pause and Resume
334
+
335
+ Save execution state for long-running workflows:
336
+
337
+ ```typescript
338
+ import { SnapshotSignal } from 'llmz'
339
+
340
+ const approvalRequired = new Tool({
341
+ name: 'submitForApproval',
342
+ handler: async (request) => {
343
+ await saveToDatabase(request)
344
+ // Halt execution until manual approval
345
+ throw new SnapshotSignal('Awaiting manager approval')
346
+ },
347
+ })
348
+
349
+ // Later, resume from snapshot
350
+ const result = await execute({
351
+ client,
352
+ snapshot: savedSnapshot,
353
+ })
354
+ ```
355
+
356
+ ### Hooks: Custom Logic Injection
357
+
358
+ ```typescript
359
+ const result = await execute({
360
+ client,
361
+ tools,
362
+ hooks: {
363
+ onTrace: (trace) => {
364
+ // Non-blocking: log tool calls, errors, outputs
365
+ logger.info(trace)
366
+ },
367
+ onExit: (exit) => {
368
+ // Validate before allowing exit
369
+ if (exit.action === 'transfer_money' && exit.amount > 10000) {
370
+ throw new Error('Amount exceeds limit')
371
+ }
372
+ },
373
+ onBeforeExecution: (code) => {
374
+ // Inspect/modify generated code before execution
375
+ if (code.includes('dangerousOperation')) {
376
+ throw new Error('Blocked unsafe operation')
377
+ }
378
+ },
379
+ },
380
+ })
64
381
  ```
65
382
 
66
383
  ---
67
384
 
68
- ## Why LLMz Beats JSON Tool Calling
385
+ ## Coming from MCP?
69
386
 
70
- ### Traditional JSON Tool Calling
387
+ LLMz is **not** a replacement for MCP—it's complementary.
71
388
 
72
- - Hard-to-parse JSON schemas for LLMs
73
- - Incapable of complex logic, loops, and conditionals
74
- - Multiple expensive roundtrips for each tool call
75
- - Unreliable beyond simple scenarios
389
+ **MCP** (Model Context Protocol): Standardizes how AI applications connect to data sources and tools across processes/machines.
76
390
 
77
- ### LLMz TypeScript Generation
391
+ **LLMz**: Replaces the execution pattern _after_ tools are exposed. Instead of making multiple LLM calls to orchestrate MCP tools via JSON, LLMz generates TypeScript code that calls those same tools in a single inference—reducing costs by up to 98%.
392
+
393
+ ---
78
394
 
79
- - **Billions** of lines of TypeScript training data
80
- - Native LLM thinking via comments and code structure
81
- - Complex logic and multi-tool orchestration in **one call**
82
- - Complete type safety and predictable schemas
83
- - Scales effortlessly in production
395
+ ## Production Ready
396
+
397
+ LLMz has been running in production for over a year:
398
+
399
+ - **Millions** of active users across enterprise and consumer applications
400
+ - **Hundreds of thousands** of deployed agents handling real-world workloads
401
+ - **Secure sandbox**: Uses QuickJS WASM for isolated code execution
402
+ - **Type-safe**: Full TypeScript inference and Zui validation
403
+ - **Observable**: Comprehensive tracing and error handling
404
+
405
+ ---
406
+
407
+ ## Architecture
408
+
409
+ **Execution Pipeline:**
410
+
411
+ 1. **Prompt Generation**: Injects tools, schemas, and context into dual-mode prompts
412
+ 2. **Code Generation**: LLM generates TypeScript with tool calls and logic
413
+ 3. **Compilation**: Babel AST transformation with custom plugins (tracking, JSX, source maps)
414
+ 4. **Execution**: Runs in QuickJS WASM sandbox with full isolation
415
+ 5. **Result Processing**: Type-safe exit handling and error recovery
416
+
417
+ **Security:**
418
+
419
+ - QuickJS WASM sandbox with complete isolation (no filesystem/network access)
420
+ - Stack trace sanitization (removes internal framework details)
421
+ - Configurable memory limits and execution timeouts
422
+ - Tool-level permissions and rate limiting
423
+ - Automatic token limit handling
84
424
 
85
425
  ---
86
426
 
87
- ## 🌟 Battle-Tested at Massive Scale
427
+ ## Comparison
428
+
429
+ | Feature | LangChain / CrewAI | MCP Servers | LLMz |
430
+ | ------------------------ | ---------------------- | ---------------------- | ------------------------- |
431
+ | Tool calling | JSON | JSON | TypeScript code |
432
+ | Multi-tool orchestration | Multiple LLM calls | Multiple LLM calls | Single LLM call |
433
+ | Complex logic | Limited | Limited | Full language support |
434
+ | Type safety | Partial | Schema-based | Full TypeScript + Zui |
435
+ | Execution environment | Python/JS runtime | Cross-process | QuickJS WASM sandbox |
436
+ | Cost (complex workflows) | High (many roundtrips) | High (many roundtrips) | Low (one-shot generation) |
437
+ | Production scale | Varies | Emerging | Battle-tested (1M+ users) |
438
+
439
+ ---
440
+
441
+ ## Examples
442
+
443
+ Check out the [examples folder](https://github.com/botpress/botpress/tree/master/packages/llmz/examples) for complete working examples:
444
+
445
+ | Title | Mode | Description |
446
+ | --------------------------------------------------------------------------------------------------------------------- | ------ | ---------------------------------------------------- |
447
+ | [Basic Chat](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/01_chat_basic) | Chat | Simple interactive chat with button-based navigation |
448
+ | [Chat with Exits](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/02_chat_exits) | Chat | Custom exit conditions with type-safe validation |
449
+ | [Conditional Tools](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/03_chat_conditional_tool) | Chat | Dynamic tool availability based on context |
450
+ | [Small Models](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/04_chat_small_models) | Chat | Optimized prompts for smaller language models |
451
+ | [Web Search](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/05_chat_web_search) | Chat | Integrate web search and content browsing |
452
+ | [Tool Confirmation](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/06_chat_confirm_tool) | Chat | User confirmation before executing tools |
453
+ | [Guardrails](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/07_chat_guardrails) | Chat | Safety constraints and content filtering |
454
+ | [Multi-Agent](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/08_chat_multi_agent) | Chat | Coordinating multiple agents in one system |
455
+ | [Variables](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/09_chat_variables) | Chat | Stateful properties that persist across iterations |
456
+ | [Components](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/10_chat_components) | Chat | Rich UI components for interactive experiences |
457
+ | [Minimal Worker](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/11_worker_minimal) | Worker | One-shot computational task execution |
458
+ | [File System](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/12_worker_fs) | Worker | Automated file operations with conditional logic |
459
+ | [Sandbox](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/13_worker_sandbox) | Worker | Secure isolated code execution environment |
460
+ | [Snapshots](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/14_worker_snapshot) | Worker | Pause and resume long-running workflows |
461
+ | [Stack Traces](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/15_worker_stacktraces) | Worker | Error handling and debugging patterns |
462
+ | [Tool Chaining](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/16_worker_tool_chaining) | Worker | Sequential multi-tool orchestration |
463
+ | [Error Recovery](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/17_worker_error_recovery) | Worker | Graceful failure handling and retries |
464
+ | [Security](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/18_worker_security) | Worker | Code inspection and security validation |
465
+ | [Wrap Tools](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/19_worker_wrap_tool) | Worker | Creating higher-order tool abstractions |
466
+ | [RAG](https://github.com/botpress/botpress/tree/master/packages/llmz/examples/20_chat_rag) | Chat | Retrieval-augmented generation with knowledge bases |
467
+
468
+ ---
469
+
470
+ ## Contributing
471
+
472
+ ```bash
473
+ git clone https://github.com/botpress/botpress
474
+ cd packages/llmz
475
+
476
+ pnpm install
477
+ pnpm test
478
+ pnpm build
479
+ ```
480
+
481
+ **Commands:**
482
+
483
+ - `pnpm test`: Run test suite (Vitest with LLM retries)
484
+ - `pnpm test:watch`: Watch mode for development
485
+ - `pnpm build`: Compile TypeScript and bundle (ESM + CJS)
486
+ - `pnpm generate`: Regenerate prompt templates from markdown
487
+
488
+ ---
489
+
490
+ ## License
491
+
492
+ MIT
493
+
494
+ ---
88
495
 
89
- LLMz isn't experimental. It's been driving production workloads globally:
496
+ ## Learn More
90
497
 
91
- - **1+ year** in production
92
- - **Millions** of active users
93
- - **Hundreds of thousands** of deployed agents
498
+ - [Anthropic: Code Execution with MCP](https://www.anthropic.com/engineering/code-execution-with-mcp)
499
+ - [How Code Execution Reduces Agent Costs by 98%](https://medium.com/@meshuggah22/weve-been-using-mcp-wrong-how-anthropic-reduced-ai-agent-costs-by-98-7-7c102fc22589)
500
+ - [Botpress Documentation](https://botpress.com/docs)