@proteinjs/conversation 1.0.7 → 1.0.9
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/.eslintrc.js +1 -1
- package/.prettierignore +4 -0
- package/CHANGELOG.md +11 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +28 -40
- package/dist/src/CodegenConversation.d.ts +11 -11
- package/dist/src/CodegenConversation.js +180 -294
- package/dist/src/Conversation.d.ts +49 -52
- package/dist/src/Conversation.js +288 -478
- package/dist/src/ConversationModule.d.ts +6 -6
- package/dist/src/ConversationModule.js +3 -3
- package/dist/src/Function.d.ts +4 -4
- package/dist/src/Function.js +3 -3
- package/dist/src/OpenAi.d.ts +10 -42
- package/dist/src/OpenAi.js +305 -495
- package/dist/src/Paragraph.d.ts +4 -4
- package/dist/src/Paragraph.js +17 -17
- package/dist/src/Sentence.d.ts +4 -4
- package/dist/src/Sentence.js +21 -21
- package/dist/src/code_template/Code.d.ts +15 -15
- package/dist/src/code_template/Code.js +73 -167
- package/dist/src/code_template/CodeTemplate.d.ts +11 -11
- package/dist/src/code_template/CodeTemplate.js +80 -169
- package/dist/src/code_template/CodeTemplateModule.d.ts +6 -6
- package/dist/src/code_template/CodeTemplateModule.js +26 -28
- package/dist/src/code_template/Repo.d.ts +38 -34
- package/dist/src/code_template/Repo.js +195 -291
- package/dist/src/fs/conversation_fs/ConversationFsModerator.d.ts +12 -12
- package/dist/src/fs/conversation_fs/ConversationFsModerator.js +108 -110
- package/dist/src/fs/conversation_fs/ConversationFsModule.d.ts +11 -11
- package/dist/src/fs/conversation_fs/ConversationFsModule.js +97 -204
- package/dist/src/fs/conversation_fs/FsFunctions.d.ts +62 -58
- package/dist/src/fs/conversation_fs/FsFunctions.js +252 -414
- package/dist/src/fs/git/GitModule.d.ts +8 -8
- package/dist/src/fs/git/GitModule.js +74 -163
- package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexFunctions.d.ts +18 -16
- package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexFunctions.js +58 -158
- package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.d.ts +26 -27
- package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.js +131 -234
- package/dist/src/fs/package/PackageFunctions.d.ts +60 -54
- package/dist/src/fs/package/PackageFunctions.js +213 -366
- package/dist/src/fs/package/PackageModule.d.ts +24 -24
- package/dist/src/fs/package/PackageModule.js +170 -292
- package/dist/src/history/MessageHistory.d.ts +12 -12
- package/dist/src/history/MessageHistory.js +46 -52
- package/dist/src/history/MessageModerator.d.ts +2 -2
- package/dist/src/history/MessageModerator.js +3 -3
- package/dist/src/template/ConversationTemplate.d.ts +8 -8
- package/dist/src/template/ConversationTemplate.js +3 -3
- package/dist/src/template/ConversationTemplateFunctions.d.ts +35 -33
- package/dist/src/template/ConversationTemplateFunctions.js +75 -176
- package/dist/src/template/ConversationTemplateModule.d.ts +48 -51
- package/dist/src/template/ConversationTemplateModule.js +116 -211
- package/dist/src/template/createApp/CreateAppTemplate.d.ts +1 -1
- package/dist/src/template/createApp/CreateAppTemplate.js +59 -151
- package/dist/src/template/createCode/CreateCodeConversationTemplate.d.ts +1 -1
- package/dist/src/template/createCode/CreateCodeConversationTemplate.js +67 -183
- package/dist/src/template/createPackage/CreatePackageConversationTemplate.d.ts +1 -1
- package/dist/src/template/createPackage/CreatePackageConversationTemplate.js +77 -174
- package/dist/src/template/createPackage/tsconfig.json +11 -11
- package/dist/test/createKeywordFilesIndex.test.d.ts +1 -1
- package/dist/test/createKeywordFilesIndex.test.js +41 -132
- package/dist/test/openai/openai.generateList.test.d.ts +1 -1
- package/dist/test/openai/openai.generateList.test.js +47 -136
- package/dist/test/openai/openai.parseCodeFromMarkdown.test.d.ts +1 -1
- package/dist/test/openai/openai.parseCodeFromMarkdown.test.js +12 -13
- package/dist/test/repo/repo.test.d.ts +1 -1
- package/dist/test/repo/repo.test.js +38 -127
- package/package.json +5 -6
- package/LICENSE +0 -21
package/dist/src/Conversation.js
CHANGED
|
@@ -1,499 +1,309 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter =
|
|
3
|
-
|
|
4
|
-
function (thisArg, _arguments, P, generator) {
|
|
5
|
-
function adopt(value) {
|
|
6
|
-
return value instanceof P
|
|
7
|
-
? value
|
|
8
|
-
: new P(function (resolve) {
|
|
9
|
-
resolve(value);
|
|
10
|
-
});
|
|
11
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
12
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
reject(e);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function rejected(value) {
|
|
21
|
-
try {
|
|
22
|
-
step(generator['throw'](value));
|
|
23
|
-
} catch (e) {
|
|
24
|
-
reject(e);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function step(result) {
|
|
28
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
29
|
-
}
|
|
30
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
31
9
|
});
|
|
32
|
-
|
|
33
|
-
var __generator =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
label: 0,
|
|
38
|
-
sent: function () {
|
|
39
|
-
if (t[0] & 1) throw t[1];
|
|
40
|
-
return t[1];
|
|
41
|
-
},
|
|
42
|
-
trys: [],
|
|
43
|
-
ops: [],
|
|
44
|
-
},
|
|
45
|
-
f,
|
|
46
|
-
y,
|
|
47
|
-
t,
|
|
48
|
-
g;
|
|
49
|
-
return (
|
|
50
|
-
(g = { next: verb(0), throw: verb(1), return: verb(2) }),
|
|
51
|
-
typeof Symbol === 'function' &&
|
|
52
|
-
(g[Symbol.iterator] = function () {
|
|
53
|
-
return this;
|
|
54
|
-
}),
|
|
55
|
-
g
|
|
56
|
-
);
|
|
57
|
-
function verb(n) {
|
|
58
|
-
return function (v) {
|
|
59
|
-
return step([n, v]);
|
|
60
|
-
};
|
|
61
|
-
}
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
62
15
|
function step(op) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
_.label++;
|
|
84
|
-
y = op[1];
|
|
85
|
-
op = [0];
|
|
86
|
-
continue;
|
|
87
|
-
case 7:
|
|
88
|
-
op = _.ops.pop();
|
|
89
|
-
_.trys.pop();
|
|
90
|
-
continue;
|
|
91
|
-
default:
|
|
92
|
-
if (!((t = _.trys), (t = t.length > 0 && t[t.length - 1])) && (op[0] === 6 || op[0] === 2)) {
|
|
93
|
-
_ = 0;
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
|
|
97
|
-
_.label = op[1];
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
101
|
-
_.label = t[1];
|
|
102
|
-
t = op;
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
if (t && _.label < t[2]) {
|
|
106
|
-
_.label = t[2];
|
|
107
|
-
_.ops.push(op);
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
if (t[2]) _.ops.pop();
|
|
111
|
-
_.trys.pop();
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
op = body.call(thisArg, _);
|
|
115
|
-
} catch (e) {
|
|
116
|
-
op = [6, e];
|
|
117
|
-
y = 0;
|
|
118
|
-
} finally {
|
|
119
|
-
f = t = 0;
|
|
120
|
-
}
|
|
121
|
-
if (op[0] & 5) throw op[1];
|
|
122
|
-
return { value: op[0] ? op[1] : void 0, done: true };
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
123
36
|
}
|
|
124
|
-
|
|
125
|
-
Object.defineProperty(exports,
|
|
126
|
-
exports.summarizeConversationHistoryFunction =
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
var
|
|
131
|
-
var
|
|
132
|
-
var
|
|
133
|
-
var util_node_1 = require('@proteinjs/util-node');
|
|
134
|
-
var tiktoken_1 = require('tiktoken');
|
|
135
|
-
var PackageFunctions_1 = require('./fs/package/PackageFunctions');
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.summarizeConversationHistoryFunction = exports.summarizeConversationHistoryFunctionName = exports.Conversation = void 0;
|
|
40
|
+
var OpenAi_1 = require("./OpenAi");
|
|
41
|
+
var MessageHistory_1 = require("./history/MessageHistory");
|
|
42
|
+
var util_1 = require("@proteinjs/util");
|
|
43
|
+
var util_node_1 = require("@proteinjs/util-node");
|
|
44
|
+
var tiktoken_1 = require("tiktoken");
|
|
45
|
+
var PackageFunctions_1 = require("./fs/package/PackageFunctions");
|
|
136
46
|
var Conversation = /** @class */ (function () {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
if ((_d = params.limits) === null || _d === void 0 ? void 0 : _d.tokenLimit) {
|
|
161
|
-
this.tokenLimit = params.limits.tokenLimit;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
Conversation.prototype.addModules = function (modules) {
|
|
165
|
-
for (var _i = 0, modules_1 = modules; _i < modules_1.length; _i++) {
|
|
166
|
-
var module_1 = modules_1[_i];
|
|
167
|
-
if (module_1.getSystemMessages().length < 1) {
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
this.addSystemMessagesToHistory([
|
|
171
|
-
'The following are instructions from the '
|
|
172
|
-
.concat(module_1.getName(), ' module: ')
|
|
173
|
-
.concat(module_1.getSystemMessages().join('. ')),
|
|
174
|
-
]);
|
|
175
|
-
this.addFunctions(module_1.getName(), module_1.getFunctions());
|
|
176
|
-
this.addMessageModerators(module_1.getMessageModerators());
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
Conversation.prototype.addFunctions = function (moduleName, functions) {
|
|
180
|
-
var _a;
|
|
181
|
-
(_a = this.functions).push.apply(_a, functions);
|
|
182
|
-
var functionInstructions = 'The following are instructions from functions in the '.concat(moduleName, ' module:');
|
|
183
|
-
var functionInstructionsAdded = false;
|
|
184
|
-
for (var _i = 0, functions_1 = functions; _i < functions_1.length; _i++) {
|
|
185
|
-
var f = functions_1[_i];
|
|
186
|
-
if (f.instructions) {
|
|
187
|
-
if (!f.instructions || f.instructions.length < 1) {
|
|
188
|
-
continue;
|
|
47
|
+
function Conversation(params) {
|
|
48
|
+
var _a, _b, _c, _d;
|
|
49
|
+
this.tokenLimit = 3000;
|
|
50
|
+
this.systemMessages = [];
|
|
51
|
+
this.functions = [];
|
|
52
|
+
this.messageModerators = [];
|
|
53
|
+
this.generatedCode = false;
|
|
54
|
+
this.generatedList = false;
|
|
55
|
+
this.params = params;
|
|
56
|
+
this.history = new MessageHistory_1.MessageHistory({
|
|
57
|
+
maxMessages: (_a = params.limits) === null || _a === void 0 ? void 0 : _a.maxMessagesInHistory,
|
|
58
|
+
enforceMessageLimit: (_b = params.limits) === null || _b === void 0 ? void 0 : _b.enforceLimits,
|
|
59
|
+
});
|
|
60
|
+
this.logger = new util_1.Logger(params.name, params.logLevel);
|
|
61
|
+
if (params.modules) {
|
|
62
|
+
this.addModules(params.modules);
|
|
63
|
+
}
|
|
64
|
+
if (typeof ((_c = params.limits) === null || _c === void 0 ? void 0 : _c.enforceLimits) === 'undefined' || params.limits.enforceLimits) {
|
|
65
|
+
this.addFunctions('Conversation', [(0, exports.summarizeConversationHistoryFunction)(this)]);
|
|
66
|
+
}
|
|
67
|
+
if ((_d = params.limits) === null || _d === void 0 ? void 0 : _d.tokenLimit) {
|
|
68
|
+
this.tokenLimit = params.limits.tokenLimit;
|
|
189
69
|
}
|
|
190
|
-
functionInstructionsAdded = true;
|
|
191
|
-
var instructionsParagraph = f.instructions.join('. ');
|
|
192
|
-
functionInstructions += ' '.concat(f.definition.name, ': ').concat(instructionsParagraph, '.');
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (!functionInstructionsAdded) {
|
|
196
|
-
return;
|
|
197
70
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
};
|
|
204
|
-
Conversation.prototype.enforceTokenLimit = function (messages, model) {
|
|
205
|
-
var _a;
|
|
206
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
207
|
-
var resolvedModel, encoder, conversation, encoded, summarizeConversationRequest, referenceSummaryRequest;
|
|
208
|
-
return __generator(this, function (_b) {
|
|
209
|
-
switch (_b.label) {
|
|
210
|
-
case 0:
|
|
211
|
-
if (((_a = this.params.limits) === null || _a === void 0 ? void 0 : _a.enforceLimits) === false) {
|
|
212
|
-
return [2 /*return*/];
|
|
71
|
+
Conversation.prototype.addModules = function (modules) {
|
|
72
|
+
for (var _i = 0, modules_1 = modules; _i < modules_1.length; _i++) {
|
|
73
|
+
var module_1 = modules_1[_i];
|
|
74
|
+
if (module_1.getSystemMessages().length < 1) {
|
|
75
|
+
continue;
|
|
213
76
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
77
|
+
this.addSystemMessagesToHistory([
|
|
78
|
+
"The following are instructions from the ".concat(module_1.getName(), " module: ").concat(module_1.getSystemMessages().join('. ')),
|
|
79
|
+
]);
|
|
80
|
+
this.addFunctions(module_1.getName(), module_1.getFunctions());
|
|
81
|
+
this.addMessageModerators(module_1.getMessageModerators());
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
Conversation.prototype.addFunctions = function (moduleName, functions) {
|
|
85
|
+
var _a;
|
|
86
|
+
(_a = this.functions).push.apply(_a, functions);
|
|
87
|
+
var functionInstructions = "The following are instructions from functions in the ".concat(moduleName, " module:");
|
|
88
|
+
var functionInstructionsAdded = false;
|
|
89
|
+
for (var _i = 0, functions_1 = functions; _i < functions_1.length; _i++) {
|
|
90
|
+
var f = functions_1[_i];
|
|
91
|
+
if (f.instructions) {
|
|
92
|
+
if (!f.instructions || f.instructions.length < 1) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
functionInstructionsAdded = true;
|
|
96
|
+
var instructionsParagraph = f.instructions.join('. ');
|
|
97
|
+
functionInstructions += " ".concat(f.definition.name, ": ").concat(instructionsParagraph, ".");
|
|
221
98
|
}
|
|
222
|
-
summarizeConversationRequest = 'First, call the '.concat(
|
|
223
|
-
exports.summarizeConversationHistoryFunctionName,
|
|
224
|
-
' function'
|
|
225
|
-
);
|
|
226
|
-
return [
|
|
227
|
-
4 /*yield*/,
|
|
228
|
-
OpenAi_1.OpenAi.generateResponse(
|
|
229
|
-
[summarizeConversationRequest],
|
|
230
|
-
model,
|
|
231
|
-
this.history,
|
|
232
|
-
this.functions,
|
|
233
|
-
this.messageModerators,
|
|
234
|
-
this.params.logLevel
|
|
235
|
-
),
|
|
236
|
-
];
|
|
237
|
-
case 1:
|
|
238
|
-
_b.sent();
|
|
239
|
-
referenceSummaryRequest =
|
|
240
|
-
"If there's a file mentioned in the conversation summary, find and read the file to better respond to my next request. If that doesn't find anything, call the ".concat(
|
|
241
|
-
PackageFunctions_1.searchLibrariesFunctionName,
|
|
242
|
-
' function on other keywords in the conversation summary to find a file to read'
|
|
243
|
-
);
|
|
244
|
-
return [
|
|
245
|
-
4 /*yield*/,
|
|
246
|
-
OpenAi_1.OpenAi.generateResponse(
|
|
247
|
-
[referenceSummaryRequest],
|
|
248
|
-
model,
|
|
249
|
-
this.history,
|
|
250
|
-
this.functions,
|
|
251
|
-
this.messageModerators,
|
|
252
|
-
this.params.logLevel
|
|
253
|
-
),
|
|
254
|
-
];
|
|
255
|
-
case 2:
|
|
256
|
-
_b.sent();
|
|
257
|
-
return [2 /*return*/];
|
|
258
99
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
};
|
|
262
|
-
Conversation.prototype.summarizeConversationHistory = function (summary) {
|
|
263
|
-
this.clearHistory();
|
|
264
|
-
this.history.push([{ role: 'assistant', content: 'Previous conversation summary: '.concat(summary) }]);
|
|
265
|
-
};
|
|
266
|
-
Conversation.prototype.clearHistory = function () {
|
|
267
|
-
this.history = new MessageHistory_1.MessageHistory();
|
|
268
|
-
this.history.push(this.systemMessages);
|
|
269
|
-
};
|
|
270
|
-
Conversation.prototype.addSystemMessagesToHistory = function (messages, unshift) {
|
|
271
|
-
var _a, _b, _c;
|
|
272
|
-
if (unshift === void 0) {
|
|
273
|
-
unshift = false;
|
|
274
|
-
}
|
|
275
|
-
var chatCompletions = messages.map(function (message) {
|
|
276
|
-
return { role: 'system', content: message };
|
|
277
|
-
});
|
|
278
|
-
if (unshift) {
|
|
279
|
-
(_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
|
|
280
|
-
this.history.prune();
|
|
281
|
-
(_b = this.systemMessages).unshift.apply(_b, chatCompletions);
|
|
282
|
-
} else {
|
|
283
|
-
this.history.push(chatCompletions);
|
|
284
|
-
(_c = this.systemMessages).push.apply(_c, chatCompletions);
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
Conversation.prototype.addAssistantMessagesToHistory = function (messages, unshift) {
|
|
288
|
-
var _a;
|
|
289
|
-
if (unshift === void 0) {
|
|
290
|
-
unshift = false;
|
|
291
|
-
}
|
|
292
|
-
var chatCompletions = messages.map(function (message) {
|
|
293
|
-
return { role: 'assistant', content: message };
|
|
294
|
-
});
|
|
295
|
-
if (unshift) {
|
|
296
|
-
(_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
|
|
297
|
-
this.history.prune();
|
|
298
|
-
} else {
|
|
299
|
-
this.history.push(chatCompletions);
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
Conversation.prototype.addUserMessagesToHistory = function (messages, unshift) {
|
|
303
|
-
var _a;
|
|
304
|
-
if (unshift === void 0) {
|
|
305
|
-
unshift = false;
|
|
306
|
-
}
|
|
307
|
-
var chatCompletions = messages.map(function (message) {
|
|
308
|
-
return { role: 'user', content: message };
|
|
309
|
-
});
|
|
310
|
-
if (unshift) {
|
|
311
|
-
(_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
|
|
312
|
-
this.history.prune();
|
|
313
|
-
} else {
|
|
314
|
-
this.history.push(chatCompletions);
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
Conversation.prototype.generateResponse = function (messages, model) {
|
|
318
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
319
|
-
return __generator(this, function (_a) {
|
|
320
|
-
switch (_a.label) {
|
|
321
|
-
case 0:
|
|
322
|
-
return [4 /*yield*/, this.enforceTokenLimit(messages, model)];
|
|
323
|
-
case 1:
|
|
324
|
-
_a.sent();
|
|
325
|
-
return [
|
|
326
|
-
4 /*yield*/,
|
|
327
|
-
OpenAi_1.OpenAi.generateResponse(
|
|
328
|
-
messages,
|
|
329
|
-
model,
|
|
330
|
-
this.history,
|
|
331
|
-
this.functions,
|
|
332
|
-
this.messageModerators,
|
|
333
|
-
this.params.logLevel
|
|
334
|
-
),
|
|
335
|
-
];
|
|
336
|
-
case 2:
|
|
337
|
-
return [2 /*return*/, _a.sent()];
|
|
100
|
+
if (!functionInstructionsAdded) {
|
|
101
|
+
return;
|
|
338
102
|
}
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
return
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
103
|
+
this.addSystemMessagesToHistory([functionInstructions]);
|
|
104
|
+
};
|
|
105
|
+
Conversation.prototype.addMessageModerators = function (messageModerators) {
|
|
106
|
+
var _a;
|
|
107
|
+
(_a = this.messageModerators).push.apply(_a, messageModerators);
|
|
108
|
+
};
|
|
109
|
+
Conversation.prototype.enforceTokenLimit = function (messages, model) {
|
|
110
|
+
var _a;
|
|
111
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
112
|
+
var resolvedModel, encoder, conversation, encoded, summarizeConversationRequest, referenceSummaryRequest;
|
|
113
|
+
return __generator(this, function (_b) {
|
|
114
|
+
switch (_b.label) {
|
|
115
|
+
case 0:
|
|
116
|
+
if (((_a = this.params.limits) === null || _a === void 0 ? void 0 : _a.enforceLimits) === false) {
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
resolvedModel = model ? model : OpenAi_1.DEFAULT_MODEL;
|
|
120
|
+
encoder = (0, tiktoken_1.encoding_for_model)(resolvedModel);
|
|
121
|
+
conversation = this.history.toString() + messages.join('. ');
|
|
122
|
+
encoded = encoder.encode(conversation);
|
|
123
|
+
console.log("current tokens: ".concat(encoded.length));
|
|
124
|
+
if (encoded.length < this.tokenLimit) {
|
|
125
|
+
return [2 /*return*/];
|
|
126
|
+
}
|
|
127
|
+
summarizeConversationRequest = "First, call the ".concat(exports.summarizeConversationHistoryFunctionName, " function");
|
|
128
|
+
return [4 /*yield*/, OpenAi_1.OpenAi.generateResponse([summarizeConversationRequest], model, this.history, this.functions, this.messageModerators, this.params.logLevel)];
|
|
129
|
+
case 1:
|
|
130
|
+
_b.sent();
|
|
131
|
+
referenceSummaryRequest = "If there's a file mentioned in the conversation summary, find and read the file to better respond to my next request. If that doesn't find anything, call the ".concat(PackageFunctions_1.searchLibrariesFunctionName, " function on other keywords in the conversation summary to find a file to read");
|
|
132
|
+
return [4 /*yield*/, OpenAi_1.OpenAi.generateResponse([referenceSummaryRequest], model, this.history, this.functions, this.messageModerators, this.params.logLevel)];
|
|
133
|
+
case 2:
|
|
134
|
+
_b.sent();
|
|
135
|
+
return [2 /*return*/];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
Conversation.prototype.summarizeConversationHistory = function (summary) {
|
|
141
|
+
this.clearHistory();
|
|
142
|
+
this.history.push([{ role: 'assistant', content: "Previous conversation summary: ".concat(summary) }]);
|
|
143
|
+
};
|
|
144
|
+
Conversation.prototype.clearHistory = function () {
|
|
145
|
+
this.history = new MessageHistory_1.MessageHistory();
|
|
146
|
+
this.history.push(this.systemMessages);
|
|
147
|
+
};
|
|
148
|
+
Conversation.prototype.addSystemMessagesToHistory = function (messages, unshift) {
|
|
149
|
+
var _a, _b, _c;
|
|
150
|
+
if (unshift === void 0) { unshift = false; }
|
|
151
|
+
var chatCompletions = messages.map(function (message) {
|
|
152
|
+
return { role: 'system', content: message };
|
|
153
|
+
});
|
|
154
|
+
if (unshift) {
|
|
155
|
+
(_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
|
|
156
|
+
this.history.prune();
|
|
157
|
+
(_b = this.systemMessages).unshift.apply(_b, chatCompletions);
|
|
366
158
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
Conversation.prototype.updateCodeFromFile = function (
|
|
371
|
-
codeToUpdateFilePath,
|
|
372
|
-
dependencyCodeFilePaths,
|
|
373
|
-
description,
|
|
374
|
-
model
|
|
375
|
-
) {
|
|
376
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
377
|
-
var codeToUpdate, dependencyDescription, _i, dependencyCodeFilePaths_1, dependencyCodeFilePath, dependencCode;
|
|
378
|
-
return __generator(this, function (_a) {
|
|
379
|
-
switch (_a.label) {
|
|
380
|
-
case 0:
|
|
381
|
-
return [4 /*yield*/, util_node_1.Fs.readFile(codeToUpdateFilePath)];
|
|
382
|
-
case 1:
|
|
383
|
-
codeToUpdate = _a.sent();
|
|
384
|
-
dependencyDescription = 'Assume the following exists:\n';
|
|
385
|
-
(_i = 0), (dependencyCodeFilePaths_1 = dependencyCodeFilePaths);
|
|
386
|
-
_a.label = 2;
|
|
387
|
-
case 2:
|
|
388
|
-
if (!(_i < dependencyCodeFilePaths_1.length)) return [3 /*break*/, 5];
|
|
389
|
-
dependencyCodeFilePath = dependencyCodeFilePaths_1[_i];
|
|
390
|
-
return [4 /*yield*/, util_node_1.Fs.readFile(dependencyCodeFilePath)];
|
|
391
|
-
case 3:
|
|
392
|
-
dependencCode = _a.sent();
|
|
393
|
-
dependencyDescription += dependencCode + '\n\n';
|
|
394
|
-
_a.label = 4;
|
|
395
|
-
case 4:
|
|
396
|
-
_i++;
|
|
397
|
-
return [3 /*break*/, 2];
|
|
398
|
-
case 5:
|
|
399
|
-
this.logger.info('Updating code from file: '.concat(codeToUpdateFilePath));
|
|
400
|
-
return [4 /*yield*/, this.updateCode(codeToUpdate, dependencyDescription + description, model)];
|
|
401
|
-
case 6:
|
|
402
|
-
return [2 /*return*/, _a.sent()];
|
|
159
|
+
else {
|
|
160
|
+
this.history.push(chatCompletions);
|
|
161
|
+
(_c = this.systemMessages).push.apply(_c, chatCompletions);
|
|
403
162
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
this.
|
|
414
|
-
'Updating code:\n'
|
|
415
|
-
.concat(code.slice(0, 150))
|
|
416
|
-
.concat(code.length > 150 ? '...' : '', '\nFrom description: ')
|
|
417
|
-
.concat(description)
|
|
418
|
-
);
|
|
419
|
-
return [
|
|
420
|
-
4 /*yield*/,
|
|
421
|
-
OpenAi_1.OpenAi.updateCode(
|
|
422
|
-
code,
|
|
423
|
-
description,
|
|
424
|
-
model,
|
|
425
|
-
this.history,
|
|
426
|
-
this.functions,
|
|
427
|
-
this.messageModerators,
|
|
428
|
-
!this.generatedCode,
|
|
429
|
-
this.params.logLevel
|
|
430
|
-
),
|
|
431
|
-
];
|
|
432
|
-
case 1:
|
|
433
|
-
updatedCode = _a.sent();
|
|
434
|
-
this.logger.info(
|
|
435
|
-
'Updated code:\n'.concat(updatedCode.slice(0, 150)).concat(updatedCode.length > 150 ? '...' : '')
|
|
436
|
-
);
|
|
437
|
-
this.generatedCode = true;
|
|
438
|
-
return [2 /*return*/, updatedCode];
|
|
163
|
+
};
|
|
164
|
+
Conversation.prototype.addAssistantMessagesToHistory = function (messages, unshift) {
|
|
165
|
+
var _a;
|
|
166
|
+
if (unshift === void 0) { unshift = false; }
|
|
167
|
+
var chatCompletions = messages.map(function (message) {
|
|
168
|
+
return { role: 'assistant', content: message };
|
|
169
|
+
});
|
|
170
|
+
if (unshift) {
|
|
171
|
+
(_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
|
|
172
|
+
this.history.prune();
|
|
439
173
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
};
|
|
443
|
-
Conversation.prototype.generateList = function (description, model) {
|
|
444
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
445
|
-
var list;
|
|
446
|
-
return __generator(this, function (_a) {
|
|
447
|
-
switch (_a.label) {
|
|
448
|
-
case 0:
|
|
449
|
-
return [
|
|
450
|
-
4 /*yield*/,
|
|
451
|
-
OpenAi_1.OpenAi.generateList(
|
|
452
|
-
description,
|
|
453
|
-
model,
|
|
454
|
-
this.history,
|
|
455
|
-
this.functions,
|
|
456
|
-
this.messageModerators,
|
|
457
|
-
!this.generatedList,
|
|
458
|
-
this.params.logLevel
|
|
459
|
-
),
|
|
460
|
-
];
|
|
461
|
-
case 1:
|
|
462
|
-
list = _a.sent();
|
|
463
|
-
this.generatedList = true;
|
|
464
|
-
return [2 /*return*/, list];
|
|
174
|
+
else {
|
|
175
|
+
this.history.push(chatCompletions);
|
|
465
176
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
177
|
+
};
|
|
178
|
+
Conversation.prototype.addUserMessagesToHistory = function (messages, unshift) {
|
|
179
|
+
var _a;
|
|
180
|
+
if (unshift === void 0) { unshift = false; }
|
|
181
|
+
var chatCompletions = messages.map(function (message) {
|
|
182
|
+
return { role: 'user', content: message };
|
|
183
|
+
});
|
|
184
|
+
if (unshift) {
|
|
185
|
+
(_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
|
|
186
|
+
this.history.prune();
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
this.history.push(chatCompletions);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
Conversation.prototype.generateResponse = function (messages, model) {
|
|
193
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
194
|
+
return __generator(this, function (_a) {
|
|
195
|
+
switch (_a.label) {
|
|
196
|
+
case 0: return [4 /*yield*/, this.enforceTokenLimit(messages, model)];
|
|
197
|
+
case 1:
|
|
198
|
+
_a.sent();
|
|
199
|
+
return [4 /*yield*/, OpenAi_1.OpenAi.generateResponse(messages, model, this.history, this.functions, this.messageModerators, this.params.logLevel)];
|
|
200
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
Conversation.prototype.generateCode = function (description, model) {
|
|
206
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
207
|
+
var code;
|
|
208
|
+
return __generator(this, function (_a) {
|
|
209
|
+
switch (_a.label) {
|
|
210
|
+
case 0:
|
|
211
|
+
this.logger.info("Generating code for description:\n".concat(description.join('\n')));
|
|
212
|
+
return [4 /*yield*/, OpenAi_1.OpenAi.generateCode(description, model, this.history, this.functions, this.messageModerators, !this.generatedCode, this.params.logLevel)];
|
|
213
|
+
case 1:
|
|
214
|
+
code = _a.sent();
|
|
215
|
+
this.logger.info("Generated code:\n".concat(code.slice(0, 150)).concat(code.length > 150 ? '...' : ''));
|
|
216
|
+
this.generatedCode = true;
|
|
217
|
+
return [2 /*return*/, code];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
Conversation.prototype.updateCodeFromFile = function (codeToUpdateFilePath, dependencyCodeFilePaths, description, model) {
|
|
223
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
224
|
+
var codeToUpdate, dependencyDescription, _i, dependencyCodeFilePaths_1, dependencyCodeFilePath, dependencCode;
|
|
225
|
+
return __generator(this, function (_a) {
|
|
226
|
+
switch (_a.label) {
|
|
227
|
+
case 0: return [4 /*yield*/, util_node_1.Fs.readFile(codeToUpdateFilePath)];
|
|
228
|
+
case 1:
|
|
229
|
+
codeToUpdate = _a.sent();
|
|
230
|
+
dependencyDescription = "Assume the following exists:\n";
|
|
231
|
+
_i = 0, dependencyCodeFilePaths_1 = dependencyCodeFilePaths;
|
|
232
|
+
_a.label = 2;
|
|
233
|
+
case 2:
|
|
234
|
+
if (!(_i < dependencyCodeFilePaths_1.length)) return [3 /*break*/, 5];
|
|
235
|
+
dependencyCodeFilePath = dependencyCodeFilePaths_1[_i];
|
|
236
|
+
return [4 /*yield*/, util_node_1.Fs.readFile(dependencyCodeFilePath)];
|
|
237
|
+
case 3:
|
|
238
|
+
dependencCode = _a.sent();
|
|
239
|
+
dependencyDescription += dependencCode + '\n\n';
|
|
240
|
+
_a.label = 4;
|
|
241
|
+
case 4:
|
|
242
|
+
_i++;
|
|
243
|
+
return [3 /*break*/, 2];
|
|
244
|
+
case 5:
|
|
245
|
+
this.logger.info("Updating code from file: ".concat(codeToUpdateFilePath));
|
|
246
|
+
return [4 /*yield*/, this.updateCode(codeToUpdate, dependencyDescription + description, model)];
|
|
247
|
+
case 6: return [2 /*return*/, _a.sent()];
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
Conversation.prototype.updateCode = function (code, description, model) {
|
|
253
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
254
|
+
var updatedCode;
|
|
255
|
+
return __generator(this, function (_a) {
|
|
256
|
+
switch (_a.label) {
|
|
257
|
+
case 0:
|
|
258
|
+
this.logger.info("Updating code:\n".concat(code.slice(0, 150)).concat(code.length > 150 ? '...' : '', "\nFrom description: ").concat(description));
|
|
259
|
+
return [4 /*yield*/, OpenAi_1.OpenAi.updateCode(code, description, model, this.history, this.functions, this.messageModerators, !this.generatedCode, this.params.logLevel)];
|
|
260
|
+
case 1:
|
|
261
|
+
updatedCode = _a.sent();
|
|
262
|
+
this.logger.info("Updated code:\n".concat(updatedCode.slice(0, 150)).concat(updatedCode.length > 150 ? '...' : ''));
|
|
263
|
+
this.generatedCode = true;
|
|
264
|
+
return [2 /*return*/, updatedCode];
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
};
|
|
269
|
+
Conversation.prototype.generateList = function (description, model) {
|
|
270
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
271
|
+
var list;
|
|
272
|
+
return __generator(this, function (_a) {
|
|
273
|
+
switch (_a.label) {
|
|
274
|
+
case 0: return [4 /*yield*/, OpenAi_1.OpenAi.generateList(description, model, this.history, this.functions, this.messageModerators, !this.generatedList, this.params.logLevel)];
|
|
275
|
+
case 1:
|
|
276
|
+
list = _a.sent();
|
|
277
|
+
this.generatedList = true;
|
|
278
|
+
return [2 /*return*/, list];
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
};
|
|
283
|
+
return Conversation;
|
|
284
|
+
}());
|
|
471
285
|
exports.Conversation = Conversation;
|
|
472
286
|
exports.summarizeConversationHistoryFunctionName = 'summarizeConversationHistory';
|
|
473
287
|
var summarizeConversationHistoryFunction = function (conversation) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
288
|
+
return {
|
|
289
|
+
definition: {
|
|
290
|
+
name: exports.summarizeConversationHistoryFunctionName,
|
|
291
|
+
description: 'Clear the conversation history and summarize what was in it',
|
|
292
|
+
parameters: {
|
|
293
|
+
type: 'object',
|
|
294
|
+
properties: {
|
|
295
|
+
summary: {
|
|
296
|
+
type: 'string',
|
|
297
|
+
description: 'A 1-3 sentence summary of the current chat history',
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
required: ['summary'],
|
|
301
|
+
},
|
|
485
302
|
},
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
491
|
-
return __generator(this, function (_a) {
|
|
492
|
-
return [2 /*return*/, conversation.summarizeConversationHistory(params.summary)];
|
|
493
|
-
});
|
|
494
|
-
});
|
|
495
|
-
},
|
|
496
|
-
};
|
|
303
|
+
call: function (params) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
304
|
+
return [2 /*return*/, conversation.summarizeConversationHistory(params.summary)];
|
|
305
|
+
}); }); },
|
|
306
|
+
};
|
|
497
307
|
};
|
|
498
308
|
exports.summarizeConversationHistoryFunction = summarizeConversationHistoryFunction;
|
|
499
|
-
//# sourceMappingURL=Conversation.js.map
|
|
309
|
+
//# sourceMappingURL=Conversation.js.map
|