llmz 0.9.0 → 1.0.2
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/DOCS.md +232 -1794
- package/ERRORS.md +101 -0
- package/README.md +96 -582
- package/dist/chat/chat.d.ts +63 -0
- package/dist/chat/component.d.ts +31 -0
- package/dist/chat/component.default.d.ts +58 -0
- package/dist/chat/response.d.ts +15 -0
- package/dist/compiler/compiler.d.ts +2 -7
- package/dist/compiler/plugins/dynamic-code.d.ts +7 -0
- package/dist/compiler/plugins/replace-comment.d.ts +2 -3
- package/dist/compiler/plugins/return-async.d.ts +5 -4
- package/dist/compiler/plugins/termination.d.ts +8 -0
- package/dist/compiler/plugins/tool-resolution.d.ts +4 -0
- package/dist/compiler/plugins/variable-extraction.d.ts +2 -8
- package/dist/component-CMFoCHyu.cjs +649 -0
- package/dist/component-DncqP4fB.js +614 -0
- package/dist/context.d.ts +71 -378
- package/dist/custom-client-C-WXvLD9.js +1655 -0
- package/dist/custom-client-Cgt9_0r1.cjs +1732 -0
- package/dist/custom-client.d.ts +12 -2
- package/dist/errors/catalog.d.ts +294 -0
- package/dist/errors/hooks.d.ts +2 -0
- package/dist/errors-C6ppQUZX.cjs +893 -0
- package/dist/errors-Crk5P8jo.js +636 -0
- package/dist/errors.d.ts +24 -36
- package/dist/execute-CiYvPs5Q.js +2061 -0
- package/dist/execute-DTTfacsi.cjs +2062 -0
- package/dist/{exit-DUM96DN1.cjs → exit-BOtKWPuT.cjs} +38 -28
- package/dist/{exit-CDdxDBRT.js → exit-DMPwCSba.js} +37 -27
- package/dist/exit.d.ts +19 -16
- package/dist/index.cjs +177 -1237
- package/dist/index.d.ts +25 -28
- package/dist/index.js +137 -1213
- package/dist/index.workerd.js +8974 -11145
- package/dist/inspect.d.ts +18 -4
- package/dist/inspection.d.ts +40 -0
- package/dist/native-0bq5ATZM.cjs +212 -0
- package/dist/native-n_718Z-T.js +177 -0
- package/dist/objects.d.ts +2 -57
- package/dist/prompts/native.d.ts +2 -0
- package/dist/prompts/prompt.d.ts +10 -101
- package/dist/quickjs-variant-21IO97PF.cjs +1080 -0
- package/dist/quickjs-variant-CtuGagsb.js +985 -0
- package/dist/result.d.ts +30 -861
- package/dist/runtime/execute.d.ts +3 -2
- package/dist/runtime/execution-activity.d.ts +11 -0
- package/dist/runtime/execution-diagnostics.d.ts +11 -0
- package/dist/runtime/execution-report.d.ts +51 -0
- package/dist/runtime/fixtures/chat.d.ts +8 -0
- package/dist/runtime/fixtures/long-search.d.ts +1 -1
- package/dist/runtime/fixtures/native-client.d.ts +33 -0
- package/dist/runtime/fixtures/protocol-languages.d.ts +1 -1
- package/dist/runtime/forced-inspection.d.ts +13 -0
- package/dist/runtime/forced-inspection.test.d.ts +1 -0
- package/dist/runtime/generate.d.ts +23 -20
- package/dist/runtime/generate.test.d.ts +1 -0
- package/dist/runtime/hook-provenance.test.d.ts +1 -0
- package/dist/runtime/inspection-hook.test.d.ts +1 -0
- package/dist/runtime/inspection-values.d.ts +14 -0
- package/dist/runtime/inspection-values.test.d.ts +1 -0
- package/dist/runtime/interruption-control-flow.test.d.ts +1 -0
- package/dist/runtime/interruption-settings.test.d.ts +1 -0
- package/dist/runtime/javascript-api.d.ts +62 -0
- package/dist/runtime/javascript-api.test.d.ts +1 -0
- package/dist/runtime/native-review.test.d.ts +1 -0
- package/dist/runtime/native-tools.d.ts +18 -0
- package/dist/runtime/native-tools.test.d.ts +1 -0
- package/dist/runtime/object-memory.test.d.ts +1 -0
- package/dist/runtime/single-tool.test.d.ts +1 -0
- package/dist/runtime/think-signal.test.d.ts +1 -0
- package/dist/runtime/token-budget.d.ts +22 -0
- package/dist/runtime/token-budget.test.d.ts +1 -0
- package/dist/runtime/tool-wrapper.d.ts +6 -5
- package/dist/runtime/traces.test.d.ts +1 -0
- package/dist/runtime/truncation.test.d.ts +1 -0
- package/dist/runtime/types.d.ts +47 -31
- package/dist/runtime/variable-recovery.test.d.ts +1 -0
- package/dist/runtime/vm-context.d.ts +6 -1
- package/dist/runtime/worker-completion.test.d.ts +1 -0
- package/dist/runtime-names.d.ts +4 -0
- package/dist/schema-CXlnbb3n.cjs +68 -0
- package/dist/schema-GE4ISPCR.js +45 -0
- package/dist/schema.d.ts +10 -0
- package/dist/schema.test.d.ts +1 -0
- package/dist/session/compactor.d.ts +51 -0
- package/dist/session/compactor.test.d.ts +1 -0
- package/dist/session/history.d.ts +36 -0
- package/dist/session/history.test.d.ts +1 -0
- package/dist/session/json.d.ts +3 -0
- package/dist/session/json.test.d.ts +1 -0
- package/dist/session/memory-codec.d.ts +9 -0
- package/dist/session/memory-codec.test.d.ts +1 -0
- package/dist/session/memory-render.d.ts +15 -0
- package/dist/session/memory-render.test.d.ts +1 -0
- package/dist/session/memory.d.ts +112 -0
- package/dist/session/memory.test.d.ts +1 -0
- package/dist/session/messages.d.ts +19 -0
- package/dist/session/messages.test.d.ts +1 -0
- package/dist/session/serialization.d.ts +36 -0
- package/dist/session/serialization.test.d.ts +1 -0
- package/dist/session/session-memory.test.d.ts +1 -0
- package/dist/session/session.d.ts +121 -0
- package/dist/session/session.test.d.ts +1 -0
- package/dist/session/speaker.test.d.ts +1 -0
- package/dist/{transcript.d.ts → session/transcript.d.ts} +4 -8
- package/dist/session/transcript.test.d.ts +1 -0
- package/dist/{tool-Bv8N4IPj.js → tool-DstssiAs.js} +144 -92
- package/dist/{tool-Dy6AJpJH.cjs → tool-_wK9hQOb.cjs} +146 -94
- package/dist/tool.d.ts +25 -17
- package/dist/truncate-BEKbdo3t.js +79 -0
- package/dist/truncate-uomAMGPs.cjs +102 -0
- package/dist/truncate.d.ts +24 -0
- package/dist/truncate.test.d.ts +1 -0
- package/dist/types.d.ts +33 -9
- package/dist/{typings-3lsqCbkn.cjs → typings-BA_XxAcY.cjs} +38 -193
- package/dist/{typings-CrBa7Tyy.js → typings-BEVDvtuk.js} +38 -175
- package/dist/typings-formatter.d.ts +1 -1
- package/dist/typings.d.ts +3 -0
- package/dist/{uniq-BYQ7GWwb.js → uniq-DfUeSNaX.js} +2 -2
- package/dist/{uniq-G872RWmE.cjs → uniq-DiXgysNV.cjs} +6 -18
- package/dist/{_baseIteratee-P_firB7b.js → utils-BOFzb8Ok.js} +1134 -16
- package/dist/{_baseIteratee-CF4qP2a8.cjs → utils-CTlsXvdj.cjs} +1281 -84
- package/dist/vm/drivers/node.d.ts +1 -1
- package/dist/vm/drivers/quickjs.d.ts +1 -1
- package/dist/vm/errors.d.ts +4 -4
- package/dist/vm/index.d.ts +3 -4
- package/dist/vm/instrument.d.ts +14 -6
- package/dist/vm/member-proxy.d.ts +4 -0
- package/dist/vm/member-proxy.test.d.ts +1 -0
- package/dist/vm/memory.test.d.ts +1 -0
- package/dist/vm/return-semantics.test.d.ts +1 -0
- package/dist/vm/types.d.ts +20 -3
- package/dist/{vm-BLsHw1da.js → vm-C7SfutHn.js} +1187 -180
- package/dist/{vm-D6s1zF6M.cjs → vm-gmekHc0i.cjs} +1221 -207
- package/package.json +13 -6
- package/dist/_baseForOwn-BfWDTL8L.cjs +0 -53
- package/dist/_baseForOwn-CFhWe_W6.js +0 -48
- package/dist/_getPrototype-CG7Dxrrz.js +0 -6
- package/dist/_getPrototype-DQDlNVVM.cjs +0 -10
- package/dist/_overArg-C5Hy7oCi.cjs +0 -196
- package/dist/_overArg-CRYbj1NH.js +0 -155
- package/dist/chat.d.ts +0 -401
- package/dist/common-BlRFtdf2.js +0 -807
- package/dist/common-BnvrDnIT.cjs +0 -860
- package/dist/compiler/plugins/track-tool-calls.d.ts +0 -33
- package/dist/compiler-C-OI9sim.cjs +0 -547
- package/dist/compiler-CW1VjQsn.js +0 -473
- package/dist/component-C6Agl2ns.cjs +0 -73
- package/dist/component-DGG7d_Mz.js +0 -44
- package/dist/component.d.ts +0 -88
- package/dist/component.default.d.ts +0 -278
- package/dist/custom-client-ChuZ63zp.js +0 -2658
- package/dist/custom-client-DAsGRnYc.cjs +0 -2729
- package/dist/dual-modes-DDkoCp3R.js +0 -3006
- package/dist/dual-modes-DPXiuvNe.cjs +0 -3090
- package/dist/errors-C962uJdS.cjs +0 -220
- package/dist/errors-Cg1G0Awu.js +0 -161
- package/dist/example-format.d.ts +0 -8
- package/dist/example.d.ts +0 -45
- package/dist/execute-BWcdv9so.js +0 -1059
- package/dist/execute-DM2-7y86.cjs +0 -1060
- package/dist/exit-parser.d.ts +0 -39
- package/dist/handlers.d.ts +0 -10
- package/dist/hoist.d.ts +0 -2
- package/dist/isEmpty-BhJRCqIF.js +0 -52
- package/dist/isEmpty-LY533Wmh.cjs +0 -57
- package/dist/isPlainObject-CSxXVKCk.cjs +0 -56
- package/dist/isPlainObject-D39AzPO2.js +0 -51
- package/dist/jsx-CIgdGGVs.js +0 -52
- package/dist/jsx-owqxrquU.cjs +0 -75
- package/dist/jsx.d.ts +0 -11
- package/dist/message-stream/index.d.ts +0 -9
- package/dist/message-stream/instructions.d.ts +0 -30
- package/dist/message-stream/normalizer.d.ts +0 -21
- package/dist/message-stream/parser.d.ts +0 -89
- package/dist/message-stream/registry.d.ts +0 -14
- package/dist/message-stream/replayable.d.ts +0 -27
- package/dist/message-stream/response-parser.d.ts +0 -24
- package/dist/message-stream/sanitize.d.ts +0 -10
- package/dist/message-stream/stream.d.ts +0 -54
- package/dist/message-stream/types.d.ts +0 -97
- package/dist/message-stream/validator.d.ts +0 -23
- package/dist/prompts/chat-mode/protocol.d.ts +0 -2
- package/dist/prompts/chat-mode/system.d.ts +0 -2
- package/dist/prompts/chat-mode/system.md.d.ts +0 -2
- package/dist/prompts/chat-mode/user.d.ts +0 -2
- package/dist/prompts/chat-mode/user.md.d.ts +0 -2
- package/dist/prompts/code-examples.d.ts +0 -2
- package/dist/prompts/common.d.ts +0 -7
- package/dist/prompts/dual-modes.d.ts +0 -2
- package/dist/prompts/execution-history.d.ts +0 -6
- package/dist/prompts/execution-state.d.ts +0 -3
- package/dist/prompts/protocol-basics.d.ts +0 -5
- package/dist/prompts/protocol.d.ts +0 -31
- package/dist/prompts/worker-mode/protocol.d.ts +0 -2
- package/dist/prompts/worker-mode/system.d.ts +0 -2
- package/dist/prompts/worker-mode/system.md.d.ts +0 -2
- package/dist/prompts/worker-mode/user.d.ts +0 -2
- package/dist/prompts/worker-mode/user.md.d.ts +0 -2
- package/dist/quickjs-variant-BYhMBpwU.js +0 -45
- package/dist/quickjs-variant-CsIwTVFC.cjs +0 -56
- package/dist/rolldown-runtime-EAsCxrDo.js +0 -14
- package/dist/runtime/fixtures/qwen-protocol-failures.d.ts +0 -29
- package/dist/runtime/interpret-result.d.ts +0 -22
- package/dist/snapshots.d.ts +0 -249
- package/dist/stack-traces-BIE_5Xwf.js +0 -21
- package/dist/stack-traces-ntw_qhoy.cjs +0 -26
- package/dist/truncator-CLkouTCO.cjs +0 -276
- package/dist/truncator-DZqEqnpD.js +0 -253
- package/dist/truncator.d.ts +0 -118
- package/dist/utils-3ESwP3yg.cjs +0 -1075
- package/dist/utils-UBeAFeEI.js +0 -950
- /package/dist/{citations.d.ts → chat/citations.d.ts} +0 -0
- /package/dist/{citations.test.d.ts → chat/citations.test.d.ts} +0 -0
- /package/dist/{component.default.test.d.ts → chat/component.default.test.d.ts} +0 -0
- /package/dist/{compiler/plugins/track-tool-calls.test.d.ts → chat/component.test.d.ts} +0 -0
- /package/dist/{example.test.d.ts → chat/response.test.d.ts} +0 -0
- /package/dist/{exit-parser.test.d.ts → compiler/plugins/dynamic-code.test.d.ts} +0 -0
- /package/dist/{handlers.test.d.ts → compiler/plugins/tool-resolution.test.d.ts} +0 -0
- /package/dist/{hoist.test.d.ts → errors/catalog.test.d.ts} +0 -0
- /package/dist/{message-stream/instructions.test.d.ts → exit.test.d.ts} +0 -0
- /package/dist/{message-stream/normalizer.test.d.ts → inspection.test.d.ts} +0 -0
- /package/dist/{message-stream/parser.test.d.ts → prompts/native.test.d.ts} +0 -0
- /package/dist/{message-stream/response-parser.test.d.ts → result.test.d.ts} +0 -0
- /package/dist/{message-stream/stream.test.d.ts → runtime/chat-completion.test.d.ts} +0 -0
- /package/dist/{message-stream/validator.test.d.ts → runtime/chat-delivery.test.d.ts} +0 -0
- /package/dist/{prompts/adherence.test.d.ts → runtime/chat-last-speaker.test.d.ts} +0 -0
- /package/dist/{prompts/catalogue-examples.test.d.ts → runtime/compatibility-regressions.test.d.ts} +0 -0
- /package/dist/{prompts/code-examples.test.d.ts → runtime/context-registration.test.d.ts} +0 -0
- /package/dist/{prompts/common.test.d.ts → runtime/empty-completion.test.d.ts} +0 -0
- /package/dist/{prompts/concrete-patterns.test.d.ts → runtime/error-recovery.test.d.ts} +0 -0
- /package/dist/{prompts/long-search.test.d.ts → runtime/errors.test.d.ts} +0 -0
- /package/dist/{prompts/protocol-simple.test.d.ts → runtime/execution-activity.test.d.ts} +0 -0
- /package/dist/{prompts/protocol.test.d.ts → runtime/execution-diagnostics.test.d.ts} +0 -0
- /package/dist/{prompts/sections.test.d.ts → runtime/execution-report.test.d.ts} +0 -0
- /package/dist/runtime/{interpret-result.test.d.ts → execution-safety.test.d.ts} +0 -0
- /package/dist/{transcript.test.d.ts → runtime/exit-control-flow.test.d.ts} +0 -0
- /package/dist/{truncator.test.d.ts → runtime/fixtures/long-search.test.d.ts} +0 -0
package/dist/chat.d.ts
DELETED
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
import { Component, RenderedComponent } from './component.js';
|
|
2
|
-
import { Context } from './context.js';
|
|
3
|
-
import { ValueOrGetter } from './getter.js';
|
|
4
|
-
import { ExecutionResult } from './result.js';
|
|
5
|
-
import { TranscriptArray, Transcript } from './transcript.js';
|
|
6
|
-
/**
|
|
7
|
-
* Function type for handling messages sent from the agent to the user.
|
|
8
|
-
*
|
|
9
|
-
* The handler receives rendered components from the agent and is responsible for
|
|
10
|
-
* presenting them to the user through the appropriate interface (CLI, web UI, mobile app, etc.).
|
|
11
|
-
* This is how the agent communicates with the user.
|
|
12
|
-
*
|
|
13
|
-
* @param input - The rendered component to display to the user
|
|
14
|
-
* @returns Promise that resolves when the message has been handled
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const handler: MessageHandler = async (input) => {
|
|
19
|
-
* if (isComponent(input, DefaultComponents.Text)) {
|
|
20
|
-
* console.log('Agent:', input.children.join(''))
|
|
21
|
-
* } else if (isComponent(input, DefaultComponents.Button)) {
|
|
22
|
-
* console.log('Button:', input.props.label)
|
|
23
|
-
* }
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
/** Correlates every generated send with its deltas and restart scope. */
|
|
28
|
-
export type MessageMetadata = {
|
|
29
|
-
/** Stable across generation attempts; standalone tool calls use their callId. */
|
|
30
|
-
iterationId: string;
|
|
31
|
-
/** Logical send ID; matches MessageDelta.id when text deltas exist. Unique across attempts. */
|
|
32
|
-
id: string;
|
|
33
|
-
};
|
|
34
|
-
export type MessageHandler = (input: RenderedComponent, metadata: MessageMetadata) => Promise<void> | void;
|
|
35
|
-
/**
|
|
36
|
-
* A chunk of a message body streamed live from the LLM, before the message is complete.
|
|
37
|
-
*
|
|
38
|
-
* On streaming cognitive clients, message bodies (`■send` blocks) arrive token by token.
|
|
39
|
-
* Each chunk is forwarded to {@link Chat} `onMessageDelta` as soon as it is parsed, so the
|
|
40
|
-
* client can render the message progressively (e.g. typewriter effect in a chat UI).
|
|
41
|
-
*
|
|
42
|
-
* Deltas are provisional: restart deltas retract previews from that iteration
|
|
43
|
-
* after a provider restart, malformed response, or transport failure. Completed
|
|
44
|
-
* `handler` messages and code wait for a valid response and successful transport.
|
|
45
|
-
*/
|
|
46
|
-
export type MessageDelta = {
|
|
47
|
-
restart: false;
|
|
48
|
-
/** Generation whose provisional messages must be cleared on a restart. */
|
|
49
|
-
iterationId: string;
|
|
50
|
-
/** Identifies the message being streamed. Unique within an `execute()` call. */
|
|
51
|
-
id: string;
|
|
52
|
-
/** Component name of the message being streamed (e.g. 'message', 'button'). */
|
|
53
|
-
component: string;
|
|
54
|
-
/** Props of the message being streamed. Final by the time the body starts streaming. */
|
|
55
|
-
props: Record<string, unknown>;
|
|
56
|
-
/** The new chunk of body text. */
|
|
57
|
-
delta: string;
|
|
58
|
-
/** The full body text accumulated so far, including this chunk. */
|
|
59
|
-
content: string;
|
|
60
|
-
} | {
|
|
61
|
-
/**
|
|
62
|
-
* Reset-only delta, emitted before replacement text (even if none follows).
|
|
63
|
-
* Clear provisional messages for iterationId. Other iterations are unaffected.
|
|
64
|
-
* Completed handler sends are never produced by an abandoned generation.
|
|
65
|
-
*/
|
|
66
|
-
restart: true;
|
|
67
|
-
iterationId: string;
|
|
68
|
-
/** 1-based index of the next attempt; a terminal failure may have no replacement. */
|
|
69
|
-
attempt: number;
|
|
70
|
-
fromModel: string;
|
|
71
|
-
toModel: string;
|
|
72
|
-
reason: string;
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* Function type for handling message chunks as they are streamed from the LLM.
|
|
76
|
-
* Text-preview errors are ignored. Restart-handler errors stop generation:
|
|
77
|
-
* replacement output must not be delivered if retraction fails.
|
|
78
|
-
*/
|
|
79
|
-
export type MessageDeltaHandler = (delta: MessageDelta) => Promise<void> | void;
|
|
80
|
-
/**
|
|
81
|
-
* Base class for implementing chat interfaces in LLMz agents.
|
|
82
|
-
*
|
|
83
|
-
* The Chat class provides the foundation for interactive conversational agents by defining
|
|
84
|
-
* how agents communicate with users. It manages the conversation transcript, available UI
|
|
85
|
-
* components, and message handling. This is the bridge between the agent's generated code
|
|
86
|
-
* and your user interface.
|
|
87
|
-
*
|
|
88
|
-
* ## Key Concepts
|
|
89
|
-
*
|
|
90
|
-
* **Handler Function**: The core method that receives agent messages (rendered components)
|
|
91
|
-
* and displays them to the user. This is called every time the agent wants to communicate.
|
|
92
|
-
*
|
|
93
|
-
* **Transcript**: An up-to-date conversation history that is evaluated at each iteration.
|
|
94
|
-
* The transcript must reflect the complete conversation state as the agent uses it to
|
|
95
|
-
* understand context and generate appropriate responses.
|
|
96
|
-
*
|
|
97
|
-
* **Components**: The UI elements available to the agent for creating rich interactive
|
|
98
|
-
* experiences (text, buttons, images, cards, etc.).
|
|
99
|
-
*
|
|
100
|
-
* ## Implementation Requirements
|
|
101
|
-
*
|
|
102
|
-
* To create a functional chat interface, you must:
|
|
103
|
-
* 1. Implement the MessageHandler to display agent messages to users
|
|
104
|
-
* 2. Maintain an accurate transcript of the conversation
|
|
105
|
-
* 3. Provide the available UI components (typically includes DefaultComponents)
|
|
106
|
-
* 4. Optional: Override onExecutionDone() to handle execution completion
|
|
107
|
-
*
|
|
108
|
-
* ## Basic Implementation
|
|
109
|
-
*
|
|
110
|
-
* ```typescript
|
|
111
|
-
* import { Chat, DefaultComponents, isComponent } from 'llmz'
|
|
112
|
-
*
|
|
113
|
-
* class MyChat extends Chat {
|
|
114
|
-
* private messages: Array<{role: 'user' | 'assistant', content: string}> = []
|
|
115
|
-
*
|
|
116
|
-
* constructor() {
|
|
117
|
-
* super({
|
|
118
|
-
* // Handle agent messages - this is called for every agent output
|
|
119
|
-
* handler: async (input) => {
|
|
120
|
-
* if (isComponent(input, DefaultComponents.Text)) {
|
|
121
|
-
* const text = input.children.join('')
|
|
122
|
-
* console.log('Agent:', text)
|
|
123
|
-
* this.messages.push({ role: 'assistant', content: text })
|
|
124
|
-
* } else if (isComponent(input, DefaultComponents.Button)) {
|
|
125
|
-
* console.log('Button:', input.props.label)
|
|
126
|
-
* }
|
|
127
|
-
* },
|
|
128
|
-
*
|
|
129
|
-
* // Provide available components - agent can use these in generated code
|
|
130
|
-
* components: [DefaultComponents.Text, DefaultComponents.Button],
|
|
131
|
-
*
|
|
132
|
-
* // Return current transcript - evaluated at each iteration
|
|
133
|
-
* transcript: () => this.messages,
|
|
134
|
-
* })
|
|
135
|
-
* }
|
|
136
|
-
*
|
|
137
|
-
* // Add user message to transcript
|
|
138
|
-
* addUserMessage(content: string) {
|
|
139
|
-
* this.messages.push({ role: 'user', content })
|
|
140
|
-
* }
|
|
141
|
-
* }
|
|
142
|
-
*
|
|
143
|
-
* // Usage
|
|
144
|
-
* const chat = new MyChat()
|
|
145
|
-
* chat.addUserMessage('Hello!')
|
|
146
|
-
*
|
|
147
|
-
* const result = await execute({
|
|
148
|
-
* instructions: 'You are a helpful assistant',
|
|
149
|
-
* chat, // Enable chat mode
|
|
150
|
-
* client,
|
|
151
|
-
* })
|
|
152
|
-
* ```
|
|
153
|
-
*
|
|
154
|
-
* ## Advanced Implementation (CLIChat Example)
|
|
155
|
-
*
|
|
156
|
-
* ```typescript
|
|
157
|
-
* import { Chat, DefaultComponents, ListenExit } from 'llmz'
|
|
158
|
-
*
|
|
159
|
-
* class CLIChat extends Chat {
|
|
160
|
-
* public transcript: Array<{role: 'user' | 'assistant', content: string}> = []
|
|
161
|
-
* private buttons: string[] = []
|
|
162
|
-
* public result?: ExecutionResult
|
|
163
|
-
*
|
|
164
|
-
* constructor() {
|
|
165
|
-
* super({
|
|
166
|
-
* // Dynamic component list with custom renderers
|
|
167
|
-
* components: () => [
|
|
168
|
-
* DefaultComponents.Text,
|
|
169
|
-
* DefaultComponents.Button,
|
|
170
|
-
* ...this.customComponents
|
|
171
|
-
* ],
|
|
172
|
-
*
|
|
173
|
-
* // Dynamic transcript access
|
|
174
|
-
* transcript: () => this.transcript,
|
|
175
|
-
*
|
|
176
|
-
* // Sophisticated message handling
|
|
177
|
-
* handler: async (input) => {
|
|
178
|
-
* if (isComponent(input, DefaultComponents.Text)) {
|
|
179
|
-
* const text = input.children.join('')
|
|
180
|
-
* console.log('🤖 Agent:', text)
|
|
181
|
-
* this.transcript.push({ role: 'assistant', content: text })
|
|
182
|
-
* } else if (isComponent(input, DefaultComponents.Button)) {
|
|
183
|
-
* this.buttons.push(input.props.label)
|
|
184
|
-
* }
|
|
185
|
-
* },
|
|
186
|
-
* })
|
|
187
|
-
* }
|
|
188
|
-
*
|
|
189
|
-
* // Handle execution completion
|
|
190
|
-
* onExecutionDone(result: ExecutionResult) {
|
|
191
|
-
* this.result = result
|
|
192
|
-
* }
|
|
193
|
-
*
|
|
194
|
-
* // Check if agent is waiting for user input
|
|
195
|
-
* isWaitingForInput(): boolean {
|
|
196
|
-
* return this.result?.is(ListenExit) ?? false
|
|
197
|
-
* }
|
|
198
|
-
*
|
|
199
|
-
* // Conversation loop
|
|
200
|
-
* async iterate(): Promise<boolean> {
|
|
201
|
-
* if (this.isWaitingForInput()) {
|
|
202
|
-
* const userInput = await this.promptUser()
|
|
203
|
-
* this.transcript.push({ role: 'user', content: userInput })
|
|
204
|
-
* return true // Continue conversation
|
|
205
|
-
* }
|
|
206
|
-
* return false // End conversation
|
|
207
|
-
* }
|
|
208
|
-
* }
|
|
209
|
-
* ```
|
|
210
|
-
*
|
|
211
|
-
* ## Component System
|
|
212
|
-
*
|
|
213
|
-
* The agent generates JSX code using available components:
|
|
214
|
-
*
|
|
215
|
-
* ```typescript
|
|
216
|
-
* // Agent generates this TSX code:
|
|
217
|
-
* yield <Text>Welcome! Choose an option:</Text>
|
|
218
|
-
* yield <Button action="postback" label="Get Help" value="help" />
|
|
219
|
-
* yield <Button action="postback" label="Contact Sales" value="sales" />
|
|
220
|
-
*
|
|
221
|
-
* // Your handler receives these as RenderedComponent objects
|
|
222
|
-
* ```
|
|
223
|
-
*
|
|
224
|
-
* ## Transcript Management
|
|
225
|
-
*
|
|
226
|
-
* The transcript is critical for agent context and must be kept up-to-date:
|
|
227
|
-
*
|
|
228
|
-
* ```typescript
|
|
229
|
-
* // ❌ Bad - static transcript
|
|
230
|
-
* transcript: [{ role: 'user', content: 'Hello' }]
|
|
231
|
-
*
|
|
232
|
-
* // ✅ Good - dynamic transcript that reflects current state
|
|
233
|
-
* transcript: () => this.messages
|
|
234
|
-
*
|
|
235
|
-
* // The transcript is evaluated at each iteration, so the agent
|
|
236
|
-
* // always sees the current conversation state
|
|
237
|
-
* ```
|
|
238
|
-
*
|
|
239
|
-
* ## Custom Components
|
|
240
|
-
*
|
|
241
|
-
* Extend the UI with custom components for your specific use case:
|
|
242
|
-
*
|
|
243
|
-
* ```typescript
|
|
244
|
-
* import { Component } from 'llmz'
|
|
245
|
-
*
|
|
246
|
-
* const ProductCard = new Component({
|
|
247
|
-
* type: 'leaf',
|
|
248
|
-
* name: 'ProductCard',
|
|
249
|
-
* leaf: {
|
|
250
|
-
* props: z.object({
|
|
251
|
-
* name: z.string(),
|
|
252
|
-
* price: z.number(),
|
|
253
|
-
* imageUrl: z.string(),
|
|
254
|
-
* })
|
|
255
|
-
* }
|
|
256
|
-
* })
|
|
257
|
-
*
|
|
258
|
-
* class ShoppingChat extends Chat {
|
|
259
|
-
* constructor() {
|
|
260
|
-
* super({
|
|
261
|
-
* components: [
|
|
262
|
-
* ...DefaultComponents,
|
|
263
|
-
* ProductCard, // Add custom component
|
|
264
|
-
* ],
|
|
265
|
-
* handler: async (input) => {
|
|
266
|
-
* if (isComponent(input, ProductCard)) {
|
|
267
|
-
* // Handle custom component rendering
|
|
268
|
-
* this.renderProduct(input.props)
|
|
269
|
-
* }
|
|
270
|
-
* // ... handle other components
|
|
271
|
-
* },
|
|
272
|
-
* })
|
|
273
|
-
* }
|
|
274
|
-
* }
|
|
275
|
-
* ```
|
|
276
|
-
*
|
|
277
|
-
* @see {@link DefaultComponents} For standard UI components
|
|
278
|
-
* @see {@link https://github.com/botpress/botpress/blob/master/packages/llmz/examples/utils/cli-chat.ts} Complete CLIChat implementation
|
|
279
|
-
*/
|
|
280
|
-
export declare class Chat {
|
|
281
|
-
readonly handler: MessageHandler;
|
|
282
|
-
readonly onMessageDelta?: MessageDeltaHandler;
|
|
283
|
-
readonly transcript: ValueOrGetter<TranscriptArray, Context>;
|
|
284
|
-
readonly components: ValueOrGetter<Component[], Context>;
|
|
285
|
-
/**
|
|
286
|
-
* Creates a new Chat instance.
|
|
287
|
-
*
|
|
288
|
-
* @param props - Chat configuration
|
|
289
|
-
* @param props.handler - Function to handle agent messages (called for every agent output)
|
|
290
|
-
* @param props.components - Available UI components (static array or dynamic function)
|
|
291
|
-
* @param props.transcript - Conversation history (static array or dynamic function, defaults to empty)
|
|
292
|
-
*
|
|
293
|
-
* @example
|
|
294
|
-
* ```typescript
|
|
295
|
-
* // Basic chat with static configuration
|
|
296
|
-
* const chat = new Chat({
|
|
297
|
-
* handler: async (input) => {
|
|
298
|
-
* if (isComponent(input, DefaultComponents.Text)) {
|
|
299
|
-
* console.log('Agent:', input.children.join(''))
|
|
300
|
-
* }
|
|
301
|
-
* },
|
|
302
|
-
* components: [DefaultComponents.Text, DefaultComponents.Button],
|
|
303
|
-
* transcript: [
|
|
304
|
-
* { role: 'user', content: 'Hello', timestamp: Date.now() }
|
|
305
|
-
* ],
|
|
306
|
-
* })
|
|
307
|
-
* ```
|
|
308
|
-
*
|
|
309
|
-
* @example
|
|
310
|
-
* ```typescript
|
|
311
|
-
* // Dynamic chat with functions for real-time updates
|
|
312
|
-
* class MyChat extends Chat {
|
|
313
|
-
* private messages: Transcript.Message[] = []
|
|
314
|
-
*
|
|
315
|
-
* constructor() {
|
|
316
|
-
* super({
|
|
317
|
-
* handler: (input) => this.handleMessage(input),
|
|
318
|
-
*
|
|
319
|
-
* // Dynamic components - can change during execution
|
|
320
|
-
* components: () => [
|
|
321
|
-
* DefaultComponents.Text,
|
|
322
|
-
* DefaultComponents.Button,
|
|
323
|
-
* ...this.getCustomComponents()
|
|
324
|
-
* ],
|
|
325
|
-
*
|
|
326
|
-
* // Dynamic transcript - always reflects current state
|
|
327
|
-
* transcript: () => this.messages,
|
|
328
|
-
* })
|
|
329
|
-
* }
|
|
330
|
-
* }
|
|
331
|
-
* ```
|
|
332
|
-
*/
|
|
333
|
-
constructor(props: {
|
|
334
|
-
handler: MessageHandler;
|
|
335
|
-
/**
|
|
336
|
-
* Called with each message body chunk as it is streamed from the LLM (streaming
|
|
337
|
-
* clients only). Enables progressive rendering; the complete message is still
|
|
338
|
-
* delivered to `handler` once fully parsed.
|
|
339
|
-
*/
|
|
340
|
-
onMessageDelta?: MessageDeltaHandler;
|
|
341
|
-
components: ValueOrGetter<Component[], Context>;
|
|
342
|
-
transcript?: ValueOrGetter<Transcript.Message[], Context>;
|
|
343
|
-
});
|
|
344
|
-
/**
|
|
345
|
-
* Called when an execution cycle completes, regardless of the outcome.
|
|
346
|
-
*
|
|
347
|
-
* Override this method to handle execution results, manage conversation flow,
|
|
348
|
-
* or perform cleanup tasks. This is called after each `execute()` call completes,
|
|
349
|
-
* whether it succeeds, fails, or is interrupted.
|
|
350
|
-
*
|
|
351
|
-
* @param result - The execution result containing status, iterations, and exit information
|
|
352
|
-
*
|
|
353
|
-
* @example
|
|
354
|
-
* ```typescript
|
|
355
|
-
* class MyChat extends Chat {
|
|
356
|
-
* public result?: ExecutionResult
|
|
357
|
-
*
|
|
358
|
-
* onExecutionDone(result: ExecutionResult) {
|
|
359
|
-
* // Store result for conversation flow control
|
|
360
|
-
* this.result = result
|
|
361
|
-
*
|
|
362
|
-
* // Handle different result types
|
|
363
|
-
* if (result.isSuccess()) {
|
|
364
|
-
* console.log('✅ Execution completed successfully')
|
|
365
|
-
* console.log('Exit:', result.output.exit_name)
|
|
366
|
-
* } else if (result.isError()) {
|
|
367
|
-
* console.error('❌ Execution failed:', result.output.error)
|
|
368
|
-
* } else if (result.isInterrupted()) {
|
|
369
|
-
* console.log('⏸️ Execution interrupted (partial result)')
|
|
370
|
-
* }
|
|
371
|
-
* }
|
|
372
|
-
*
|
|
373
|
-
* // Use stored result for conversation flow
|
|
374
|
-
* isWaitingForInput(): boolean {
|
|
375
|
-
* return this.result?.is(ListenExit) ?? false
|
|
376
|
-
* }
|
|
377
|
-
* }
|
|
378
|
-
* ```
|
|
379
|
-
*
|
|
380
|
-
* @example
|
|
381
|
-
* ```typescript
|
|
382
|
-
* // CLIChat implementation example
|
|
383
|
-
* class CLIChat extends Chat {
|
|
384
|
-
* public status?: IterationStatus
|
|
385
|
-
* public result?: ExecutionResult
|
|
386
|
-
*
|
|
387
|
-
* onExecutionDone(result: ExecutionResult) {
|
|
388
|
-
* this.result = result
|
|
389
|
-
* this.status = result.iterations.at(-1)?.status
|
|
390
|
-
* }
|
|
391
|
-
*
|
|
392
|
-
* // Check if agent exited with specific exit type
|
|
393
|
-
* hasExitedWith<R>(exit: Exit<R>): boolean {
|
|
394
|
-
* return this.status?.type === 'exit_success' &&
|
|
395
|
-
* this.status.exit_success.exit_name === exit.name
|
|
396
|
-
* }
|
|
397
|
-
* }
|
|
398
|
-
* ```
|
|
399
|
-
*/
|
|
400
|
-
onExecutionDone(_result: ExecutionResult): void;
|
|
401
|
-
}
|