mojentic 1.0.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/LICENSE +21 -0
- package/README.md +459 -0
- package/dist/agents/async-aggregator-agent.d.ts +101 -0
- package/dist/agents/async-aggregator-agent.d.ts.map +1 -0
- package/dist/agents/async-aggregator-agent.js +160 -0
- package/dist/agents/async-aggregator-agent.js.map +1 -0
- package/dist/agents/async-dispatcher.d.ts +98 -0
- package/dist/agents/async-dispatcher.d.ts.map +1 -0
- package/dist/agents/async-dispatcher.js +173 -0
- package/dist/agents/async-dispatcher.js.map +1 -0
- package/dist/agents/async-llm-agent-with-memory.d.ts +95 -0
- package/dist/agents/async-llm-agent-with-memory.d.ts.map +1 -0
- package/dist/agents/async-llm-agent-with-memory.js +136 -0
- package/dist/agents/async-llm-agent-with-memory.js.map +1 -0
- package/dist/agents/async-llm-agent.d.ts +85 -0
- package/dist/agents/async-llm-agent.d.ts.map +1 -0
- package/dist/agents/async-llm-agent.js +87 -0
- package/dist/agents/async-llm-agent.js.map +1 -0
- package/dist/agents/base-agent.d.ts +59 -0
- package/dist/agents/base-agent.d.ts.map +1 -0
- package/dist/agents/base-agent.js +24 -0
- package/dist/agents/base-agent.js.map +1 -0
- package/dist/agents/base-async-agent.d.ts +42 -0
- package/dist/agents/base-async-agent.d.ts.map +1 -0
- package/dist/agents/base-async-agent.js +6 -0
- package/dist/agents/base-async-agent.js.map +1 -0
- package/dist/agents/event.d.ts +26 -0
- package/dist/agents/event.d.ts.map +1 -0
- package/dist/agents/event.js +13 -0
- package/dist/agents/event.js.map +1 -0
- package/dist/agents/index.d.ts +14 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +30 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/iterative-problem-solver.d.ts +83 -0
- package/dist/agents/iterative-problem-solver.d.ts.map +1 -0
- package/dist/agents/iterative-problem-solver.js +120 -0
- package/dist/agents/iterative-problem-solver.js.map +1 -0
- package/dist/agents/router.d.ts +44 -0
- package/dist/agents/router.d.ts.map +1 -0
- package/dist/agents/router.js +53 -0
- package/dist/agents/router.js.map +1 -0
- package/dist/agents/simple-recursive-agent.d.ts +182 -0
- package/dist/agents/simple-recursive-agent.d.ts.map +1 -0
- package/dist/agents/simple-recursive-agent.js +272 -0
- package/dist/agents/simple-recursive-agent.js.map +1 -0
- package/dist/context/index.d.ts +5 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +21 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/shared-working-memory.d.ts +76 -0
- package/dist/context/shared-working-memory.d.ts.map +1 -0
- package/dist/context/shared-working-memory.js +121 -0
- package/dist/context/shared-working-memory.js.map +1 -0
- package/dist/error.d.ts +93 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +149 -0
- package/dist/error.js.map +1 -0
- package/dist/examples/react/decisioning-agent.d.ts +48 -0
- package/dist/examples/react/decisioning-agent.d.ts.map +1 -0
- package/dist/examples/react/decisioning-agent.js +204 -0
- package/dist/examples/react/decisioning-agent.js.map +1 -0
- package/dist/examples/react/events.d.ts +77 -0
- package/dist/examples/react/events.d.ts.map +1 -0
- package/dist/examples/react/events.js +9 -0
- package/dist/examples/react/events.js.map +1 -0
- package/dist/examples/react/formatters.d.ts +23 -0
- package/dist/examples/react/formatters.d.ts.map +1 -0
- package/dist/examples/react/formatters.js +68 -0
- package/dist/examples/react/formatters.js.map +1 -0
- package/dist/examples/react/index.d.ts +12 -0
- package/dist/examples/react/index.d.ts.map +1 -0
- package/dist/examples/react/index.js +28 -0
- package/dist/examples/react/index.js.map +1 -0
- package/dist/examples/react/models.d.ts +57 -0
- package/dist/examples/react/models.d.ts.map +1 -0
- package/dist/examples/react/models.js +19 -0
- package/dist/examples/react/models.js.map +1 -0
- package/dist/examples/react/output-agent.d.ts +23 -0
- package/dist/examples/react/output-agent.d.ts.map +1 -0
- package/dist/examples/react/output-agent.js +28 -0
- package/dist/examples/react/output-agent.js.map +1 -0
- package/dist/examples/react/summarization-agent.d.ts +46 -0
- package/dist/examples/react/summarization-agent.d.ts.map +1 -0
- package/dist/examples/react/summarization-agent.js +102 -0
- package/dist/examples/react/summarization-agent.js.map +1 -0
- package/dist/examples/react/thinking-agent.d.ts +47 -0
- package/dist/examples/react/thinking-agent.d.ts.map +1 -0
- package/dist/examples/react/thinking-agent.js +127 -0
- package/dist/examples/react/thinking-agent.js.map +1 -0
- package/dist/examples/react/tool-call-agent.d.ts +25 -0
- package/dist/examples/react/tool-call-agent.d.ts.map +1 -0
- package/dist/examples/react/tool-call-agent.js +83 -0
- package/dist/examples/react/tool-call-agent.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/agent.d.ts +68 -0
- package/dist/llm/agent.d.ts.map +1 -0
- package/dist/llm/agent.js +85 -0
- package/dist/llm/agent.js.map +1 -0
- package/dist/llm/broker.d.ts +150 -0
- package/dist/llm/broker.d.ts.map +1 -0
- package/dist/llm/broker.js +355 -0
- package/dist/llm/broker.js.map +1 -0
- package/dist/llm/chat-session.d.ts +98 -0
- package/dist/llm/chat-session.d.ts.map +1 -0
- package/dist/llm/chat-session.js +156 -0
- package/dist/llm/chat-session.js.map +1 -0
- package/dist/llm/gateway.d.ts +28 -0
- package/dist/llm/gateway.d.ts.map +1 -0
- package/dist/llm/gateway.js +6 -0
- package/dist/llm/gateway.js.map +1 -0
- package/dist/llm/gateways/index.d.ts +9 -0
- package/dist/llm/gateways/index.d.ts.map +1 -0
- package/dist/llm/gateways/index.js +25 -0
- package/dist/llm/gateways/index.js.map +1 -0
- package/dist/llm/gateways/ollama.d.ts +30 -0
- package/dist/llm/gateways/ollama.d.ts.map +1 -0
- package/dist/llm/gateways/ollama.js +322 -0
- package/dist/llm/gateways/ollama.js.map +1 -0
- package/dist/llm/gateways/openai-messages-adapter.d.ts +29 -0
- package/dist/llm/gateways/openai-messages-adapter.d.ts.map +1 -0
- package/dist/llm/gateways/openai-messages-adapter.js +188 -0
- package/dist/llm/gateways/openai-messages-adapter.js.map +1 -0
- package/dist/llm/gateways/openai-model-registry.d.ts +82 -0
- package/dist/llm/gateways/openai-model-registry.d.ts.map +1 -0
- package/dist/llm/gateways/openai-model-registry.js +352 -0
- package/dist/llm/gateways/openai-model-registry.js.map +1 -0
- package/dist/llm/gateways/openai.d.ts +40 -0
- package/dist/llm/gateways/openai.d.ts.map +1 -0
- package/dist/llm/gateways/openai.js +469 -0
- package/dist/llm/gateways/openai.js.map +1 -0
- package/dist/llm/gateways/tokenizerGateway.d.ts +61 -0
- package/dist/llm/gateways/tokenizerGateway.d.ts.map +1 -0
- package/dist/llm/gateways/tokenizerGateway.js +75 -0
- package/dist/llm/gateways/tokenizerGateway.js.map +1 -0
- package/dist/llm/index.d.ts +11 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +27 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/models.d.ts +95 -0
- package/dist/llm/models.d.ts.map +1 -0
- package/dist/llm/models.js +50 -0
- package/dist/llm/models.js.map +1 -0
- package/dist/llm/tools/ask-user.d.ts +39 -0
- package/dist/llm/tools/ask-user.d.ts.map +1 -0
- package/dist/llm/tools/ask-user.js +111 -0
- package/dist/llm/tools/ask-user.js.map +1 -0
- package/dist/llm/tools/current-datetime.d.ts +17 -0
- package/dist/llm/tools/current-datetime.d.ts.map +1 -0
- package/dist/llm/tools/current-datetime.js +76 -0
- package/dist/llm/tools/current-datetime.js.map +1 -0
- package/dist/llm/tools/date-resolver.d.ts +17 -0
- package/dist/llm/tools/date-resolver.d.ts.map +1 -0
- package/dist/llm/tools/date-resolver.js +135 -0
- package/dist/llm/tools/date-resolver.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/append-task.d.ts +13 -0
- package/dist/llm/tools/ephemeral-task-manager/append-task.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/append-task.js +54 -0
- package/dist/llm/tools/ephemeral-task-manager/append-task.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/clear-tasks.d.ts +13 -0
- package/dist/llm/tools/ephemeral-task-manager/clear-tasks.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/clear-tasks.js +37 -0
- package/dist/llm/tools/ephemeral-task-manager/clear-tasks.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/complete-task.d.ts +15 -0
- package/dist/llm/tools/ephemeral-task-manager/complete-task.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/complete-task.js +56 -0
- package/dist/llm/tools/ephemeral-task-manager/complete-task.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/index.d.ts +44 -0
- package/dist/llm/tools/ephemeral-task-manager/index.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/index.js +73 -0
- package/dist/llm/tools/ephemeral-task-manager/index.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/insert-task-after.d.ts +13 -0
- package/dist/llm/tools/ephemeral-task-manager/insert-task-after.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/insert-task-after.js +59 -0
- package/dist/llm/tools/ephemeral-task-manager/insert-task-after.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/list-tasks.d.ts +14 -0
- package/dist/llm/tools/ephemeral-task-manager/list-tasks.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/list-tasks.js +45 -0
- package/dist/llm/tools/ephemeral-task-manager/list-tasks.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/prepend-task.d.ts +13 -0
- package/dist/llm/tools/ephemeral-task-manager/prepend-task.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/prepend-task.js +54 -0
- package/dist/llm/tools/ephemeral-task-manager/prepend-task.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/start-task.d.ts +15 -0
- package/dist/llm/tools/ephemeral-task-manager/start-task.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/start-task.js +56 -0
- package/dist/llm/tools/ephemeral-task-manager/start-task.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/task-list.d.ts +68 -0
- package/dist/llm/tools/ephemeral-task-manager/task-list.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/task-list.js +120 -0
- package/dist/llm/tools/ephemeral-task-manager/task-list.js.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/task.d.ts +21 -0
- package/dist/llm/tools/ephemeral-task-manager/task.d.ts.map +1 -0
- package/dist/llm/tools/ephemeral-task-manager/task.js +24 -0
- package/dist/llm/tools/ephemeral-task-manager/task.js.map +1 -0
- package/dist/llm/tools/file-manager.d.ts +127 -0
- package/dist/llm/tools/file-manager.d.ts.map +1 -0
- package/dist/llm/tools/file-manager.js +598 -0
- package/dist/llm/tools/file-manager.js.map +1 -0
- package/dist/llm/tools/index.d.ts +11 -0
- package/dist/llm/tools/index.d.ts.map +1 -0
- package/dist/llm/tools/index.js +27 -0
- package/dist/llm/tools/index.js.map +1 -0
- package/dist/llm/tools/tell-user.d.ts +31 -0
- package/dist/llm/tools/tell-user.d.ts.map +1 -0
- package/dist/llm/tools/tell-user.js +57 -0
- package/dist/llm/tools/tell-user.js.map +1 -0
- package/dist/llm/tools/tool-wrapper.d.ts +54 -0
- package/dist/llm/tools/tool-wrapper.d.ts.map +1 -0
- package/dist/llm/tools/tool-wrapper.js +91 -0
- package/dist/llm/tools/tool-wrapper.js.map +1 -0
- package/dist/llm/tools/tool.d.ts +70 -0
- package/dist/llm/tools/tool.d.ts.map +1 -0
- package/dist/llm/tools/tool.js +19 -0
- package/dist/llm/tools/tool.js.map +1 -0
- package/dist/llm/tools/web-search-tool.d.ts +35 -0
- package/dist/llm/tools/web-search-tool.d.ts.map +1 -0
- package/dist/llm/tools/web-search-tool.js +105 -0
- package/dist/llm/tools/web-search-tool.js.map +1 -0
- package/dist/llm/utils/image.d.ts +30 -0
- package/dist/llm/utils/image.d.ts.map +1 -0
- package/dist/llm/utils/image.js +65 -0
- package/dist/llm/utils/image.js.map +1 -0
- package/dist/tracer/eventStore.d.ts +101 -0
- package/dist/tracer/eventStore.d.ts.map +1 -0
- package/dist/tracer/eventStore.js +120 -0
- package/dist/tracer/eventStore.js.map +1 -0
- package/dist/tracer/index.d.ts +8 -0
- package/dist/tracer/index.d.ts.map +1 -0
- package/dist/tracer/index.js +24 -0
- package/dist/tracer/index.js.map +1 -0
- package/dist/tracer/nullTracer.d.ts +127 -0
- package/dist/tracer/nullTracer.d.ts.map +1 -0
- package/dist/tracer/nullTracer.js +148 -0
- package/dist/tracer/nullTracer.js.map +1 -0
- package/dist/tracer/tracerEvents.d.ts +209 -0
- package/dist/tracer/tracerEvents.d.ts.map +1 -0
- package/dist/tracer/tracerEvents.js +312 -0
- package/dist/tracer/tracerEvents.js.map +1 -0
- package/dist/tracer/tracerSystem.d.ts +149 -0
- package/dist/tracer/tracerSystem.d.ts.map +1 -0
- package/dist/tracer/tracerSystem.js +196 -0
- package/dist/tracer/tracerSystem.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Broker - Main interface for LLM interactions with tool support
|
|
3
|
+
*/
|
|
4
|
+
import { LlmGateway } from './gateway';
|
|
5
|
+
import { LlmMessage, CompletionConfig } from './models';
|
|
6
|
+
import { LlmTool } from './tools';
|
|
7
|
+
import { Result } from '../error';
|
|
8
|
+
import { TracerSystem } from '../tracer';
|
|
9
|
+
/**
|
|
10
|
+
* Main broker for LLM interactions with automatic tool execution and streaming support.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const gateway = new OllamaGateway();
|
|
15
|
+
* const broker = new LlmBroker('qwen3:32b', gateway);
|
|
16
|
+
*
|
|
17
|
+
* const result = await broker.generate(
|
|
18
|
+
* [Message.user('What is TypeScript?')],
|
|
19
|
+
* [new DateResolverTool()]
|
|
20
|
+
* );
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class LlmBroker {
|
|
24
|
+
private readonly model;
|
|
25
|
+
private readonly gateway;
|
|
26
|
+
private readonly tracer?;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new LLM broker instance.
|
|
29
|
+
*
|
|
30
|
+
* @param model - The model name to use (e.g., 'qwen3:32b', 'gpt-4')
|
|
31
|
+
* @param gateway - The gateway implementation for the LLM provider
|
|
32
|
+
* @param tracer - Optional tracer system for recording LLM calls and responses
|
|
33
|
+
*/
|
|
34
|
+
constructor(model: string, gateway: LlmGateway, tracer?: TracerSystem);
|
|
35
|
+
/**
|
|
36
|
+
* Generate a text completion from the LLM with automatic recursive tool execution.
|
|
37
|
+
*
|
|
38
|
+
* When the LLM requests tool calls, this method automatically executes them and
|
|
39
|
+
* recursively calls the LLM with the results until a final text response is obtained.
|
|
40
|
+
*
|
|
41
|
+
* @param messages - Conversation history as an array of messages
|
|
42
|
+
* @param tools - Optional array of tools the LLM can call
|
|
43
|
+
* @param config - Optional completion configuration (temperature, tokens, etc.)
|
|
44
|
+
* @param maxToolIterations - Maximum number of recursive tool call iterations (default: 10)
|
|
45
|
+
* @returns Result containing the final text response or an error
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const result = await broker.generate(
|
|
50
|
+
* [Message.user('What day is next Friday?')],
|
|
51
|
+
* [new DateResolverTool()],
|
|
52
|
+
* { temperature: 0.7 }
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* if (isOk(result)) {
|
|
56
|
+
* console.log(result.value);
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
generate(messages: LlmMessage[], tools?: LlmTool[], config?: CompletionConfig, maxToolIterations?: number, correlationId?: string): Promise<Result<string, Error>>;
|
|
61
|
+
/**
|
|
62
|
+
* Generate a structured object that conforms to a JSON schema.
|
|
63
|
+
*
|
|
64
|
+
* Forces the LLM to return a JSON object matching the provided schema.
|
|
65
|
+
* The response is automatically parsed and type-cast to the specified type.
|
|
66
|
+
*
|
|
67
|
+
* @param messages - Conversation history as an array of messages
|
|
68
|
+
* @param schema - JSON schema the response must conform to
|
|
69
|
+
* @param config - Optional completion configuration
|
|
70
|
+
* @param correlationId - UUID for tracing related events
|
|
71
|
+
* @returns Result containing the parsed object or an error
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* interface Sentiment {
|
|
76
|
+
* sentiment: 'positive' | 'negative' | 'neutral';
|
|
77
|
+
* confidence: number;
|
|
78
|
+
* }
|
|
79
|
+
*
|
|
80
|
+
* const schema = {
|
|
81
|
+
* type: 'object',
|
|
82
|
+
* properties: {
|
|
83
|
+
* sentiment: { type: 'string', enum: ['positive', 'negative', 'neutral'] },
|
|
84
|
+
* confidence: { type: 'number' }
|
|
85
|
+
* },
|
|
86
|
+
* required: ['sentiment', 'confidence']
|
|
87
|
+
* };
|
|
88
|
+
*
|
|
89
|
+
* const result = await broker.generateObject<Sentiment>(
|
|
90
|
+
* [Message.user('I love TypeScript!')],
|
|
91
|
+
* schema
|
|
92
|
+
* );
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
generateObject<T = Record<string, unknown>>(messages: LlmMessage[], schema: Record<string, unknown>, config?: CompletionConfig, correlationId?: string): Promise<Result<T, Error>>;
|
|
96
|
+
/**
|
|
97
|
+
* Generate a streaming completion with full recursive tool calling support.
|
|
98
|
+
*
|
|
99
|
+
* Yields content chunks as they arrive in real-time. When the LLM requests tool calls,
|
|
100
|
+
* this method automatically executes them and recursively streams the follow-up response.
|
|
101
|
+
* This provides immediate user feedback while still supporting complex tool workflows.
|
|
102
|
+
*
|
|
103
|
+
* @param messages - Conversation history as an array of messages
|
|
104
|
+
* @param config - Optional completion configuration
|
|
105
|
+
* @param tools - Optional array of tools the LLM can call
|
|
106
|
+
* @param correlationId - UUID for tracing related events
|
|
107
|
+
* @yields Result containing content chunks or errors
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* for await (const chunk of broker.generateStream(
|
|
112
|
+
* [Message.user('Tell me about tomorrow')],
|
|
113
|
+
* { temperature: 0.7 },
|
|
114
|
+
* [new DateResolverTool()]
|
|
115
|
+
* )) {
|
|
116
|
+
* if (isOk(chunk)) {
|
|
117
|
+
* process.stdout.write(chunk.value);
|
|
118
|
+
* }
|
|
119
|
+
* }
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
generateStream(messages: LlmMessage[], config?: CompletionConfig, tools?: LlmTool[], correlationId?: string): AsyncGenerator<Result<string, Error>>;
|
|
123
|
+
/**
|
|
124
|
+
* List all available models from the gateway.
|
|
125
|
+
*
|
|
126
|
+
* @returns Result containing array of model names or an error
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const result = await broker.listModels();
|
|
131
|
+
* if (isOk(result)) {
|
|
132
|
+
* console.log('Available models:', result.value);
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
listModels(): Promise<Result<string[], Error>>;
|
|
137
|
+
/**
|
|
138
|
+
* Get the current model name being used by this broker.
|
|
139
|
+
*
|
|
140
|
+
* @returns The model name string
|
|
141
|
+
*/
|
|
142
|
+
getModel(): string;
|
|
143
|
+
/**
|
|
144
|
+
* Get the gateway instance used by this broker.
|
|
145
|
+
*
|
|
146
|
+
* @returns The LlmGateway implementation
|
|
147
|
+
*/
|
|
148
|
+
getGateway(): LlmGateway;
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=broker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../src/llm/broker.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC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AE,EACjB,aAAa,CAAC,EAAE,MAAM,GACrB,cAAc,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAuKxC;;;;;;;;;;;;OAYG;IACG,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,KAAK,CAAC,CAAC;IAIpD;;;;OAIG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,UAAU,IAAI,UAAU;CAGzB"}
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LLM Broker - Main interface for LLM interactions with tool support
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.LlmBroker = void 0;
|
|
7
|
+
const models_1 = require("./models");
|
|
8
|
+
const error_1 = require("../error");
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
10
|
+
/**
|
|
11
|
+
* Main broker for LLM interactions with automatic tool execution and streaming support.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const gateway = new OllamaGateway();
|
|
16
|
+
* const broker = new LlmBroker('qwen3:32b', gateway);
|
|
17
|
+
*
|
|
18
|
+
* const result = await broker.generate(
|
|
19
|
+
* [Message.user('What is TypeScript?')],
|
|
20
|
+
* [new DateResolverTool()]
|
|
21
|
+
* );
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
class LlmBroker {
|
|
25
|
+
model;
|
|
26
|
+
gateway;
|
|
27
|
+
tracer;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new LLM broker instance.
|
|
30
|
+
*
|
|
31
|
+
* @param model - The model name to use (e.g., 'qwen3:32b', 'gpt-4')
|
|
32
|
+
* @param gateway - The gateway implementation for the LLM provider
|
|
33
|
+
* @param tracer - Optional tracer system for recording LLM calls and responses
|
|
34
|
+
*/
|
|
35
|
+
constructor(model, gateway, tracer) {
|
|
36
|
+
this.model = model;
|
|
37
|
+
this.gateway = gateway;
|
|
38
|
+
this.tracer = tracer;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Generate a text completion from the LLM with automatic recursive tool execution.
|
|
42
|
+
*
|
|
43
|
+
* When the LLM requests tool calls, this method automatically executes them and
|
|
44
|
+
* recursively calls the LLM with the results until a final text response is obtained.
|
|
45
|
+
*
|
|
46
|
+
* @param messages - Conversation history as an array of messages
|
|
47
|
+
* @param tools - Optional array of tools the LLM can call
|
|
48
|
+
* @param config - Optional completion configuration (temperature, tokens, etc.)
|
|
49
|
+
* @param maxToolIterations - Maximum number of recursive tool call iterations (default: 10)
|
|
50
|
+
* @returns Result containing the final text response or an error
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const result = await broker.generate(
|
|
55
|
+
* [Message.user('What day is next Friday?')],
|
|
56
|
+
* [new DateResolverTool()],
|
|
57
|
+
* { temperature: 0.7 }
|
|
58
|
+
* );
|
|
59
|
+
*
|
|
60
|
+
* if (isOk(result)) {
|
|
61
|
+
* console.log(result.value);
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
async generate(messages, tools, config, maxToolIterations = 10, correlationId) {
|
|
66
|
+
try {
|
|
67
|
+
const toolDescriptors = tools?.map((t) => t.descriptor());
|
|
68
|
+
const currentMessages = [...messages];
|
|
69
|
+
let iterations = 0;
|
|
70
|
+
// Generate correlationId if not provided
|
|
71
|
+
const corrId = correlationId || (0, crypto_1.randomUUID)();
|
|
72
|
+
while (iterations < maxToolIterations) {
|
|
73
|
+
// Record LLM call in tracer
|
|
74
|
+
if (this.tracer) {
|
|
75
|
+
this.tracer.recordLlmCall(this.model, currentMessages, config?.temperature ?? 1.0, toolDescriptors, corrId, 'LlmBroker.generate');
|
|
76
|
+
}
|
|
77
|
+
const startTime = Date.now();
|
|
78
|
+
const result = await this.gateway.generate(this.model, currentMessages, config, toolDescriptors);
|
|
79
|
+
const callDurationMs = Date.now() - startTime;
|
|
80
|
+
if (!(0, error_1.isOk)(result)) {
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
const response = result.value;
|
|
84
|
+
// Record LLM response in tracer
|
|
85
|
+
if (this.tracer) {
|
|
86
|
+
this.tracer.recordLlmResponse(this.model, response.content, response.toolCalls, callDurationMs, corrId, 'LlmBroker.generate');
|
|
87
|
+
}
|
|
88
|
+
// If no tool calls, we're done
|
|
89
|
+
if (!response.toolCalls || response.toolCalls.length === 0) {
|
|
90
|
+
return (0, error_1.Ok)(response.content);
|
|
91
|
+
}
|
|
92
|
+
// Add assistant message with tool calls
|
|
93
|
+
currentMessages.push(models_1.Message.assistant(response.content || '', response.toolCalls));
|
|
94
|
+
// Execute tools
|
|
95
|
+
if (!tools) {
|
|
96
|
+
return (0, error_1.Err)(new error_1.ToolError('LLM requested tool calls but no tools provided'));
|
|
97
|
+
}
|
|
98
|
+
for (const toolCall of response.toolCalls) {
|
|
99
|
+
const tool = tools.find((t) => t.matches(toolCall.function.name));
|
|
100
|
+
if (!tool) {
|
|
101
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify({ error: `Tool ${toolCall.function.name} not found` }), toolCall.id, toolCall.function.name));
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const args = JSON.parse(toolCall.function.arguments);
|
|
106
|
+
// Record tool call with timing
|
|
107
|
+
const toolStartTime = Date.now();
|
|
108
|
+
const toolResult = await tool.run(args);
|
|
109
|
+
const toolDurationMs = Date.now() - toolStartTime;
|
|
110
|
+
if (!(0, error_1.isOk)(toolResult)) {
|
|
111
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify({ error: toolResult.error.message }), toolCall.id, toolCall.function.name));
|
|
112
|
+
// Record tool call error in tracer
|
|
113
|
+
if (this.tracer) {
|
|
114
|
+
this.tracer.recordToolCall(toolCall.function.name, args, { error: toolResult.error.message }, 'LlmBroker', toolDurationMs, corrId, 'LlmBroker.generate');
|
|
115
|
+
}
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
// Record successful tool call in tracer
|
|
119
|
+
if (this.tracer) {
|
|
120
|
+
this.tracer.recordToolCall(toolCall.function.name, args, toolResult.value, 'LlmBroker', toolDurationMs, corrId, 'LlmBroker.generate');
|
|
121
|
+
}
|
|
122
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify(toolResult.value), toolCall.id, toolCall.function.name));
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify({
|
|
126
|
+
error: error instanceof Error ? error.message : String(error),
|
|
127
|
+
}), toolCall.id, toolCall.function.name));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
iterations++;
|
|
131
|
+
}
|
|
132
|
+
return (0, error_1.Err)(new error_1.ToolError(`Maximum tool iterations (${maxToolIterations}) exceeded`));
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
return (0, error_1.Err)(new Error(`Failed to generate: ${error instanceof Error ? error.message : String(error)}`));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Generate a structured object that conforms to a JSON schema.
|
|
140
|
+
*
|
|
141
|
+
* Forces the LLM to return a JSON object matching the provided schema.
|
|
142
|
+
* The response is automatically parsed and type-cast to the specified type.
|
|
143
|
+
*
|
|
144
|
+
* @param messages - Conversation history as an array of messages
|
|
145
|
+
* @param schema - JSON schema the response must conform to
|
|
146
|
+
* @param config - Optional completion configuration
|
|
147
|
+
* @param correlationId - UUID for tracing related events
|
|
148
|
+
* @returns Result containing the parsed object or an error
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* interface Sentiment {
|
|
153
|
+
* sentiment: 'positive' | 'negative' | 'neutral';
|
|
154
|
+
* confidence: number;
|
|
155
|
+
* }
|
|
156
|
+
*
|
|
157
|
+
* const schema = {
|
|
158
|
+
* type: 'object',
|
|
159
|
+
* properties: {
|
|
160
|
+
* sentiment: { type: 'string', enum: ['positive', 'negative', 'neutral'] },
|
|
161
|
+
* confidence: { type: 'number' }
|
|
162
|
+
* },
|
|
163
|
+
* required: ['sentiment', 'confidence']
|
|
164
|
+
* };
|
|
165
|
+
*
|
|
166
|
+
* const result = await broker.generateObject<Sentiment>(
|
|
167
|
+
* [Message.user('I love TypeScript!')],
|
|
168
|
+
* schema
|
|
169
|
+
* );
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
async generateObject(messages, schema, config, correlationId) {
|
|
173
|
+
try {
|
|
174
|
+
const corrId = correlationId || (0, crypto_1.randomUUID)();
|
|
175
|
+
const objectConfig = {
|
|
176
|
+
...config,
|
|
177
|
+
responseFormat: {
|
|
178
|
+
type: 'json_object',
|
|
179
|
+
schema,
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
// Record LLM call in tracer
|
|
183
|
+
if (this.tracer) {
|
|
184
|
+
this.tracer.recordLlmCall(this.model, messages, config?.temperature ?? 1.0, undefined, corrId, 'LlmBroker.generateObject');
|
|
185
|
+
}
|
|
186
|
+
const startTime = Date.now();
|
|
187
|
+
const result = await this.gateway.generate(this.model, messages, objectConfig);
|
|
188
|
+
const callDurationMs = Date.now() - startTime;
|
|
189
|
+
if (!(0, error_1.isOk)(result)) {
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
const response = result.value;
|
|
193
|
+
// Record LLM response in tracer
|
|
194
|
+
if (this.tracer) {
|
|
195
|
+
this.tracer.recordLlmResponse(this.model, response.content, undefined, callDurationMs, corrId, 'LlmBroker.generateObject');
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
const parsed = JSON.parse(response.content);
|
|
199
|
+
return (0, error_1.Ok)(parsed);
|
|
200
|
+
}
|
|
201
|
+
catch (parseError) {
|
|
202
|
+
return (0, error_1.Err)(new error_1.ParseError(`Failed to parse JSON response: ${parseError instanceof Error ? parseError.message : String(parseError)}`));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
return (0, error_1.Err)(new Error(`Failed to generate object: ${error instanceof Error ? error.message : String(error)}`));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Generate a streaming completion with full recursive tool calling support.
|
|
211
|
+
*
|
|
212
|
+
* Yields content chunks as they arrive in real-time. When the LLM requests tool calls,
|
|
213
|
+
* this method automatically executes them and recursively streams the follow-up response.
|
|
214
|
+
* This provides immediate user feedback while still supporting complex tool workflows.
|
|
215
|
+
*
|
|
216
|
+
* @param messages - Conversation history as an array of messages
|
|
217
|
+
* @param config - Optional completion configuration
|
|
218
|
+
* @param tools - Optional array of tools the LLM can call
|
|
219
|
+
* @param correlationId - UUID for tracing related events
|
|
220
|
+
* @yields Result containing content chunks or errors
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* for await (const chunk of broker.generateStream(
|
|
225
|
+
* [Message.user('Tell me about tomorrow')],
|
|
226
|
+
* { temperature: 0.7 },
|
|
227
|
+
* [new DateResolverTool()]
|
|
228
|
+
* )) {
|
|
229
|
+
* if (isOk(chunk)) {
|
|
230
|
+
* process.stdout.write(chunk.value);
|
|
231
|
+
* }
|
|
232
|
+
* }
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
async *generateStream(messages, config, tools, correlationId) {
|
|
236
|
+
const corrId = correlationId || (0, crypto_1.randomUUID)();
|
|
237
|
+
const toolDescriptors = tools?.map((t) => t.descriptor());
|
|
238
|
+
const currentMessages = [...messages];
|
|
239
|
+
let accumulatedContent = '';
|
|
240
|
+
const accumulatedToolCalls = [];
|
|
241
|
+
// Record LLM call in tracer
|
|
242
|
+
if (this.tracer) {
|
|
243
|
+
this.tracer.recordLlmCall(this.model, currentMessages, config?.temperature ?? 1.0, toolDescriptors, corrId, 'LlmBroker.generateStream');
|
|
244
|
+
}
|
|
245
|
+
const startTime = Date.now();
|
|
246
|
+
// Stream from gateway and accumulate
|
|
247
|
+
for await (const chunkResult of this.gateway.generateStream(this.model, currentMessages, config, toolDescriptors)) {
|
|
248
|
+
if (!(0, error_1.isOk)(chunkResult)) {
|
|
249
|
+
yield chunkResult;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const chunk = chunkResult.value;
|
|
253
|
+
// Yield content chunks immediately
|
|
254
|
+
if (chunk.content) {
|
|
255
|
+
accumulatedContent += chunk.content;
|
|
256
|
+
yield (0, error_1.Ok)(chunk.content);
|
|
257
|
+
}
|
|
258
|
+
// Accumulate tool calls
|
|
259
|
+
if (chunk.toolCalls) {
|
|
260
|
+
accumulatedToolCalls.push(...chunk.toolCalls);
|
|
261
|
+
}
|
|
262
|
+
// Check if stream is done
|
|
263
|
+
if (chunk.done && accumulatedToolCalls.length > 0) {
|
|
264
|
+
const callDurationMs = Date.now() - startTime;
|
|
265
|
+
// Record LLM response in tracer
|
|
266
|
+
if (this.tracer) {
|
|
267
|
+
this.tracer.recordLlmResponse(this.model, accumulatedContent, accumulatedToolCalls, callDurationMs, corrId, 'LlmBroker.generateStream');
|
|
268
|
+
}
|
|
269
|
+
// Tool calls present - execute and recursively stream
|
|
270
|
+
if (!tools) {
|
|
271
|
+
yield (0, error_1.Err)(new error_1.ToolError('LLM requested tool calls but no tools provided'));
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
// Add assistant message with tool calls
|
|
275
|
+
currentMessages.push(models_1.Message.assistant(accumulatedContent, accumulatedToolCalls));
|
|
276
|
+
// Execute all tool calls
|
|
277
|
+
for (const toolCall of accumulatedToolCalls) {
|
|
278
|
+
const tool = tools.find((t) => t.matches(toolCall.function.name));
|
|
279
|
+
if (!tool) {
|
|
280
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify({ error: `Tool ${toolCall.function.name} not found` }), toolCall.id, toolCall.function.name));
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
try {
|
|
284
|
+
const args = JSON.parse(toolCall.function.arguments);
|
|
285
|
+
const toolStartTime = Date.now();
|
|
286
|
+
const toolResult = await tool.run(args);
|
|
287
|
+
const toolDurationMs = Date.now() - toolStartTime;
|
|
288
|
+
if (!(0, error_1.isOk)(toolResult)) {
|
|
289
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify({ error: toolResult.error.message }), toolCall.id, toolCall.function.name));
|
|
290
|
+
// Record tool call error in tracer
|
|
291
|
+
if (this.tracer) {
|
|
292
|
+
this.tracer.recordToolCall(toolCall.function.name, args, { error: toolResult.error.message }, 'LlmBroker', toolDurationMs, corrId, 'LlmBroker.generateStream');
|
|
293
|
+
}
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
// Record successful tool call in tracer
|
|
297
|
+
if (this.tracer) {
|
|
298
|
+
this.tracer.recordToolCall(toolCall.function.name, args, toolResult.value, 'LlmBroker', toolDurationMs, corrId, 'LlmBroker.generateStream');
|
|
299
|
+
}
|
|
300
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify(toolResult.value), toolCall.id, toolCall.function.name));
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
currentMessages.push(models_1.Message.tool(JSON.stringify({
|
|
304
|
+
error: error instanceof Error ? error.message : String(error),
|
|
305
|
+
}), toolCall.id, toolCall.function.name));
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// Recursively stream with updated messages
|
|
309
|
+
yield* this.generateStream(currentMessages, config, tools, corrId);
|
|
310
|
+
}
|
|
311
|
+
else if (chunk.done) {
|
|
312
|
+
// Stream done without tool calls
|
|
313
|
+
const callDurationMs = Date.now() - startTime;
|
|
314
|
+
// Record LLM response in tracer
|
|
315
|
+
if (this.tracer) {
|
|
316
|
+
this.tracer.recordLlmResponse(this.model, accumulatedContent, undefined, callDurationMs, corrId, 'LlmBroker.generateStream');
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* List all available models from the gateway.
|
|
323
|
+
*
|
|
324
|
+
* @returns Result containing array of model names or an error
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* const result = await broker.listModels();
|
|
329
|
+
* if (isOk(result)) {
|
|
330
|
+
* console.log('Available models:', result.value);
|
|
331
|
+
* }
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
async listModels() {
|
|
335
|
+
return this.gateway.listModels();
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Get the current model name being used by this broker.
|
|
339
|
+
*
|
|
340
|
+
* @returns The model name string
|
|
341
|
+
*/
|
|
342
|
+
getModel() {
|
|
343
|
+
return this.model;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Get the gateway instance used by this broker.
|
|
347
|
+
*
|
|
348
|
+
* @returns The LlmGateway implementation
|
|
349
|
+
*/
|
|
350
|
+
getGateway() {
|
|
351
|
+
return this.gateway;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
exports.LlmBroker = LlmBroker;
|
|
355
|
+
//# sourceMappingURL=broker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"broker.js","sourceRoot":"","sources":["../../src/llm/broker.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAGH,qCAA2E;AAE3E,oCAAwE;AAExE,mCAAoC;AAEpC;;;;;;;;;;;;;GAaG;AACH,MAAa,SAAS;IAWD;IACA;IAXF,MAAM,CAAgB;IAEvC;;;;;;OAMG;IACH,YACmB,KAAa,EACb,OAAmB,EACpC,MAAqB;QAFJ,UAAK,GAAL,KAAK,CAAQ;QACb,YAAO,GAAP,OAAO,CAAY;QAGpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,KAAK,CAAC,QAAQ,CACZ,QAAsB,EACtB,KAAiB,EACjB,MAAyB,EACzB,oB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|
2
|
+
* ChatSession - Manages conversational state with automatic context window management
|
|
3
|
+
*/
|
|
4
|
+
import { LlmBroker } from './broker';
|
|
5
|
+
import { LlmMessage } from './models';
|
|
6
|
+
import { LlmTool } from './tools';
|
|
7
|
+
import { TokenizerGateway } from './gateways/tokenizerGateway';
|
|
8
|
+
/**
|
|
9
|
+
* Configuration options for ChatSession
|
|
10
|
+
*/
|
|
11
|
+
export interface ChatSessionConfig {
|
|
12
|
+
systemPrompt?: string;
|
|
13
|
+
tools?: LlmTool[];
|
|
14
|
+
maxContext?: number;
|
|
15
|
+
tokenizerGateway?: TokenizerGateway;
|
|
16
|
+
temperature?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Manages the state of a conversation with an LLM, automatically handling
|
|
20
|
+
* context window limits by removing older messages when needed.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const broker = new LlmBroker('qwen3:32b', new OllamaGateway());
|
|
25
|
+
* const session = new ChatSession(broker, {
|
|
26
|
+
* systemPrompt: 'You are a helpful coding assistant.',
|
|
27
|
+
* tools: [new DateResolverTool()],
|
|
28
|
+
* maxContext: 8192
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* const response = await session.send('What is TypeScript?');
|
|
32
|
+
* console.log(response);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class ChatSession {
|
|
36
|
+
private messages;
|
|
37
|
+
private readonly llm;
|
|
38
|
+
private readonly systemPrompt;
|
|
39
|
+
private readonly tools?;
|
|
40
|
+
private readonly maxContext;
|
|
41
|
+
private readonly temperature;
|
|
42
|
+
private readonly tokenizerGateway;
|
|
43
|
+
/**
|
|
44
|
+
* Create a new ChatSession instance.
|
|
45
|
+
*
|
|
46
|
+
* @param llm - The LLM broker to use for generating responses
|
|
47
|
+
* @param config - Optional configuration options
|
|
48
|
+
*/
|
|
49
|
+
constructor(llm: LlmBroker, config?: ChatSessionConfig);
|
|
50
|
+
/**
|
|
51
|
+
* Send a query to the LLM and return the response.
|
|
52
|
+
* Also records the query and response in the ongoing chat session.
|
|
53
|
+
*
|
|
54
|
+
* @param query - The query to send to the LLM
|
|
55
|
+
* @returns The response from the LLM
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const response = await session.send('Hello, how are you?');
|
|
60
|
+
* console.log(response);
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
send(query: string): Promise<string>;
|
|
64
|
+
/**
|
|
65
|
+
* Get the current message history.
|
|
66
|
+
*
|
|
67
|
+
* @returns Array of messages in the session
|
|
68
|
+
*/
|
|
69
|
+
getMessages(): LlmMessage[];
|
|
70
|
+
/**
|
|
71
|
+
* Clear all messages except the system prompt.
|
|
72
|
+
*/
|
|
73
|
+
clear(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Insert a message into the chat session.
|
|
76
|
+
* If the total token count exceeds maxContext, older messages are removed.
|
|
77
|
+
*
|
|
78
|
+
* @param message - The message to add
|
|
79
|
+
*/
|
|
80
|
+
private insertMessage;
|
|
81
|
+
/**
|
|
82
|
+
* Build a sized message with token count.
|
|
83
|
+
*
|
|
84
|
+
* @param message - The message to size
|
|
85
|
+
* @returns Message with token length
|
|
86
|
+
*/
|
|
87
|
+
private buildSizedMessage;
|
|
88
|
+
/**
|
|
89
|
+
* Ensure all messages in the history have token counts.
|
|
90
|
+
* This is needed after tool calls which may add messages without sizing.
|
|
91
|
+
*/
|
|
92
|
+
private ensureAllMessagesAreSized;
|
|
93
|
+
/**
|
|
94
|
+
* Clean up resources when done with the session.
|
|
95
|
+
*/
|
|
96
|
+
dispose(): void;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=chat-session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-session.d.ts","sourceRoot":"","sources":["../../src/llm/chat-session.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,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