@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/OpenAi.js
CHANGED
|
@@ -1,512 +1,322 @@
|
|
|
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,
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
126
39
|
exports.OpenAi = exports.DEFAULT_MODEL = void 0;
|
|
127
|
-
var openai_1 = require(
|
|
128
|
-
var util_1 = require(
|
|
129
|
-
var MessageHistory_1 = require(
|
|
40
|
+
var openai_1 = require("openai");
|
|
41
|
+
var util_1 = require("@proteinjs/util");
|
|
42
|
+
var MessageHistory_1 = require("./history/MessageHistory");
|
|
130
43
|
function delay(ms) {
|
|
131
|
-
|
|
132
|
-
return setTimeout(resolve, ms);
|
|
133
|
-
});
|
|
44
|
+
return new Promise(function (resolve) { return setTimeout(resolve, ms); });
|
|
134
45
|
}
|
|
135
46
|
exports.DEFAULT_MODEL = 'gpt-3.5-turbo';
|
|
136
47
|
var OpenAi = /** @class */ (function () {
|
|
137
|
-
|
|
138
|
-
OpenAi.generateResponse = function (messages, model, history, functions, messageModerators, logLevel) {
|
|
139
|
-
if (logLevel === void 0) {
|
|
140
|
-
logLevel = 'info';
|
|
48
|
+
function OpenAi() {
|
|
141
49
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
50
|
+
OpenAi.generateResponse = function (messages, model, history, functions, messageModerators, logLevel) {
|
|
51
|
+
if (logLevel === void 0) { logLevel = 'info'; }
|
|
52
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
53
|
+
var logger, messageParams, messageParamsWithHistory, response, responseMessage, functionReturnMessage, responseText;
|
|
54
|
+
return __generator(this, function (_a) {
|
|
55
|
+
switch (_a.label) {
|
|
56
|
+
case 0:
|
|
57
|
+
logger = new util_1.Logger('OpenAi.generateResponse', logLevel);
|
|
58
|
+
messageParams = messages.map(function (message) {
|
|
59
|
+
return { role: 'user', content: message };
|
|
60
|
+
});
|
|
61
|
+
if (history) {
|
|
62
|
+
history.push(messageParams);
|
|
63
|
+
}
|
|
64
|
+
messageParamsWithHistory = history ? history : new MessageHistory_1.MessageHistory().push(messageParams);
|
|
65
|
+
if (messageModerators) {
|
|
66
|
+
messageParamsWithHistory = OpenAi.moderateHistory(messageParamsWithHistory, messageModerators);
|
|
67
|
+
}
|
|
68
|
+
return [4 /*yield*/, OpenAi.executeRequest(messageParamsWithHistory, logLevel, functions, model)];
|
|
69
|
+
case 1:
|
|
70
|
+
response = _a.sent();
|
|
71
|
+
responseMessage = response.choices[0].message;
|
|
72
|
+
if (!responseMessage.function_call) return [3 /*break*/, 4];
|
|
73
|
+
messageParamsWithHistory.push([responseMessage]);
|
|
74
|
+
return [4 /*yield*/, this.callFunction(logLevel, responseMessage.function_call, functions)];
|
|
75
|
+
case 2:
|
|
76
|
+
functionReturnMessage = _a.sent();
|
|
77
|
+
if (functionReturnMessage) {
|
|
78
|
+
messageParamsWithHistory.push([functionReturnMessage]);
|
|
79
|
+
}
|
|
80
|
+
return [4 /*yield*/, this.generateResponse([], model, messageParamsWithHistory, functions, messageModerators, logLevel)];
|
|
81
|
+
case 3: return [2 /*return*/, _a.sent()];
|
|
82
|
+
case 4:
|
|
83
|
+
responseText = responseMessage.content;
|
|
84
|
+
if (!responseText) {
|
|
85
|
+
logger.error("Received response: ".concat(JSON.stringify(response)));
|
|
86
|
+
throw new Error("Response was empty for messages: ".concat(messages.join('\n')));
|
|
87
|
+
}
|
|
88
|
+
messageParamsWithHistory.push([responseMessage]);
|
|
89
|
+
return [2 /*return*/, responseText];
|
|
90
|
+
}
|
|
156
91
|
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
return [4 /*yield*/, OpenAi.executeRequest(messageParamsWithHistory, logLevel, functions, model)];
|
|
165
|
-
case 1:
|
|
166
|
-
response = _a.sent();
|
|
167
|
-
responseMessage = response.choices[0].message;
|
|
168
|
-
if (!responseMessage.function_call) return [3 /*break*/, 4];
|
|
169
|
-
messageParamsWithHistory.push([responseMessage]);
|
|
170
|
-
return [4 /*yield*/, this.callFunction(logLevel, responseMessage.function_call, functions)];
|
|
171
|
-
case 2:
|
|
172
|
-
functionReturnMessage = _a.sent();
|
|
173
|
-
if (functionReturnMessage) {
|
|
174
|
-
messageParamsWithHistory.push([functionReturnMessage]);
|
|
175
|
-
}
|
|
176
|
-
return [
|
|
177
|
-
4 /*yield*/,
|
|
178
|
-
this.generateResponse([], model, messageParamsWithHistory, functions, messageModerators, logLevel),
|
|
179
|
-
];
|
|
180
|
-
case 3:
|
|
181
|
-
return [2 /*return*/, _a.sent()];
|
|
182
|
-
case 4:
|
|
183
|
-
responseText = responseMessage.content;
|
|
184
|
-
if (!responseText) {
|
|
185
|
-
logger.error('Received response: '.concat(JSON.stringify(response)));
|
|
186
|
-
throw new Error('Response was empty for messages: '.concat(messages.join('\n')));
|
|
187
|
-
}
|
|
188
|
-
messageParamsWithHistory.push([responseMessage]);
|
|
189
|
-
return [2 /*return*/, responseText];
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
OpenAi.moderateHistory = function (history, messageModerators) {
|
|
95
|
+
for (var _i = 0, messageModerators_1 = messageModerators; _i < messageModerators_1.length; _i++) {
|
|
96
|
+
var messageModerator = messageModerators_1[_i];
|
|
97
|
+
history.setMessages(messageModerator.observe(history.getMessages()));
|
|
190
98
|
}
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
99
|
+
return history;
|
|
100
|
+
};
|
|
101
|
+
OpenAi.executeRequest = function (messageParamsWithHistory, logLevel, functions, model) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
103
|
+
var logger, openaiApi, response, latestMessage, responseMessage, error_1, waitTime, remainingTokens, delayMs;
|
|
104
|
+
return __generator(this, function (_a) {
|
|
105
|
+
switch (_a.label) {
|
|
106
|
+
case 0:
|
|
107
|
+
logger = new util_1.Logger('OpenAi.executeRequest', logLevel);
|
|
108
|
+
openaiApi = new openai_1.OpenAI();
|
|
109
|
+
_a.label = 1;
|
|
110
|
+
case 1:
|
|
111
|
+
_a.trys.push([1, 3, , 7]);
|
|
112
|
+
latestMessage = messageParamsWithHistory.getMessages()[messageParamsWithHistory.getMessages().length - 1];
|
|
113
|
+
if (latestMessage.content) {
|
|
114
|
+
logger.info("Sending request: ".concat(latestMessage.content));
|
|
115
|
+
}
|
|
116
|
+
else if (latestMessage.role == 'function') {
|
|
117
|
+
logger.info("Sending request: returning output of ".concat(latestMessage.name, " function"));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
logger.info("Sending request");
|
|
121
|
+
}
|
|
122
|
+
logger.debug("Sending messages: ".concat(JSON.stringify(messageParamsWithHistory.getMessages(), null, 2)), true);
|
|
123
|
+
return [4 /*yield*/, openaiApi.chat.completions.create({
|
|
124
|
+
model: model ? model : exports.DEFAULT_MODEL,
|
|
125
|
+
temperature: 0,
|
|
126
|
+
messages: messageParamsWithHistory.getMessages(),
|
|
127
|
+
functions: functions === null || functions === void 0 ? void 0 : functions.map(function (f) { return f.definition; }),
|
|
128
|
+
})];
|
|
129
|
+
case 2:
|
|
130
|
+
response = _a.sent();
|
|
131
|
+
responseMessage = response.choices[0].message;
|
|
132
|
+
if (responseMessage.content) {
|
|
133
|
+
logger.info("Received response: ".concat(responseMessage.content));
|
|
134
|
+
}
|
|
135
|
+
else if (responseMessage.function_call) {
|
|
136
|
+
logger.info("Received response: call ".concat(responseMessage.function_call.name, " function"));
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
logger.info("Received response");
|
|
140
|
+
}
|
|
141
|
+
if (response.usage) {
|
|
142
|
+
logger.info(JSON.stringify(response.usage));
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
logger.info(JSON.stringify("Usage data missing"));
|
|
146
|
+
}
|
|
147
|
+
return [3 /*break*/, 7];
|
|
148
|
+
case 3:
|
|
149
|
+
error_1 = _a.sent();
|
|
150
|
+
logger.info("Received error response, error type: ".concat(error_1.type));
|
|
151
|
+
if (!(typeof error_1.status !== 'undefined' && error_1.status == 429)) return [3 /*break*/, 6];
|
|
152
|
+
if (!(error_1.type == 'tokens' && typeof error_1.headers['x-ratelimit-reset-tokens'] === 'string')) return [3 /*break*/, 6];
|
|
153
|
+
waitTime = parseInt(error_1.headers['x-ratelimit-reset-tokens']);
|
|
154
|
+
remainingTokens = error_1.headers['x-ratelimit-remaining-tokens'];
|
|
155
|
+
delayMs = 15000;
|
|
156
|
+
logger.warn("Waiting to retry in ".concat(delayMs / 1000, "s, token reset in: ").concat(waitTime, "s, remaining tokens: ").concat(remainingTokens));
|
|
157
|
+
return [4 /*yield*/, delay(delayMs)];
|
|
158
|
+
case 4:
|
|
159
|
+
_a.sent();
|
|
160
|
+
return [4 /*yield*/, OpenAi.executeRequest(messageParamsWithHistory, logLevel, functions, model)];
|
|
161
|
+
case 5: return [2 /*return*/, _a.sent()];
|
|
162
|
+
case 6: throw error_1;
|
|
163
|
+
case 7: return [2 /*return*/, response];
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
OpenAi.callFunction = function (logLevel, functionCall, functions) {
|
|
169
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
170
|
+
var logger, warning, message, f, warning, message, returnObject, _a, _b, error_2;
|
|
171
|
+
return __generator(this, function (_c) {
|
|
172
|
+
switch (_c.label) {
|
|
173
|
+
case 0:
|
|
174
|
+
logger = new util_1.Logger('OpenAi.callFunction', logLevel);
|
|
175
|
+
if (!functions) {
|
|
176
|
+
warning = "Assistant attempted to call a function when no functions were provided";
|
|
177
|
+
logger.warn(warning);
|
|
178
|
+
message = { role: 'user', content: warning };
|
|
179
|
+
return [2 /*return*/, message];
|
|
180
|
+
}
|
|
181
|
+
functionCall.name = functionCall.name.split('.').pop();
|
|
182
|
+
f = functions.find(function (f) { return f.definition.name === functionCall.name; });
|
|
183
|
+
if (!f) {
|
|
184
|
+
warning = "Assistant attempted to call nonexistent function: ".concat(functionCall.name);
|
|
185
|
+
logger.warn(warning);
|
|
186
|
+
message = { role: 'user', content: warning };
|
|
187
|
+
return [2 /*return*/, message];
|
|
188
|
+
}
|
|
189
|
+
returnObject = null;
|
|
190
|
+
_c.label = 1;
|
|
191
|
+
case 1:
|
|
192
|
+
_c.trys.push([1, 3, , 4]);
|
|
193
|
+
logger.info("Assistant calling function: ".concat(f.definition.name, "(").concat(functionCall.arguments, ")"));
|
|
194
|
+
_b = (_a = JSON).stringify;
|
|
195
|
+
return [4 /*yield*/, f.call(JSON.parse(functionCall.arguments))];
|
|
196
|
+
case 2:
|
|
197
|
+
returnObject = _b.apply(_a, [_c.sent()]);
|
|
198
|
+
logger.info("Assistant called function: ".concat(f.definition.name, "(").concat(functionCall.arguments, ") => ").concat(returnObject), 1000);
|
|
199
|
+
return [3 /*break*/, 4];
|
|
200
|
+
case 3:
|
|
201
|
+
error_2 = _c.sent();
|
|
202
|
+
logger.error(error_2.message);
|
|
203
|
+
return [3 /*break*/, 4];
|
|
204
|
+
case 4:
|
|
205
|
+
if (!returnObject) {
|
|
206
|
+
return [2 /*return*/];
|
|
207
|
+
}
|
|
208
|
+
return [2 /*return*/, {
|
|
209
|
+
role: 'function',
|
|
210
|
+
name: f.definition.name,
|
|
211
|
+
content: returnObject,
|
|
212
|
+
}];
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
OpenAi.generateCode = function (messages, model, history, functions, messageModerators, includeSystemMessages, logLevel) {
|
|
218
|
+
if (includeSystemMessages === void 0) { includeSystemMessages = true; }
|
|
219
|
+
if (logLevel === void 0) { logLevel = 'info'; }
|
|
220
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
221
|
+
var systemMessages, resolvedHistory, code;
|
|
222
|
+
return __generator(this, function (_a) {
|
|
223
|
+
switch (_a.label) {
|
|
224
|
+
case 0:
|
|
225
|
+
systemMessages = [
|
|
226
|
+
{
|
|
227
|
+
role: 'system',
|
|
228
|
+
content: 'Return only the code and exclude example usage, markdown, explanations, comments and notes.',
|
|
229
|
+
},
|
|
230
|
+
{ role: 'system', content: "Write code in typescript." },
|
|
231
|
+
{ role: 'system', content: "Declare explicit types for all function parameters." },
|
|
232
|
+
{ role: 'system', content: 'Export all functions and objects generated.' },
|
|
233
|
+
{ role: 'system', content: 'Do not omit function implementations.' },
|
|
234
|
+
];
|
|
235
|
+
resolvedHistory = history
|
|
236
|
+
? includeSystemMessages
|
|
237
|
+
? history.push(systemMessages)
|
|
238
|
+
: history
|
|
239
|
+
: includeSystemMessages
|
|
240
|
+
? new MessageHistory_1.MessageHistory().push(systemMessages)
|
|
241
|
+
: undefined;
|
|
242
|
+
return [4 /*yield*/, this.generateResponse(messages, model, resolvedHistory, functions, messageModerators, logLevel)];
|
|
243
|
+
case 1:
|
|
244
|
+
code = _a.sent();
|
|
245
|
+
return [2 /*return*/, this.parseCodeFromMarkdown(code)];
|
|
246
|
+
}
|
|
299
247
|
});
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
OpenAi.updateCode = function (code, description, model, history, functions, messageModerators, includeSystemMessages, logLevel) {
|
|
251
|
+
if (includeSystemMessages === void 0) { includeSystemMessages = true; }
|
|
252
|
+
if (logLevel === void 0) { logLevel = 'info'; }
|
|
253
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
254
|
+
return __generator(this, function (_a) {
|
|
255
|
+
switch (_a.label) {
|
|
256
|
+
case 0: return [4 /*yield*/, this.generateCode([this.updateCodeDescription(code, description)], model, history, functions, messageModerators, includeSystemMessages, logLevel)];
|
|
257
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
OpenAi.updateCodeDescription = function (code, description) {
|
|
263
|
+
return "Update this code:\n\n".concat(code, "\n\n").concat(description);
|
|
264
|
+
};
|
|
265
|
+
OpenAi.parseCodeFromMarkdown = function (code) {
|
|
266
|
+
if (!code.match(/```([\s\S]+?)```/g)) {
|
|
267
|
+
return code;
|
|
268
|
+
}
|
|
269
|
+
var filteredLines = [];
|
|
270
|
+
var inCodeBlock = false;
|
|
271
|
+
for (var _i = 0, _a = code.split('\n'); _i < _a.length; _i++) {
|
|
272
|
+
var line = _a[_i];
|
|
273
|
+
if (line.startsWith('```')) {
|
|
274
|
+
inCodeBlock = !inCodeBlock;
|
|
275
|
+
if (!inCodeBlock) {
|
|
276
|
+
filteredLines.push('');
|
|
277
|
+
}
|
|
278
|
+
continue;
|
|
305
279
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
case 1:
|
|
309
|
-
_c.trys.push([1, 3, , 4]);
|
|
310
|
-
logger.info(
|
|
311
|
-
'Assistant calling function: '.concat(f.definition.name, '(').concat(functionCall.arguments, ')')
|
|
312
|
-
);
|
|
313
|
-
_b = (_a = JSON).stringify;
|
|
314
|
-
return [4 /*yield*/, f.call(JSON.parse(functionCall.arguments))];
|
|
315
|
-
case 2:
|
|
316
|
-
returnObject = _b.apply(_a, [_c.sent()]);
|
|
317
|
-
logger.info(
|
|
318
|
-
'Assistant called function: '
|
|
319
|
-
.concat(f.definition.name, '(')
|
|
320
|
-
.concat(functionCall.arguments, ') => ')
|
|
321
|
-
.concat(returnObject),
|
|
322
|
-
1000
|
|
323
|
-
);
|
|
324
|
-
return [3 /*break*/, 4];
|
|
325
|
-
case 3:
|
|
326
|
-
error_2 = _c.sent();
|
|
327
|
-
logger.error(error_2.message);
|
|
328
|
-
return [3 /*break*/, 4];
|
|
329
|
-
case 4:
|
|
330
|
-
if (!returnObject) {
|
|
331
|
-
return [2 /*return*/];
|
|
280
|
+
if (inCodeBlock) {
|
|
281
|
+
filteredLines.push(line);
|
|
332
282
|
}
|
|
333
|
-
return [
|
|
334
|
-
2 /*return*/,
|
|
335
|
-
{
|
|
336
|
-
role: 'function',
|
|
337
|
-
name: f.definition.name,
|
|
338
|
-
content: returnObject,
|
|
339
|
-
},
|
|
340
|
-
];
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
});
|
|
344
|
-
};
|
|
345
|
-
OpenAi.generateCode = function (
|
|
346
|
-
messages,
|
|
347
|
-
model,
|
|
348
|
-
history,
|
|
349
|
-
functions,
|
|
350
|
-
messageModerators,
|
|
351
|
-
includeSystemMessages,
|
|
352
|
-
logLevel
|
|
353
|
-
) {
|
|
354
|
-
if (includeSystemMessages === void 0) {
|
|
355
|
-
includeSystemMessages = true;
|
|
356
|
-
}
|
|
357
|
-
if (logLevel === void 0) {
|
|
358
|
-
logLevel = 'info';
|
|
359
|
-
}
|
|
360
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
361
|
-
var systemMessages, resolvedHistory, code;
|
|
362
|
-
return __generator(this, function (_a) {
|
|
363
|
-
switch (_a.label) {
|
|
364
|
-
case 0:
|
|
365
|
-
systemMessages = [
|
|
366
|
-
{
|
|
367
|
-
role: 'system',
|
|
368
|
-
content: 'Return only the code and exclude example usage, markdown, explanations, comments and notes.',
|
|
369
|
-
},
|
|
370
|
-
{ role: 'system', content: 'Write code in typescript.' },
|
|
371
|
-
{ role: 'system', content: 'Declare explicit types for all function parameters.' },
|
|
372
|
-
{ role: 'system', content: 'Export all functions and objects generated.' },
|
|
373
|
-
{ role: 'system', content: 'Do not omit function implementations.' },
|
|
374
|
-
];
|
|
375
|
-
resolvedHistory = history
|
|
376
|
-
? includeSystemMessages
|
|
377
|
-
? history.push(systemMessages)
|
|
378
|
-
: history
|
|
379
|
-
: includeSystemMessages
|
|
380
|
-
? new MessageHistory_1.MessageHistory().push(systemMessages)
|
|
381
|
-
: undefined;
|
|
382
|
-
return [
|
|
383
|
-
4 /*yield*/,
|
|
384
|
-
this.generateResponse(messages, model, resolvedHistory, functions, messageModerators, logLevel),
|
|
385
|
-
];
|
|
386
|
-
case 1:
|
|
387
|
-
code = _a.sent();
|
|
388
|
-
return [2 /*return*/, this.parseCodeFromMarkdown(code)];
|
|
389
|
-
}
|
|
390
|
-
});
|
|
391
|
-
});
|
|
392
|
-
};
|
|
393
|
-
OpenAi.updateCode = function (
|
|
394
|
-
code,
|
|
395
|
-
description,
|
|
396
|
-
model,
|
|
397
|
-
history,
|
|
398
|
-
functions,
|
|
399
|
-
messageModerators,
|
|
400
|
-
includeSystemMessages,
|
|
401
|
-
logLevel
|
|
402
|
-
) {
|
|
403
|
-
if (includeSystemMessages === void 0) {
|
|
404
|
-
includeSystemMessages = true;
|
|
405
|
-
}
|
|
406
|
-
if (logLevel === void 0) {
|
|
407
|
-
logLevel = 'info';
|
|
408
|
-
}
|
|
409
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
410
|
-
return __generator(this, function (_a) {
|
|
411
|
-
switch (_a.label) {
|
|
412
|
-
case 0:
|
|
413
|
-
return [
|
|
414
|
-
4 /*yield*/,
|
|
415
|
-
this.generateCode(
|
|
416
|
-
[this.updateCodeDescription(code, description)],
|
|
417
|
-
model,
|
|
418
|
-
history,
|
|
419
|
-
functions,
|
|
420
|
-
messageModerators,
|
|
421
|
-
includeSystemMessages,
|
|
422
|
-
logLevel
|
|
423
|
-
),
|
|
424
|
-
];
|
|
425
|
-
case 1:
|
|
426
|
-
return [2 /*return*/, _a.sent()];
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
});
|
|
430
|
-
};
|
|
431
|
-
OpenAi.updateCodeDescription = function (code, description) {
|
|
432
|
-
return 'Update this code:\n\n'.concat(code, '\n\n').concat(description);
|
|
433
|
-
};
|
|
434
|
-
OpenAi.parseCodeFromMarkdown = function (code) {
|
|
435
|
-
if (!code.match(/```([\s\S]+?)```/g)) {
|
|
436
|
-
return code;
|
|
437
|
-
}
|
|
438
|
-
var filteredLines = [];
|
|
439
|
-
var inCodeBlock = false;
|
|
440
|
-
for (var _i = 0, _a = code.split('\n'); _i < _a.length; _i++) {
|
|
441
|
-
var line = _a[_i];
|
|
442
|
-
if (line.startsWith('```')) {
|
|
443
|
-
inCodeBlock = !inCodeBlock;
|
|
444
|
-
if (!inCodeBlock) {
|
|
445
|
-
filteredLines.push('');
|
|
446
|
-
}
|
|
447
|
-
continue;
|
|
448
|
-
}
|
|
449
|
-
if (inCodeBlock) {
|
|
450
|
-
filteredLines.push(line);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
// remove the last '' that will become a \n
|
|
454
|
-
// we only want spaces between code blocks
|
|
455
|
-
filteredLines.pop();
|
|
456
|
-
return filteredLines.join('\n');
|
|
457
|
-
};
|
|
458
|
-
OpenAi.generateList = function (
|
|
459
|
-
messages,
|
|
460
|
-
model,
|
|
461
|
-
history,
|
|
462
|
-
functions,
|
|
463
|
-
messageModerators,
|
|
464
|
-
includeSystemMessages,
|
|
465
|
-
logLevel
|
|
466
|
-
) {
|
|
467
|
-
if (includeSystemMessages === void 0) {
|
|
468
|
-
includeSystemMessages = true;
|
|
469
|
-
}
|
|
470
|
-
if (logLevel === void 0) {
|
|
471
|
-
logLevel = 'info';
|
|
472
|
-
}
|
|
473
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
474
|
-
var systemMessages, resolvedHistory, list;
|
|
475
|
-
return __generator(this, function (_a) {
|
|
476
|
-
switch (_a.label) {
|
|
477
|
-
case 0:
|
|
478
|
-
systemMessages = [
|
|
479
|
-
{
|
|
480
|
-
role: 'system',
|
|
481
|
-
content:
|
|
482
|
-
'Return only the list and exclude example usage, markdown and all explanations, comments and notes.',
|
|
483
|
-
},
|
|
484
|
-
{ role: 'system', content: 'Separate each item in the list by a ;' },
|
|
485
|
-
];
|
|
486
|
-
resolvedHistory = history
|
|
487
|
-
? includeSystemMessages
|
|
488
|
-
? history.push(systemMessages)
|
|
489
|
-
: history
|
|
490
|
-
: includeSystemMessages
|
|
491
|
-
? new MessageHistory_1.MessageHistory().push(systemMessages)
|
|
492
|
-
: undefined;
|
|
493
|
-
return [
|
|
494
|
-
4 /*yield*/,
|
|
495
|
-
this.generateResponse(messages, model, resolvedHistory, functions, messageModerators, logLevel),
|
|
496
|
-
];
|
|
497
|
-
case 1:
|
|
498
|
-
list = _a.sent();
|
|
499
|
-
return [
|
|
500
|
-
2 /*return*/,
|
|
501
|
-
list.split(';').map(function (item) {
|
|
502
|
-
return item.trim();
|
|
503
|
-
}),
|
|
504
|
-
];
|
|
505
283
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
}
|
|
284
|
+
// remove the last '' that will become a \n
|
|
285
|
+
// we only want spaces between code blocks
|
|
286
|
+
filteredLines.pop();
|
|
287
|
+
return filteredLines.join('\n');
|
|
288
|
+
};
|
|
289
|
+
OpenAi.generateList = function (messages, model, history, functions, messageModerators, includeSystemMessages, logLevel) {
|
|
290
|
+
if (includeSystemMessages === void 0) { includeSystemMessages = true; }
|
|
291
|
+
if (logLevel === void 0) { logLevel = 'info'; }
|
|
292
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
293
|
+
var systemMessages, resolvedHistory, list;
|
|
294
|
+
return __generator(this, function (_a) {
|
|
295
|
+
switch (_a.label) {
|
|
296
|
+
case 0:
|
|
297
|
+
systemMessages = [
|
|
298
|
+
{
|
|
299
|
+
role: 'system',
|
|
300
|
+
content: 'Return only the list and exclude example usage, markdown and all explanations, comments and notes.',
|
|
301
|
+
},
|
|
302
|
+
{ role: 'system', content: 'Separate each item in the list by a ;' },
|
|
303
|
+
];
|
|
304
|
+
resolvedHistory = history
|
|
305
|
+
? includeSystemMessages
|
|
306
|
+
? history.push(systemMessages)
|
|
307
|
+
: history
|
|
308
|
+
: includeSystemMessages
|
|
309
|
+
? new MessageHistory_1.MessageHistory().push(systemMessages)
|
|
310
|
+
: undefined;
|
|
311
|
+
return [4 /*yield*/, this.generateResponse(messages, model, resolvedHistory, functions, messageModerators, logLevel)];
|
|
312
|
+
case 1:
|
|
313
|
+
list = _a.sent();
|
|
314
|
+
return [2 /*return*/, list.split(';').map(function (item) { return item.trim(); })];
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
});
|
|
318
|
+
};
|
|
319
|
+
return OpenAi;
|
|
320
|
+
}());
|
|
511
321
|
exports.OpenAi = OpenAi;
|
|
512
|
-
//# sourceMappingURL=OpenAi.js.map
|
|
322
|
+
//# sourceMappingURL=OpenAi.js.map
|