llmjs2 1.0.0 → 1.0.5
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 +39 -450
- package/grapes.jpg +0 -0
- package/index.d.ts +43 -0
- package/index.js +465 -0
- package/package.json +7 -47
- package/spec.txt +73 -0
- package/test-generate-tools-suite.js +100 -0
- package/test-generate-tools.js +57 -0
- package/test-generate.js +31 -0
- package/test.js +33 -0
- package/LICENSE +0 -21
- package/dist/agent.d.ts +0 -80
- package/dist/agent.d.ts.map +0 -1
- package/dist/agent.js +0 -189
- package/dist/agent.js.map +0 -1
- package/dist/index.d.ts +0 -74
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -191
- package/dist/index.js.map +0 -1
- package/dist/providers/base.d.ts +0 -58
- package/dist/providers/base.d.ts.map +0 -1
- package/dist/providers/base.js +0 -149
- package/dist/providers/base.js.map +0 -1
- package/dist/providers/index.d.ts +0 -8
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js +0 -7
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/ollama.d.ts +0 -42
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js +0 -260
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts +0 -38
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js +0 -289
- package/dist/providers/openai.js.map +0 -1
- package/dist/types.d.ts +0 -182
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -1
- package/src/agent.ts +0 -285
- package/src/index.ts +0 -268
- package/src/providers/base.ts +0 -216
- package/src/providers/index.ts +0 -8
- package/src/providers/ollama.ts +0 -429
- package/src/providers/openai.ts +0 -485
- package/src/types.ts +0 -231
package/dist/types.d.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Unified type definitions for llmjs2
|
|
3
|
-
* Enterprise-grade LLM abstraction layer supporting OpenAI and Ollama
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Supported LLM providers
|
|
7
|
-
*/
|
|
8
|
-
export type ProviderType = 'openai' | 'ollama';
|
|
9
|
-
/**
|
|
10
|
-
* Role of a message in a conversation
|
|
11
|
-
*/
|
|
12
|
-
export type MessageRole = 'system' | 'user' | 'assistant';
|
|
13
|
-
/**
|
|
14
|
-
* A single message in a conversation
|
|
15
|
-
*/
|
|
16
|
-
export interface Message {
|
|
17
|
-
role: MessageRole;
|
|
18
|
-
content: string;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Tool/function definition for function calling
|
|
22
|
-
*/
|
|
23
|
-
export interface Tool {
|
|
24
|
-
type: 'function';
|
|
25
|
-
function: {
|
|
26
|
-
name: string;
|
|
27
|
-
description?: string;
|
|
28
|
-
parameters?: Record<string, unknown>;
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Configuration for a completion request
|
|
33
|
-
*/
|
|
34
|
-
export interface CompletionRequest {
|
|
35
|
-
/** Model identifier (e.g., 'openai/gpt-4', 'ollama/mistral') */
|
|
36
|
-
model: string;
|
|
37
|
-
/** API key or authentication token */
|
|
38
|
-
apiKey?: string;
|
|
39
|
-
/** Base URL for the API (mainly for Ollama) */
|
|
40
|
-
baseUrl?: string;
|
|
41
|
-
/** Messages for the completion */
|
|
42
|
-
messages: Message[];
|
|
43
|
-
/** Maximum tokens to generate */
|
|
44
|
-
maxTokens?: number;
|
|
45
|
-
/** Sampling temperature (0-2 for OpenAI, typically 0-1) */
|
|
46
|
-
temperature?: number;
|
|
47
|
-
/** Top-p (nucleus sampling) */
|
|
48
|
-
topP?: number;
|
|
49
|
-
/** Top-k sampling parameter */
|
|
50
|
-
topK?: number;
|
|
51
|
-
/** Frequency penalty (-2 to 2 for OpenAI) */
|
|
52
|
-
frequencyPenalty?: number;
|
|
53
|
-
/** Presence penalty (-2 to 2 for OpenAI) */
|
|
54
|
-
presencePenalty?: number;
|
|
55
|
-
/** Stop sequences */
|
|
56
|
-
stop?: string[];
|
|
57
|
-
/** Available tools for function calling */
|
|
58
|
-
tools?: Tool[];
|
|
59
|
-
/** Force tool usage */
|
|
60
|
-
toolChoice?: 'auto' | 'required' | string;
|
|
61
|
-
/** Custom headers to send with requests */
|
|
62
|
-
headers?: Record<string, string>;
|
|
63
|
-
/** Request timeout in milliseconds */
|
|
64
|
-
timeout?: number;
|
|
65
|
-
/** Retry configuration */
|
|
66
|
-
retry?: {
|
|
67
|
-
maxRetries?: number;
|
|
68
|
-
backoffMultiplier?: number;
|
|
69
|
-
initialDelayMs?: number;
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Completion response
|
|
74
|
-
*/
|
|
75
|
-
export interface CompletionResponse {
|
|
76
|
-
/** Generated text content */
|
|
77
|
-
content: string;
|
|
78
|
-
/** The model used */
|
|
79
|
-
model: string;
|
|
80
|
-
/** Stop reason */
|
|
81
|
-
stopReason?: 'stop_sequence' | 'length' | 'tool_calls' | 'end_turn' | string;
|
|
82
|
-
/** Total tokens used (if available) */
|
|
83
|
-
usage?: {
|
|
84
|
-
promptTokens?: number;
|
|
85
|
-
completionTokens?: number;
|
|
86
|
-
totalTokens?: number;
|
|
87
|
-
};
|
|
88
|
-
/** Raw provider response for advanced use cases */
|
|
89
|
-
raw?: unknown;
|
|
90
|
-
/** Finish reason from provider */
|
|
91
|
-
finishReason?: string;
|
|
92
|
-
/** Tool calls if function calling was used */
|
|
93
|
-
toolCalls?: Array<{
|
|
94
|
-
id?: string;
|
|
95
|
-
name: string;
|
|
96
|
-
arguments: Record<string, unknown>;
|
|
97
|
-
}>;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Streaming completion chunk
|
|
101
|
-
*/
|
|
102
|
-
export interface CompletionChunk {
|
|
103
|
-
/** Delta content */
|
|
104
|
-
delta: string;
|
|
105
|
-
/** Stop reason if stream ended */
|
|
106
|
-
stopReason?: string;
|
|
107
|
-
/** Usage at end of stream */
|
|
108
|
-
usage?: {
|
|
109
|
-
promptTokens?: number;
|
|
110
|
-
completionTokens?: number;
|
|
111
|
-
totalTokens?: number;
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Provider configuration options
|
|
116
|
-
*/
|
|
117
|
-
export interface ProviderConfig {
|
|
118
|
-
/** Provider type */
|
|
119
|
-
type: ProviderType;
|
|
120
|
-
/** API key */
|
|
121
|
-
apiKey?: string;
|
|
122
|
-
/** Base URL for API */
|
|
123
|
-
baseUrl?: string;
|
|
124
|
-
/** Default model */
|
|
125
|
-
model?: string;
|
|
126
|
-
/** Request timeout */
|
|
127
|
-
timeout?: number;
|
|
128
|
-
/** Retry configuration */
|
|
129
|
-
retry?: {
|
|
130
|
-
maxRetries?: number;
|
|
131
|
-
backoffMultiplier?: number;
|
|
132
|
-
initialDelayMs?: number;
|
|
133
|
-
};
|
|
134
|
-
/** Custom headers */
|
|
135
|
-
headers?: Record<string, string>;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Error response from provider
|
|
139
|
-
*/
|
|
140
|
-
export interface ProviderError extends Error {
|
|
141
|
-
name: string;
|
|
142
|
-
message: string;
|
|
143
|
-
code?: string;
|
|
144
|
-
statusCode?: number;
|
|
145
|
-
details?: unknown;
|
|
146
|
-
retryable?: boolean;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Provider interface that all providers must implement
|
|
150
|
-
*/
|
|
151
|
-
export interface IProvider {
|
|
152
|
-
/** Create a completion request */
|
|
153
|
-
complete(request: CompletionRequest): Promise<CompletionResponse>;
|
|
154
|
-
/** Stream a completion request */
|
|
155
|
-
completeStream(request: CompletionRequest): AsyncIterable<CompletionChunk>;
|
|
156
|
-
/** Validate that the configuration is correct */
|
|
157
|
-
validate(): Promise<void>;
|
|
158
|
-
/** Parse model string (e.g., 'openai/gpt-4' -> 'gpt-4') */
|
|
159
|
-
parseModel(model: string): string;
|
|
160
|
-
/** Enable or disable debug mode */
|
|
161
|
-
setDebug(debug: boolean): void;
|
|
162
|
-
/** Set custom logger function */
|
|
163
|
-
setLogger(logger: (level: string, message: string, data?: unknown) => void): void;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Global completion options
|
|
167
|
-
*/
|
|
168
|
-
export interface CompletionOptions {
|
|
169
|
-
/** Enable request logging for debugging */
|
|
170
|
-
debug?: boolean;
|
|
171
|
-
/** Custom logger function */
|
|
172
|
-
logger?: (level: string, message: string, data?: unknown) => void;
|
|
173
|
-
/** Global timeout override */
|
|
174
|
-
globalTimeout?: number;
|
|
175
|
-
/** Retry configuration override */
|
|
176
|
-
globalRetry?: {
|
|
177
|
-
maxRetries?: number;
|
|
178
|
-
backoffMultiplier?: number;
|
|
179
|
-
initialDelayMs?: number;
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAE1D;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE;QACR,IAAI,EAAE,MAAM,CAAC;QACb,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACtC,CAAC;CACH;AAED;;GAEG;AACH,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WAAW,CAAC,EAAE;QACZ,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB,CAAC;CACH"}
|
package/dist/types.js
DELETED
package/dist/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|
package/src/agent.ts
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent - Stateful conversation manager with tool support
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
completion,
|
|
7
|
-
streamCompletion,
|
|
8
|
-
CompletionRequest,
|
|
9
|
-
CompletionResponse,
|
|
10
|
-
CompletionChunk,
|
|
11
|
-
Message,
|
|
12
|
-
Tool,
|
|
13
|
-
LLMError,
|
|
14
|
-
} from './index.js';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Agent configuration
|
|
18
|
-
*/
|
|
19
|
-
export interface AgentConfig {
|
|
20
|
-
/** Model identifier (e.g., 'ollama/qwen3.5:397b-cloud') */
|
|
21
|
-
model: string;
|
|
22
|
-
|
|
23
|
-
/** API key for the provider (optional, reads from OLLAMA_CLOUD_API_KEY env by default) */
|
|
24
|
-
apiKey?: string;
|
|
25
|
-
|
|
26
|
-
/** Base URL for the API (optional, defaults to https://ollama.com) */
|
|
27
|
-
baseUrl?: string;
|
|
28
|
-
|
|
29
|
-
/** System instruction for the agent */
|
|
30
|
-
instruction?: string;
|
|
31
|
-
|
|
32
|
-
/** Available tools/functions */
|
|
33
|
-
tools?: Tool[];
|
|
34
|
-
|
|
35
|
-
/** Tool executor function - receives tool name and arguments, returns result string */
|
|
36
|
-
toolExecutor?: (toolName: string, args: Record<string, unknown>) => string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Agent generation request
|
|
41
|
-
*/
|
|
42
|
-
export interface AgentGenerateRequest {
|
|
43
|
-
/** User prompt/message */
|
|
44
|
-
userPrompt: string;
|
|
45
|
-
|
|
46
|
-
/** Optional images (base64 or URLs) */
|
|
47
|
-
images?: string[];
|
|
48
|
-
|
|
49
|
-
/** Optional reference documents or context */
|
|
50
|
-
references?: string[];
|
|
51
|
-
|
|
52
|
-
/** Additional context variables */
|
|
53
|
-
context?: Record<string, unknown>;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Agent generation response
|
|
58
|
-
*/
|
|
59
|
-
export interface AgentGenerateResponse {
|
|
60
|
-
/** Generated response */
|
|
61
|
-
response: string;
|
|
62
|
-
|
|
63
|
-
/** Full completion response from provider */
|
|
64
|
-
completion: CompletionResponse;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Stateful agent for conversations and tool use
|
|
69
|
-
*/
|
|
70
|
-
export class Agent {
|
|
71
|
-
private config: AgentConfig;
|
|
72
|
-
private conversationHistory: Message[] = [];
|
|
73
|
-
|
|
74
|
-
constructor(config: AgentConfig) {
|
|
75
|
-
if (!config.model) {
|
|
76
|
-
throw new LLMError('Model is required in agent config', 'MISSING_MODEL');
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
this.config = config;
|
|
80
|
-
|
|
81
|
-
// Initialize conversation with system instruction
|
|
82
|
-
if (config.instruction) {
|
|
83
|
-
this.conversationHistory.push({
|
|
84
|
-
role: 'system',
|
|
85
|
-
content: config.instruction,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Generate a response for a user message
|
|
92
|
-
*/
|
|
93
|
-
async generate(request: AgentGenerateRequest): Promise<AgentGenerateResponse> {
|
|
94
|
-
if (!request.userPrompt) {
|
|
95
|
-
throw new LLMError('userPrompt is required', 'MISSING_USER_PROMPT');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Build user message with context
|
|
99
|
-
let userMessage = request.userPrompt;
|
|
100
|
-
|
|
101
|
-
if (request.images && request.images.length > 0) {
|
|
102
|
-
userMessage += `\n\n[Images: ${request.images.length} image(s) attached]`;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (request.references && request.references.length > 0) {
|
|
106
|
-
userMessage += '\n\nReferences:\n' + request.references.join('\n---\n');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (request.context && Object.keys(request.context).length > 0) {
|
|
110
|
-
userMessage +=
|
|
111
|
-
'\n\nContext: ' + JSON.stringify(request.context, null, 2);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Add user message to history
|
|
115
|
-
this.conversationHistory.push({
|
|
116
|
-
role: 'user',
|
|
117
|
-
content: userMessage,
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Get initial completion
|
|
121
|
-
let completion_result = await this.getCompletion();
|
|
122
|
-
let toolCalls = completion_result.toolCalls;
|
|
123
|
-
let maxIterations = 10;
|
|
124
|
-
let iterations = 0;
|
|
125
|
-
|
|
126
|
-
// Handle tool calls in a loop
|
|
127
|
-
while (toolCalls && toolCalls.length > 0 && iterations < maxIterations) {
|
|
128
|
-
iterations++;
|
|
129
|
-
|
|
130
|
-
// Execute all tool calls
|
|
131
|
-
const toolResults: string[] = [];
|
|
132
|
-
|
|
133
|
-
for (const toolCall of toolCalls) {
|
|
134
|
-
try {
|
|
135
|
-
let result: string;
|
|
136
|
-
|
|
137
|
-
if (this.config.toolExecutor) {
|
|
138
|
-
// Use provided tool executor
|
|
139
|
-
result = this.config.toolExecutor(toolCall.name, toolCall.arguments);
|
|
140
|
-
} else {
|
|
141
|
-
// Fallback: return error
|
|
142
|
-
result = `Tool '${toolCall.name}' not configured`;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
toolResults.push(`Tool: ${toolCall.name}\nResult: ${result}`);
|
|
146
|
-
} catch (error) {
|
|
147
|
-
toolResults.push(`Tool: ${toolCall.name}\nError: ${error instanceof Error ? error.message : String(error)}`);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Add tool results to conversation
|
|
152
|
-
this.conversationHistory.push({
|
|
153
|
-
role: 'user',
|
|
154
|
-
content: 'Tool Results:\n' + toolResults.join('\n\n'),
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
// Get next completion
|
|
158
|
-
completion_result = await this.getCompletion();
|
|
159
|
-
toolCalls = completion_result.toolCalls;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Add final assistant response to history
|
|
163
|
-
this.conversationHistory.push({
|
|
164
|
-
role: 'assistant',
|
|
165
|
-
content: completion_result.content,
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
return {
|
|
169
|
-
response: completion_result.content,
|
|
170
|
-
completion: completion_result,
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Get completion from the model
|
|
176
|
-
*/
|
|
177
|
-
private async getCompletion(): Promise<CompletionResponse> {
|
|
178
|
-
const completionRequest: CompletionRequest = {
|
|
179
|
-
model: this.config.model,
|
|
180
|
-
apiKey: this.config.apiKey,
|
|
181
|
-
baseUrl: this.config.baseUrl,
|
|
182
|
-
messages: this.conversationHistory,
|
|
183
|
-
tools: this.config.tools,
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
return completion(completionRequest);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Stream a response for a user message
|
|
191
|
-
*/
|
|
192
|
-
async *generateStream(
|
|
193
|
-
request: AgentGenerateRequest
|
|
194
|
-
): AsyncIterable<CompletionChunk> {
|
|
195
|
-
if (!request.userPrompt) {
|
|
196
|
-
throw new LLMError('userPrompt is required', 'MISSING_USER_PROMPT');
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Build user message with context
|
|
200
|
-
let userMessage = request.userPrompt;
|
|
201
|
-
|
|
202
|
-
if (request.images && request.images.length > 0) {
|
|
203
|
-
userMessage += `\n\n[Images: ${request.images.length} image(s) attached]`;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (request.references && request.references.length > 0) {
|
|
207
|
-
userMessage += '\n\nReferences:\n' + request.references.join('\n---\n');
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (request.context && Object.keys(request.context).length > 0) {
|
|
211
|
-
userMessage +=
|
|
212
|
-
'\n\nContext: ' + JSON.stringify(request.context, null, 2);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Add user message to history
|
|
216
|
-
this.conversationHistory.push({
|
|
217
|
-
role: 'user',
|
|
218
|
-
content: userMessage,
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
// Create completion request
|
|
222
|
-
const completionRequest: CompletionRequest = {
|
|
223
|
-
model: this.config.model,
|
|
224
|
-
apiKey: this.config.apiKey,
|
|
225
|
-
baseUrl: this.config.baseUrl,
|
|
226
|
-
messages: this.conversationHistory,
|
|
227
|
-
tools: this.config.tools,
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
// Stream and collect response
|
|
231
|
-
let fullResponse = '';
|
|
232
|
-
|
|
233
|
-
const stream = streamCompletion(completionRequest);
|
|
234
|
-
for await (const chunk of stream) {
|
|
235
|
-
fullResponse += chunk.delta;
|
|
236
|
-
yield chunk;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// Add assistant response to history
|
|
240
|
-
this.conversationHistory.push({
|
|
241
|
-
role: 'assistant',
|
|
242
|
-
content: fullResponse,
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Get conversation history
|
|
248
|
-
*/
|
|
249
|
-
getHistory(): Message[] {
|
|
250
|
-
return [...this.conversationHistory];
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Clear conversation history (keeps system instruction if set)
|
|
255
|
-
*/
|
|
256
|
-
clearHistory(): void {
|
|
257
|
-
if (this.config.instruction) {
|
|
258
|
-
this.conversationHistory = [
|
|
259
|
-
{
|
|
260
|
-
role: 'system',
|
|
261
|
-
content: this.config.instruction,
|
|
262
|
-
},
|
|
263
|
-
];
|
|
264
|
-
} else {
|
|
265
|
-
this.conversationHistory = [];
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Add a message to history
|
|
271
|
-
*/
|
|
272
|
-
addMessage(role: 'system' | 'user' | 'assistant', content: string): void {
|
|
273
|
-
this.conversationHistory.push({
|
|
274
|
-
role,
|
|
275
|
-
content,
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Get the current configuration
|
|
281
|
-
*/
|
|
282
|
-
getConfig(): AgentConfig {
|
|
283
|
-
return { ...this.config };
|
|
284
|
-
}
|
|
285
|
-
}
|