@proteinjs/conversation 1.7.2 → 1.7.4
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/CHANGELOG.md +19 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/src/Conversation.d.ts +1 -1
- package/dist/src/Conversation.d.ts.map +1 -1
- package/dist/src/Conversation.js +11 -7
- package/dist/src/Conversation.js.map +1 -1
- package/dist/src/OpenAi.d.ts +1 -1
- package/dist/src/OpenAi.d.ts.map +1 -1
- package/dist/src/OpenAi.js +51 -27
- package/dist/src/OpenAi.js.map +1 -1
- package/dist/src/OpenAiStreamProcessor.d.ts +7 -2
- package/dist/src/OpenAiStreamProcessor.d.ts.map +1 -1
- package/dist/src/OpenAiStreamProcessor.js +14 -10
- package/dist/src/OpenAiStreamProcessor.js.map +1 -1
- package/dist/src/code_template/CodeTemplate.d.ts +1 -1
- package/dist/src/code_template/CodeTemplate.d.ts.map +1 -1
- package/dist/src/code_template/CodeTemplate.js +4 -4
- package/dist/src/code_template/CodeTemplate.js.map +1 -1
- package/dist/src/code_template/Repo.d.ts.map +1 -1
- package/dist/src/code_template/Repo.js +8 -8
- package/dist/src/code_template/Repo.js.map +1 -1
- package/dist/src/fs/conversation_fs/ConversationFsModerator.d.ts +2 -1
- package/dist/src/fs/conversation_fs/ConversationFsModerator.d.ts.map +1 -1
- package/dist/src/fs/conversation_fs/ConversationFsModerator.js +6 -5
- package/dist/src/fs/conversation_fs/ConversationFsModerator.js.map +1 -1
- package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.d.ts.map +1 -1
- package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.js +7 -7
- package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.js.map +1 -1
- package/dist/src/history/MessageHistory.d.ts.map +1 -1
- package/dist/src/history/MessageHistory.js +3 -3
- package/dist/src/history/MessageHistory.js.map +1 -1
- package/dist/src/template/ConversationTemplateModule.d.ts.map +1 -1
- package/dist/src/template/ConversationTemplateModule.js +3 -3
- package/dist/src/template/ConversationTemplateModule.js.map +1 -1
- package/index.ts +1 -0
- package/package.json +3 -2
- package/src/Conversation.ts +11 -9
- package/src/OpenAi.ts +51 -40
- package/src/OpenAiStreamProcessor.ts +19 -10
- package/src/code_template/CodeTemplate.ts +5 -4
- package/src/code_template/Repo.ts +8 -8
- package/src/fs/conversation_fs/ConversationFsModerator.ts +7 -5
- package/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.ts +7 -7
- package/src/history/MessageHistory.ts +3 -3
- package/src/template/ConversationTemplateModule.ts +4 -3
package/src/OpenAi.ts
CHANGED
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
ChatCompletionMessageToolCall,
|
|
6
6
|
ChatCompletionChunk,
|
|
7
7
|
} from 'openai/resources/chat';
|
|
8
|
-
import {
|
|
8
|
+
import { isInstanceOf } from '@proteinjs/util';
|
|
9
|
+
import { LogLevel, Logger } from '@proteinjs/logger';
|
|
9
10
|
import { MessageModerator } from './history/MessageModerator';
|
|
10
11
|
import { Function } from './Function';
|
|
11
12
|
import { MessageHistory } from './history/MessageHistory';
|
|
@@ -80,11 +81,11 @@ export class OpenAi {
|
|
|
80
81
|
logLevel: LogLevel = 'info',
|
|
81
82
|
maxFunctionCalls: number = 50
|
|
82
83
|
): Promise<string | Readable> {
|
|
83
|
-
const logger = new Logger('OpenAi.generateResponseHelper', logLevel);
|
|
84
|
+
const logger = new Logger({ name: 'OpenAi.generateResponseHelper', logLevel });
|
|
84
85
|
const updatedHistory = OpenAi.getUpdatedMessageHistory(messages, history, messageModerators);
|
|
85
86
|
const response = await OpenAi.executeRequest(updatedHistory, stream, logLevel, functions, model, abortSignal);
|
|
86
87
|
if (stream) {
|
|
87
|
-
logger.info(`Processing response stream`);
|
|
88
|
+
logger.info({ message: `Processing response stream` });
|
|
88
89
|
const inputStream = response as Stream<ChatCompletionChunk>;
|
|
89
90
|
|
|
90
91
|
// For subsequent tool calls, return the raw OpenAI stream to `OpenAiStreamProcessor`
|
|
@@ -174,7 +175,7 @@ export class OpenAi {
|
|
|
174
175
|
model?: string,
|
|
175
176
|
abortSignal?: AbortSignal
|
|
176
177
|
): Promise<ChatCompletion | Stream<ChatCompletionChunk>> {
|
|
177
|
-
const logger = new Logger('OpenAi.executeRequest', logLevel);
|
|
178
|
+
const logger = new Logger({ name: 'OpenAi.executeRequest', logLevel });
|
|
178
179
|
const openaiApi = new OpenAIApi();
|
|
179
180
|
try {
|
|
180
181
|
const latestMessage = messageParamsWithHistory.getMessages()[messageParamsWithHistory.getMessages().length - 1];
|
|
@@ -211,7 +212,7 @@ export class OpenAi {
|
|
|
211
212
|
messageParamsWithHistory: MessageHistory
|
|
212
213
|
) {
|
|
213
214
|
if (latestMessage.role == 'tool') {
|
|
214
|
-
logger.info(`Sending request: returning output of tool call (${latestMessage.tool_call_id})`);
|
|
215
|
+
logger.info({ message: `Sending request: returning output of tool call (${latestMessage.tool_call_id})` });
|
|
215
216
|
} else if (latestMessage.content) {
|
|
216
217
|
const requestContent =
|
|
217
218
|
typeof latestMessage.content === 'string'
|
|
@@ -219,31 +220,32 @@ export class OpenAi {
|
|
|
219
220
|
: latestMessage.content[0].type === 'text'
|
|
220
221
|
? latestMessage.content[0].text
|
|
221
222
|
: 'image';
|
|
222
|
-
logger.info(`Sending request:
|
|
223
|
+
logger.info({ message: `Sending request`, obj: { requestContent } });
|
|
223
224
|
} else {
|
|
224
|
-
logger.info(`Sending request`);
|
|
225
|
+
logger.info({ message: `Sending request` });
|
|
225
226
|
}
|
|
226
227
|
|
|
227
228
|
if (logLevel === 'debug') {
|
|
228
|
-
logger.debug(`Sending messages:
|
|
229
|
+
logger.debug({ message: `Sending messages:`, obj: { messages: messageParamsWithHistory.getMessages() } });
|
|
229
230
|
}
|
|
230
231
|
}
|
|
231
232
|
|
|
232
233
|
private static logResponseDetails(logger: Logger, response: ChatCompletion) {
|
|
233
234
|
const responseMessage = response.choices[0].message;
|
|
234
235
|
if (responseMessage.content) {
|
|
235
|
-
logger.info(`Received response:
|
|
236
|
+
logger.info({ message: `Received response`, obj: { response: responseMessage.content } });
|
|
236
237
|
} else if (responseMessage.tool_calls) {
|
|
237
|
-
logger.info(
|
|
238
|
-
`Received response: call functions
|
|
239
|
-
|
|
238
|
+
logger.info({
|
|
239
|
+
message: `Received response: call functions`,
|
|
240
|
+
obj: { functions: responseMessage.tool_calls.map((toolCall) => toolCall.function.name) },
|
|
241
|
+
});
|
|
240
242
|
} else {
|
|
241
|
-
logger.info(`Received response`);
|
|
243
|
+
logger.info({ message: `Received response` });
|
|
242
244
|
}
|
|
243
245
|
if (response.usage) {
|
|
244
|
-
logger.info(
|
|
246
|
+
logger.info({ message: `Usage data`, obj: { usageData: response.usage } });
|
|
245
247
|
} else {
|
|
246
|
-
logger.info(
|
|
248
|
+
logger.info({ message: `Usage data missing` });
|
|
247
249
|
}
|
|
248
250
|
}
|
|
249
251
|
|
|
@@ -257,16 +259,16 @@ export class OpenAi {
|
|
|
257
259
|
model?: string
|
|
258
260
|
): Promise<ChatCompletion | Stream<ChatCompletionChunk>> {
|
|
259
261
|
if (error.type) {
|
|
260
|
-
logger.info(`Received error response, error type: ${error.type}`);
|
|
262
|
+
logger.info({ message: `Received error response, error type: ${error.type}` });
|
|
261
263
|
}
|
|
262
264
|
if (typeof error.status !== 'undefined' && error.status == 429) {
|
|
263
265
|
if (error.type == 'tokens' && typeof error.headers['x-ratelimit-reset-tokens'] === 'string') {
|
|
264
266
|
const waitTime = parseInt(error.headers['x-ratelimit-reset-tokens']);
|
|
265
267
|
const remainingTokens = error.headers['x-ratelimit-remaining-tokens'];
|
|
266
268
|
const delayMs = 15000;
|
|
267
|
-
logger.warn(
|
|
268
|
-
`Waiting to retry in ${delayMs / 1000}s, token reset in: ${waitTime}s, remaining tokens: ${remainingTokens}
|
|
269
|
-
);
|
|
269
|
+
logger.warn({
|
|
270
|
+
message: `Waiting to retry in ${delayMs / 1000}s, token reset in: ${waitTime}s, remaining tokens: ${remainingTokens}`,
|
|
271
|
+
});
|
|
270
272
|
await delay(delayMs);
|
|
271
273
|
return await OpenAi.executeRequest(messageParamsWithHistory, stream, logLevel, functions, model);
|
|
272
274
|
}
|
|
@@ -343,27 +345,33 @@ export class OpenAi {
|
|
|
343
345
|
toolCallId: string,
|
|
344
346
|
functions?: Omit<Function, 'instructions'>[]
|
|
345
347
|
): Promise<ChatCompletionMessageParam[]> {
|
|
346
|
-
const logger = new Logger('OpenAi.callFunction', logLevel);
|
|
348
|
+
const logger = new Logger({ name: 'OpenAi.callFunction', logLevel });
|
|
347
349
|
if (!functions) {
|
|
348
|
-
const
|
|
349
|
-
logger.error(
|
|
350
|
-
return [{ role: 'tool', tool_call_id: toolCallId, content: JSON.stringify({ error }) }];
|
|
350
|
+
const errorMessage = `Assistant attempted to call a function when no functions were provided`;
|
|
351
|
+
logger.error({ message: errorMessage });
|
|
352
|
+
return [{ role: 'tool', tool_call_id: toolCallId, content: JSON.stringify({ error: errorMessage }) }];
|
|
351
353
|
}
|
|
352
354
|
|
|
353
355
|
functionCall.name = functionCall.name.split('.').pop() as string;
|
|
354
356
|
const f = functions.find((f) => f.definition.name === functionCall.name);
|
|
355
357
|
if (!f) {
|
|
356
|
-
const
|
|
357
|
-
logger.error(
|
|
358
|
-
return [
|
|
358
|
+
const errorMessage = `Assistant attempted to call nonexistent function`;
|
|
359
|
+
logger.error({ message: errorMessage, obj: { functionName: functionCall.name } });
|
|
360
|
+
return [
|
|
361
|
+
{
|
|
362
|
+
role: 'tool',
|
|
363
|
+
tool_call_id: toolCallId,
|
|
364
|
+
content: JSON.stringify({ error: errorMessage, functionName: functionCall.name }),
|
|
365
|
+
},
|
|
366
|
+
];
|
|
359
367
|
}
|
|
360
368
|
|
|
361
369
|
try {
|
|
362
370
|
const parsedArguments = JSON.parse(functionCall.arguments);
|
|
363
|
-
logger.info(
|
|
364
|
-
`Assistant calling function: (${toolCallId}) ${f.definition.name}
|
|
365
|
-
|
|
366
|
-
);
|
|
371
|
+
logger.info({
|
|
372
|
+
message: `Assistant calling function: (${toolCallId}) ${f.definition.name}`,
|
|
373
|
+
obj: { toolCallId, functionName: f.definition.name, args: parsedArguments },
|
|
374
|
+
});
|
|
367
375
|
const returnObject = await f.call(parsedArguments);
|
|
368
376
|
|
|
369
377
|
const returnObjectCompletionParams: ChatCompletionMessageParam[] = [];
|
|
@@ -377,10 +385,10 @@ export class OpenAi {
|
|
|
377
385
|
content: `The the return data from this function is provided in the following messages`,
|
|
378
386
|
};
|
|
379
387
|
returnObjectCompletionParams.push(instructionMessageParam, ...messageParams);
|
|
380
|
-
logger.info(
|
|
381
|
-
`Assistant called function: (${toolCallId}) ${f.definition.name}
|
|
382
|
-
|
|
383
|
-
);
|
|
388
|
+
logger.info({
|
|
389
|
+
message: `Assistant called function: (${toolCallId}) ${f.definition.name}`,
|
|
390
|
+
obj: { toolCallId, functionName: f.definition.name, return: messageParams },
|
|
391
|
+
});
|
|
384
392
|
} else {
|
|
385
393
|
// handle all other functions
|
|
386
394
|
const serializedReturnObject = JSON.stringify(returnObject);
|
|
@@ -389,10 +397,10 @@ export class OpenAi {
|
|
|
389
397
|
tool_call_id: toolCallId,
|
|
390
398
|
content: serializedReturnObject,
|
|
391
399
|
});
|
|
392
|
-
logger.info(
|
|
393
|
-
`Assistant called function: (${toolCallId}) ${f.definition.name}
|
|
394
|
-
|
|
395
|
-
);
|
|
400
|
+
logger.info({
|
|
401
|
+
message: `Assistant called function: (${toolCallId}) ${f.definition.name}`,
|
|
402
|
+
obj: { toolCallId, functionName: f.definition.name, return: returnObject },
|
|
403
|
+
});
|
|
396
404
|
}
|
|
397
405
|
|
|
398
406
|
if (typeof returnObject === 'undefined') {
|
|
@@ -407,8 +415,11 @@ export class OpenAi {
|
|
|
407
415
|
|
|
408
416
|
return returnObjectCompletionParams;
|
|
409
417
|
} catch (error: any) {
|
|
410
|
-
|
|
411
|
-
|
|
418
|
+
logger.error({
|
|
419
|
+
message: `An error occurred while executing function`,
|
|
420
|
+
error,
|
|
421
|
+
obj: { toolCallId, functionName: f.definition.name },
|
|
422
|
+
});
|
|
412
423
|
throw error;
|
|
413
424
|
}
|
|
414
425
|
}
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { ChatCompletionMessageToolCall, ChatCompletionChunk } from 'openai/resources/chat';
|
|
2
|
-
import { LogLevel, Logger } from '@proteinjs/
|
|
2
|
+
import { LogLevel, Logger } from '@proteinjs/logger';
|
|
3
3
|
import { Stream } from 'openai/streaming';
|
|
4
4
|
import { Readable, Transform, TransformCallback, PassThrough } from 'stream';
|
|
5
5
|
|
|
6
|
+
export interface AssistantResponseStreamChunk {
|
|
7
|
+
content?: string;
|
|
8
|
+
finishReason?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
/**
|
|
7
12
|
* Processes streaming responses from OpenAI's `ChatCompletions` api.
|
|
8
13
|
* - When a tool call is received, it delegates processing to `onToolCalls`; this can happen recursively
|
|
@@ -26,15 +31,16 @@ export class OpenAiStreamProcessor {
|
|
|
26
31
|
logLevel: LogLevel,
|
|
27
32
|
private abortSignal?: AbortSignal
|
|
28
33
|
) {
|
|
29
|
-
this.logger = new Logger(this.constructor.name, logLevel);
|
|
34
|
+
this.logger = new Logger({ name: this.constructor.name, logLevel });
|
|
30
35
|
this.inputStream = Readable.from(inputStream);
|
|
31
36
|
this.controlStream = this.createControlStream();
|
|
32
|
-
this.outputStream = new PassThrough();
|
|
37
|
+
this.outputStream = new PassThrough({ objectMode: true });
|
|
33
38
|
this.inputStream.pipe(this.controlStream);
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
/**
|
|
37
|
-
* @returns a `Readable` stream that will receive the assistant's text response to the user
|
|
42
|
+
* @returns a `Readable` stream, in object mode, that will receive the assistant's text response to the user.
|
|
43
|
+
* The object chunks written to the stream will be of type `AssistantResponseStreamChunk`.
|
|
38
44
|
*/
|
|
39
45
|
getOutputStream(): Readable {
|
|
40
46
|
return this.outputStream;
|
|
@@ -49,7 +55,7 @@ export class OpenAiStreamProcessor {
|
|
|
49
55
|
transform: (chunk: ChatCompletionChunk, encoding: string, callback: TransformCallback) => {
|
|
50
56
|
try {
|
|
51
57
|
if (this.outputStream.destroyed) {
|
|
52
|
-
this.logger.warn(`Destroying input and control streams since output stream is destroyed`);
|
|
58
|
+
this.logger.warn({ message: `Destroying input and control streams since output stream is destroyed` });
|
|
53
59
|
this.inputStream.destroy();
|
|
54
60
|
this.controlStream.destroy();
|
|
55
61
|
return;
|
|
@@ -58,26 +64,29 @@ export class OpenAiStreamProcessor {
|
|
|
58
64
|
if (!chunk || !chunk.choices) {
|
|
59
65
|
throw new Error(`Received invalid chunk:\n${JSON.stringify(chunk, null, 2)}`);
|
|
60
66
|
} else if (chunk.choices[0]?.delta?.content) {
|
|
61
|
-
this.outputStream.push(chunk.choices[0].delta.content);
|
|
67
|
+
this.outputStream.push({ content: chunk.choices[0].delta.content } as AssistantResponseStreamChunk);
|
|
62
68
|
} else if (chunk.choices[0]?.delta?.tool_calls) {
|
|
63
69
|
this.handleToolCallDelta(chunk.choices[0].delta.tool_calls);
|
|
64
70
|
} else if (chunk.choices[0]?.finish_reason === 'tool_calls') {
|
|
65
71
|
this.handleToolCalls();
|
|
66
72
|
} else if (chunk.choices[0]?.finish_reason === 'stop') {
|
|
73
|
+
this.outputStream.push({ finishReason: 'stop' } as AssistantResponseStreamChunk);
|
|
67
74
|
this.outputStream.push(null);
|
|
68
75
|
this.destroyStreams();
|
|
69
76
|
} else if (chunk.choices[0]?.finish_reason === 'length') {
|
|
70
|
-
this.logger.
|
|
77
|
+
this.logger.info({ message: `The maximum number of output tokens was reached` });
|
|
78
|
+
this.outputStream.push({ finishReason: 'length' } as AssistantResponseStreamChunk);
|
|
71
79
|
this.outputStream.push(null);
|
|
72
80
|
this.destroyStreams();
|
|
73
81
|
} else if (chunk.choices[0]?.finish_reason === 'content_filter') {
|
|
74
|
-
this.logger.
|
|
82
|
+
this.logger.warn({ message: `Content was omitted due to a flag from OpenAI's content filters` });
|
|
83
|
+
this.outputStream.push({ finishReason: 'content_filter' } as AssistantResponseStreamChunk);
|
|
75
84
|
this.outputStream.push(null);
|
|
76
85
|
this.destroyStreams();
|
|
77
86
|
}
|
|
78
87
|
callback();
|
|
79
88
|
} catch (error: any) {
|
|
80
|
-
this.logger.error('Error tranforming chunk', error);
|
|
89
|
+
this.logger.error({ message: 'Error tranforming chunk', error });
|
|
81
90
|
this.destroyStreams(error);
|
|
82
91
|
}
|
|
83
92
|
},
|
|
@@ -155,7 +164,7 @@ export class OpenAiStreamProcessor {
|
|
|
155
164
|
this.toolCallsExecuted += completedToolCalls.length;
|
|
156
165
|
} catch (error: any) {
|
|
157
166
|
if (!this.abortSignal?.aborted) {
|
|
158
|
-
this.logger.error('Error processing tool calls
|
|
167
|
+
this.logger.error({ message: 'Error processing tool calls', error });
|
|
159
168
|
}
|
|
160
169
|
this.destroyStreams(error);
|
|
161
170
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Logger } from '@proteinjs/
|
|
1
|
+
import { Logger } from '@proteinjs/logger';
|
|
2
2
|
import { Fs, PackageUtil, Package } from '@proteinjs/util-node';
|
|
3
3
|
import { SourceFile } from './Code';
|
|
4
4
|
|
|
@@ -9,10 +9,11 @@ export type TemplateArgs = {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
export abstract class CodeTemplate {
|
|
12
|
-
protected logger
|
|
12
|
+
protected logger: Logger;
|
|
13
13
|
protected templateArgs: TemplateArgs;
|
|
14
14
|
|
|
15
15
|
constructor(templateArgs: TemplateArgs) {
|
|
16
|
+
this.logger = new Logger({ name: this.constructor.name });
|
|
16
17
|
this.templateArgs = templateArgs;
|
|
17
18
|
}
|
|
18
19
|
|
|
@@ -23,10 +24,10 @@ export abstract class CodeTemplate {
|
|
|
23
24
|
await PackageUtil.installPackages(this.resolvePackages());
|
|
24
25
|
for (const sourceFile of this.sourceFiles()) {
|
|
25
26
|
const filePath = Fs.baseContainedJoin(this.templateArgs.srcPath, sourceFile.relativePath);
|
|
26
|
-
this.logger.info(`Generating source file: ${filePath}`);
|
|
27
|
+
this.logger.info({ message: `Generating source file: ${filePath}` });
|
|
27
28
|
const code = await sourceFile.code.generate();
|
|
28
29
|
await Fs.writeFiles([{ path: filePath, content: code }]);
|
|
29
|
-
this.logger.info(`Generated source file: ${filePath}`);
|
|
30
|
+
this.logger.info({ message: `Generated source file: ${filePath}` });
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Logger } from '@proteinjs/
|
|
1
|
+
import { Logger } from '@proteinjs/logger';
|
|
2
2
|
import { FileDescriptor, Fs, PackageUtil } from '@proteinjs/util-node';
|
|
3
3
|
import fs from 'fs/promises';
|
|
4
4
|
import path from 'path';
|
|
@@ -24,7 +24,7 @@ export type RepoParams = {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
export class Repo {
|
|
27
|
-
private logger = new Logger(this.constructor.name);
|
|
27
|
+
private logger = new Logger({ name: this.constructor.name });
|
|
28
28
|
params: RepoParams;
|
|
29
29
|
|
|
30
30
|
constructor(params: RepoParams) {
|
|
@@ -48,7 +48,7 @@ export class Repo {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
searchFiles(params: { keyword: string }) {
|
|
51
|
-
this.logger.info(`Searching for file, keyword: ${params.keyword}`);
|
|
51
|
+
this.logger.info({ message: `Searching for file, keyword: ${params.keyword}` });
|
|
52
52
|
const filePaths = this.keywordFilesIndex()[params.keyword];
|
|
53
53
|
return filePaths || [];
|
|
54
54
|
}
|
|
@@ -57,17 +57,17 @@ export class Repo {
|
|
|
57
57
|
const queriedDeclarations: { [tsFilePath: string]: string } = {};
|
|
58
58
|
for (const tsFilePath of params.tsFilePaths) {
|
|
59
59
|
queriedDeclarations[tsFilePath] = this.params.tsFiles[tsFilePath].declaration;
|
|
60
|
-
this.logger.info(`Accessed declaration: ${tsFilePath}`);
|
|
60
|
+
this.logger.info({ message: `Accessed declaration: ${tsFilePath}` });
|
|
61
61
|
}
|
|
62
62
|
return queriedDeclarations;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
export class RepoFactory {
|
|
67
|
-
private static LOGGER = new Logger('RepoFactory');
|
|
67
|
+
private static LOGGER = new Logger({ name: 'RepoFactory' });
|
|
68
68
|
|
|
69
69
|
public static async createRepo(dir: string): Promise<Repo> {
|
|
70
|
-
this.LOGGER.info(`Creating repo for dir: ${dir}`);
|
|
70
|
+
this.LOGGER.info({ message: `Creating repo for dir: ${dir}` });
|
|
71
71
|
const repoParams: RepoParams = { packages: {}, slimPackages: {}, tsFiles: {}, keywordFilesIndex: {} };
|
|
72
72
|
|
|
73
73
|
async function traverse(dir: string) {
|
|
@@ -106,13 +106,13 @@ export class RepoFactory {
|
|
|
106
106
|
const { packageJSON, tsFiles, ...slimPackage } = repoParams.packages[packageName];
|
|
107
107
|
repoParams.slimPackages[packageName] = slimPackage;
|
|
108
108
|
});
|
|
109
|
-
this.LOGGER.info(`Created repo for dir: ${dir}`);
|
|
109
|
+
this.LOGGER.info({ message: `Created repo for dir: ${dir}` });
|
|
110
110
|
return new Repo(repoParams);
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
private static async loadFiles(repoParams: RepoParams) {
|
|
114
114
|
for (const packageName of Object.keys(repoParams.packages)) {
|
|
115
|
-
this.LOGGER.info(`Loading files for package: ${packageName}`);
|
|
115
|
+
this.LOGGER.info({ message: `Loading files for package: ${packageName}` });
|
|
116
116
|
const dirPath = repoParams.packages[packageName].dirPath;
|
|
117
117
|
if (dirPath) {
|
|
118
118
|
repoParams.packages[packageName].fileDescriptors.push(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-empty */
|
|
2
|
-
import { LogLevel, Logger } from '@proteinjs/
|
|
2
|
+
import { LogLevel, Logger } from '@proteinjs/logger';
|
|
3
3
|
import { FileContentMap } from '@proteinjs/util-node';
|
|
4
4
|
import { ChatCompletionMessageParam } from 'openai/resources/chat';
|
|
5
5
|
import { MessageModerator } from '../../history/MessageModerator';
|
|
@@ -20,14 +20,14 @@ export class ConversationFsFactory {
|
|
|
20
20
|
|
|
21
21
|
constructor(params?: Partial<ConversationFsFactoryParams>) {
|
|
22
22
|
this.params = Object.assign({ maxFiles: 3, logLevel: 'info' }, params);
|
|
23
|
-
this.logger = new Logger(this.constructor.name, this.params.logLevel);
|
|
23
|
+
this.logger = new Logger({ name: this.constructor.name, logLevel: this.params.logLevel });
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
merge(existingFs: ConversationFs, updates: FileContentMap): ConversationFs {
|
|
27
27
|
for (const filePath of Object.keys(updates)) {
|
|
28
28
|
// if the file already exists in the fs
|
|
29
29
|
if (existingFs.fileContentMap[filePath]) {
|
|
30
|
-
this.logger.debug(`Updating existing file: ${filePath}`);
|
|
30
|
+
this.logger.debug({ message: `Updating existing file: ${filePath}` });
|
|
31
31
|
existingFs.fileContentMap[filePath] = updates[filePath];
|
|
32
32
|
const oldIndex = existingFs.order.findIndex((item) => item == filePath);
|
|
33
33
|
existingFs.order.splice(oldIndex, 1);
|
|
@@ -37,12 +37,12 @@ export class ConversationFsFactory {
|
|
|
37
37
|
|
|
38
38
|
// if we have less than the max number of files in the fs
|
|
39
39
|
if (Object.keys(existingFs.fileContentMap).length < this.params.maxFiles) {
|
|
40
|
-
this.logger.debug(`Adding new file (under limit): ${filePath}`);
|
|
40
|
+
this.logger.debug({ message: `Adding new file (under limit): ${filePath}` });
|
|
41
41
|
existingFs.fileContentMap[filePath] = updates[filePath];
|
|
42
42
|
existingFs.order.push(filePath);
|
|
43
43
|
continue;
|
|
44
44
|
} else {
|
|
45
|
-
this.logger.debug(`Adding new file (over limit): ${filePath}`);
|
|
45
|
+
this.logger.debug({ message: `Adding new file (over limit): ${filePath}` });
|
|
46
46
|
const removedFilePath = existingFs.order.splice(0, 1)[0];
|
|
47
47
|
delete existingFs.fileContentMap[removedFilePath];
|
|
48
48
|
existingFs.fileContentMap[filePath] = updates[filePath];
|
|
@@ -56,11 +56,13 @@ export class ConversationFsFactory {
|
|
|
56
56
|
|
|
57
57
|
export class ConversationFsModerator implements MessageModerator {
|
|
58
58
|
private logLevel: LogLevel = 'info';
|
|
59
|
+
private logger: Logger;
|
|
59
60
|
|
|
60
61
|
constructor(logLevel?: LogLevel) {
|
|
61
62
|
if (logLevel) {
|
|
62
63
|
this.logLevel = logLevel;
|
|
63
64
|
}
|
|
65
|
+
this.logger = new Logger({ name: this.constructor.name, logLevel: this.logLevel });
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
observe(messages: ChatCompletionMessageParam[]): ChatCompletionMessageParam[] {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Logger } from '@proteinjs/
|
|
1
|
+
import { Logger } from '@proteinjs/logger';
|
|
2
2
|
import { Fs } from '@proteinjs/util-node';
|
|
3
3
|
import { ConversationModule, ConversationModuleFactory } from '../../ConversationModule';
|
|
4
4
|
import { Function } from '../../Function';
|
|
@@ -11,7 +11,7 @@ export type KeywordToFilesIndexModuleParams = {
|
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
export class KeywordToFilesIndexModule implements ConversationModule {
|
|
14
|
-
private logger = new Logger(this.constructor.name);
|
|
14
|
+
private logger = new Logger({ name: this.constructor.name });
|
|
15
15
|
params: KeywordToFilesIndexModuleParams;
|
|
16
16
|
|
|
17
17
|
constructor(params: KeywordToFilesIndexModuleParams) {
|
|
@@ -23,7 +23,7 @@ export class KeywordToFilesIndexModule implements ConversationModule {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
searchFiles(params: { keyword: string }) {
|
|
26
|
-
this.logger.info(`Searching for file, keyword: ${params.keyword}`);
|
|
26
|
+
this.logger.info({ message: `Searching for file, keyword: ${params.keyword}` });
|
|
27
27
|
const filePaths = this.params.keywordFilesIndex[params.keyword];
|
|
28
28
|
return filePaths || [];
|
|
29
29
|
}
|
|
@@ -44,13 +44,13 @@ export class KeywordToFilesIndexModule implements ConversationModule {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
export class KeywordToFilesIndexModuleFactory implements ConversationModuleFactory {
|
|
47
|
-
private logger = new Logger(this.constructor.name);
|
|
47
|
+
private logger = new Logger({ name: this.constructor.name });
|
|
48
48
|
|
|
49
49
|
async createModule(repoPath: string): Promise<KeywordToFilesIndexModule> {
|
|
50
|
-
this.logger.debug(`Creating module for repo: ${repoPath}`);
|
|
50
|
+
this.logger.debug({ message: `Creating module for repo: ${repoPath}` });
|
|
51
51
|
const repoParams: KeywordToFilesIndexModuleParams = { keywordFilesIndex: {}, dir: repoPath };
|
|
52
52
|
repoParams.keywordFilesIndex = await this.createKeywordFilesIndex(repoPath, ['**/node-typescript-parser/**']);
|
|
53
|
-
this.logger.debug(`Created module for repo: ${repoPath}`);
|
|
53
|
+
this.logger.debug({ message: `Created module for repo: ${repoPath}` });
|
|
54
54
|
return new KeywordToFilesIndexModule(repoParams);
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -82,7 +82,7 @@ export class KeywordToFilesIndexModuleFactory implements ConversationModuleFacto
|
|
|
82
82
|
keywordFilesIndex[fileName] = [];
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
this.logger.debug(`fileName: ${fileName}, filePath: ${filePath}`);
|
|
85
|
+
this.logger.debug({ message: `fileName: ${fileName}, filePath: ${filePath}` });
|
|
86
86
|
keywordFilesIndex[fileName].push(filePath);
|
|
87
87
|
}
|
|
88
88
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ChatCompletionMessageParam } from 'openai/resources/chat';
|
|
2
|
-
import { Logger } from '@proteinjs/
|
|
2
|
+
import { Logger } from '@proteinjs/logger';
|
|
3
3
|
|
|
4
4
|
export interface MessageHistoryParams {
|
|
5
5
|
enforceMessageLimit?: boolean;
|
|
@@ -7,7 +7,7 @@ export interface MessageHistoryParams {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export class MessageHistory {
|
|
10
|
-
private logger = new Logger(this.constructor.name);
|
|
10
|
+
private logger = new Logger({ name: this.constructor.name });
|
|
11
11
|
private messages: ChatCompletionMessageParam[] = [];
|
|
12
12
|
private params: MessageHistoryParams;
|
|
13
13
|
|
|
@@ -55,6 +55,6 @@ export class MessageHistory {
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
this.messages = this.messages.filter((message, i) => !messagesToRemoveIndexes.includes(i));
|
|
58
|
-
this.logger.debug(`Pruned ${messagesToRemoveIndexes.length} messages`);
|
|
58
|
+
this.logger.debug({ message: `Pruned ${messagesToRemoveIndexes.length} messages` });
|
|
59
59
|
}
|
|
60
60
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Logger } from '@proteinjs/
|
|
1
|
+
import { Logger } from '@proteinjs/logger';
|
|
2
2
|
import { ConversationTemplate } from './ConversationTemplate';
|
|
3
3
|
import { createPackageConversationTemplate } from './createPackage/CreatePackageConversationTemplate';
|
|
4
4
|
import { ConversationModule, ConversationModuleFactory } from '../ConversationModule';
|
|
@@ -23,10 +23,11 @@ export type ConversationTemplateModuleParams = {
|
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
export class ConversationTemplateModule implements ConversationModule {
|
|
26
|
-
private logger
|
|
26
|
+
private logger: Logger;
|
|
27
27
|
params: ConversationTemplateModuleParams;
|
|
28
28
|
|
|
29
29
|
constructor(params: ConversationTemplateModuleParams) {
|
|
30
|
+
this.logger = new Logger({ name: this.constructor.name });
|
|
30
31
|
this.params = params;
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -35,7 +36,7 @@ export class ConversationTemplateModule implements ConversationModule {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
searchConversationTemplates(keyword: string) {
|
|
38
|
-
this.logger.info(`Searching for conversation template, keyword: ${keyword}`);
|
|
39
|
+
this.logger.info({ message: `Searching for conversation template, keyword: ${keyword}` });
|
|
39
40
|
const conversationNames = this.params.conversationTemplateKeywordIndex[keyword];
|
|
40
41
|
return conversationNames || [];
|
|
41
42
|
}
|