@proteinjs/conversation 1.0.6 → 1.0.7

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.
Files changed (152) hide show
  1. package/.eslintrc.js +20 -0
  2. package/.prettierrc +8 -0
  3. package/CHANGELOG.md +7 -4
  4. package/LICENSE +21 -0
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.js +40 -28
  7. package/dist/src/CodegenConversation.d.ts +11 -11
  8. package/dist/src/CodegenConversation.d.ts.map +1 -1
  9. package/dist/src/CodegenConversation.js +294 -180
  10. package/dist/src/CodegenConversation.js.map +1 -1
  11. package/dist/src/Conversation.d.ts +52 -49
  12. package/dist/src/Conversation.d.ts.map +1 -1
  13. package/dist/src/Conversation.js +480 -274
  14. package/dist/src/Conversation.js.map +1 -1
  15. package/dist/src/ConversationModule.d.ts +6 -6
  16. package/dist/src/ConversationModule.js +3 -3
  17. package/dist/src/Function.d.ts +4 -4
  18. package/dist/src/Function.d.ts.map +1 -1
  19. package/dist/src/Function.js +3 -3
  20. package/dist/src/OpenAi.d.ts +42 -10
  21. package/dist/src/OpenAi.d.ts.map +1 -1
  22. package/dist/src/OpenAi.js +496 -289
  23. package/dist/src/OpenAi.js.map +1 -1
  24. package/dist/src/Paragraph.d.ts +4 -4
  25. package/dist/src/Paragraph.d.ts.map +1 -1
  26. package/dist/src/Paragraph.js +17 -16
  27. package/dist/src/Paragraph.js.map +1 -1
  28. package/dist/src/Sentence.d.ts +4 -4
  29. package/dist/src/Sentence.d.ts.map +1 -1
  30. package/dist/src/Sentence.js +21 -19
  31. package/dist/src/Sentence.js.map +1 -1
  32. package/dist/src/code_template/Code.d.ts +15 -15
  33. package/dist/src/code_template/Code.d.ts.map +1 -1
  34. package/dist/src/code_template/Code.js +167 -69
  35. package/dist/src/code_template/Code.js.map +1 -1
  36. package/dist/src/code_template/CodeTemplate.d.ts +11 -11
  37. package/dist/src/code_template/CodeTemplate.d.ts.map +1 -1
  38. package/dist/src/code_template/CodeTemplate.js +169 -79
  39. package/dist/src/code_template/CodeTemplate.js.map +1 -1
  40. package/dist/src/code_template/CodeTemplateModule.d.ts +6 -6
  41. package/dist/src/code_template/CodeTemplateModule.js +28 -26
  42. package/dist/src/code_template/Repo.d.ts +34 -38
  43. package/dist/src/code_template/Repo.d.ts.map +1 -1
  44. package/dist/src/code_template/Repo.js +291 -191
  45. package/dist/src/code_template/Repo.js.map +1 -1
  46. package/dist/src/fs/conversation_fs/ConversationFsModerator.d.ts +12 -12
  47. package/dist/src/fs/conversation_fs/ConversationFsModerator.d.ts.map +1 -1
  48. package/dist/src/fs/conversation_fs/ConversationFsModerator.js +111 -98
  49. package/dist/src/fs/conversation_fs/ConversationFsModerator.js.map +1 -1
  50. package/dist/src/fs/conversation_fs/ConversationFsModule.d.ts +11 -11
  51. package/dist/src/fs/conversation_fs/ConversationFsModule.d.ts.map +1 -1
  52. package/dist/src/fs/conversation_fs/ConversationFsModule.js +204 -99
  53. package/dist/src/fs/conversation_fs/ConversationFsModule.js.map +1 -1
  54. package/dist/src/fs/conversation_fs/FsFunctions.d.ts +58 -62
  55. package/dist/src/fs/conversation_fs/FsFunctions.d.ts.map +1 -1
  56. package/dist/src/fs/conversation_fs/FsFunctions.js +414 -266
  57. package/dist/src/fs/conversation_fs/FsFunctions.js.map +1 -1
  58. package/dist/src/fs/git/GitModule.d.ts +8 -8
  59. package/dist/src/fs/git/GitModule.d.ts.map +1 -1
  60. package/dist/src/fs/git/GitModule.js +163 -74
  61. package/dist/src/fs/git/GitModule.js.map +1 -1
  62. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexFunctions.d.ts +16 -18
  63. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexFunctions.d.ts.map +1 -1
  64. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexFunctions.js +158 -58
  65. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexFunctions.js.map +1 -1
  66. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.d.ts +27 -26
  67. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.d.ts.map +1 -1
  68. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.js +234 -133
  69. package/dist/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.js.map +1 -1
  70. package/dist/src/fs/package/PackageFunctions.d.ts +54 -60
  71. package/dist/src/fs/package/PackageFunctions.d.ts.map +1 -1
  72. package/dist/src/fs/package/PackageFunctions.js +366 -223
  73. package/dist/src/fs/package/PackageFunctions.js.map +1 -1
  74. package/dist/src/fs/package/PackageModule.d.ts +24 -24
  75. package/dist/src/fs/package/PackageModule.d.ts.map +1 -1
  76. package/dist/src/fs/package/PackageModule.js +292 -163
  77. package/dist/src/fs/package/PackageModule.js.map +1 -1
  78. package/dist/src/history/MessageHistory.d.ts +12 -12
  79. package/dist/src/history/MessageHistory.d.ts.map +1 -1
  80. package/dist/src/history/MessageHistory.js +52 -43
  81. package/dist/src/history/MessageHistory.js.map +1 -1
  82. package/dist/src/history/MessageModerator.d.ts +2 -2
  83. package/dist/src/history/MessageModerator.js +3 -3
  84. package/dist/src/template/ConversationTemplate.d.ts +8 -8
  85. package/dist/src/template/ConversationTemplate.d.ts.map +1 -1
  86. package/dist/src/template/ConversationTemplate.js +3 -3
  87. package/dist/src/template/ConversationTemplateFunctions.d.ts +33 -35
  88. package/dist/src/template/ConversationTemplateFunctions.d.ts.map +1 -1
  89. package/dist/src/template/ConversationTemplateFunctions.js +176 -75
  90. package/dist/src/template/ConversationTemplateFunctions.js.map +1 -1
  91. package/dist/src/template/ConversationTemplateModule.d.ts +51 -48
  92. package/dist/src/template/ConversationTemplateModule.d.ts.map +1 -1
  93. package/dist/src/template/ConversationTemplateModule.js +211 -114
  94. package/dist/src/template/ConversationTemplateModule.js.map +1 -1
  95. package/dist/src/template/createApp/CreateAppTemplate.d.ts +1 -1
  96. package/dist/src/template/createApp/CreateAppTemplate.js +151 -59
  97. package/dist/src/template/createCode/CreateCodeConversationTemplate.d.ts +1 -1
  98. package/dist/src/template/createCode/CreateCodeConversationTemplate.d.ts.map +1 -1
  99. package/dist/src/template/createCode/CreateCodeConversationTemplate.js +183 -74
  100. package/dist/src/template/createCode/CreateCodeConversationTemplate.js.map +1 -1
  101. package/dist/src/template/createPackage/CreatePackageConversationTemplate.d.ts +1 -1
  102. package/dist/src/template/createPackage/CreatePackageConversationTemplate.d.ts.map +1 -1
  103. package/dist/src/template/createPackage/CreatePackageConversationTemplate.js +174 -84
  104. package/dist/src/template/createPackage/CreatePackageConversationTemplate.js.map +1 -1
  105. package/dist/src/template/createPackage/tsconfig.json +11 -11
  106. package/dist/test/createKeywordFilesIndex.test.d.ts +1 -1
  107. package/dist/test/createKeywordFilesIndex.test.js +132 -41
  108. package/dist/test/createKeywordFilesIndex.test.js.map +1 -1
  109. package/dist/test/openai/openai.generateList.test.d.ts +1 -1
  110. package/dist/test/openai/openai.generateList.test.js +136 -47
  111. package/dist/test/openai/openai.generateList.test.js.map +1 -1
  112. package/dist/test/openai/openai.parseCodeFromMarkdown.test.d.ts +1 -1
  113. package/dist/test/openai/openai.parseCodeFromMarkdown.test.js +15 -10
  114. package/dist/test/openai/openai.parseCodeFromMarkdown.test.js.map +1 -1
  115. package/dist/test/repo/repo.test.d.ts +1 -1
  116. package/dist/test/repo/repo.test.js +127 -38
  117. package/dist/test/repo/repo.test.js.map +1 -1
  118. package/jest.config.js +2 -2
  119. package/package.json +9 -3
  120. package/src/CodegenConversation.ts +6 -4
  121. package/src/Conversation.ts +102 -33
  122. package/src/ConversationModule.ts +2 -2
  123. package/src/Function.ts +0 -1
  124. package/src/OpenAi.ts +128 -63
  125. package/src/Paragraph.ts +3 -2
  126. package/src/Sentence.ts +5 -3
  127. package/src/code_template/Code.ts +25 -19
  128. package/src/code_template/CodeTemplate.ts +8 -7
  129. package/src/code_template/CodeTemplateModule.ts +2 -2
  130. package/src/code_template/Repo.ts +25 -19
  131. package/src/fs/conversation_fs/ConversationFsModerator.ts +34 -20
  132. package/src/fs/conversation_fs/ConversationFsModule.ts +13 -7
  133. package/src/fs/conversation_fs/FsFunctions.ts +33 -44
  134. package/src/fs/git/GitModule.ts +2 -4
  135. package/src/fs/keyword_to_files_index/KeywordToFilesIndexFunctions.ts +31 -31
  136. package/src/fs/keyword_to_files_index/KeywordToFilesIndexModule.ts +19 -18
  137. package/src/fs/package/PackageFunctions.ts +34 -41
  138. package/src/fs/package/PackageModule.ts +33 -21
  139. package/src/history/MessageHistory.ts +7 -4
  140. package/src/history/MessageModerator.ts +1 -1
  141. package/src/template/ConversationTemplate.ts +9 -9
  142. package/src/template/ConversationTemplateFunctions.ts +8 -7
  143. package/src/template/ConversationTemplateModule.ts +24 -15
  144. package/src/template/createApp/CreateAppTemplate.ts +1 -1
  145. package/src/template/createCode/CreateCodeConversationTemplate.ts +9 -11
  146. package/src/template/createPackage/CreatePackageConversationTemplate.ts +2 -9
  147. package/src/template/createPackage/jest.config.js +2 -2
  148. package/test/createKeywordFilesIndex.test.ts +3 -3
  149. package/test/openai/openai.generateList.test.ts +5 -3
  150. package/test/openai/openai.parseCodeFromMarkdown.test.ts +10 -5
  151. package/test/repo/repo.test.ts +3 -4
  152. package/tsconfig.json +16 -20
@@ -1,293 +1,499 @@
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); }); }
1
+ 'use strict';
2
+ var __awaiter =
3
+ (this && this.__awaiter) ||
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
+ }
4
12
  return new (P || (P = Promise))(function (resolve, reject) {
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());
13
+ function fulfilled(value) {
14
+ try {
15
+ step(generator.next(value));
16
+ } catch (e) {
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());
9
31
  });
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]); }; }
32
+ };
33
+ var __generator =
34
+ (this && this.__generator) ||
35
+ function (thisArg, body) {
36
+ var _ = {
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
+ }
15
62
  function step(op) {
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 };
63
+ if (f) throw new TypeError('Generator is already executing.');
64
+ while ((g && ((g = 0), op[0] && (_ = 0)), _))
65
+ try {
66
+ if (
67
+ ((f = 1),
68
+ y &&
69
+ (t = op[0] & 2 ? y['return'] : op[0] ? y['throw'] || ((t = y['return']) && t.call(y), 0) : y.next) &&
70
+ !(t = t.call(y, op[1])).done)
71
+ )
72
+ return t;
73
+ if (((y = 0), t)) op = [op[0] & 2, t.value];
74
+ switch (op[0]) {
75
+ case 0:
76
+ case 1:
77
+ t = op;
78
+ break;
79
+ case 4:
80
+ _.label++;
81
+ return { value: op[1], done: false };
82
+ case 5:
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 };
36
123
  }
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");
124
+ };
125
+ Object.defineProperty(exports, '__esModule', { value: true });
126
+ exports.summarizeConversationHistoryFunction =
127
+ exports.summarizeConversationHistoryFunctionName =
128
+ exports.Conversation =
129
+ void 0;
130
+ var OpenAi_1 = require('./OpenAi');
131
+ var MessageHistory_1 = require('./history/MessageHistory');
132
+ var util_1 = require('@proteinjs/util');
133
+ var util_node_1 = require('@proteinjs/util-node');
134
+ var tiktoken_1 = require('tiktoken');
135
+ var PackageFunctions_1 = require('./fs/package/PackageFunctions');
46
136
  var Conversation = /** @class */ (function () {
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({ maxMessages: (_a = params.limits) === null || _a === void 0 ? void 0 : _a.maxMessagesInHistory, enforceMessageLimit: (_b = params.limits) === null || _b === void 0 ? void 0 : _b.enforceLimits });
57
- this.logger = new util_1.Logger(params.name, params.logLevel);
58
- if (params.modules)
59
- this.addModules(params.modules);
60
- if (typeof ((_c = params.limits) === null || _c === void 0 ? void 0 : _c.enforceLimits) === 'undefined' || params.limits.enforceLimits) {
61
- this.addFunctions('Conversation', [
62
- (0, exports.summarizeConversationHistoryFunction)(this),
63
- ]);
64
- }
65
- if ((_d = params.limits) === null || _d === void 0 ? void 0 : _d.tokenLimit)
66
- this.tokenLimit = params.limits.tokenLimit;
137
+ function Conversation(params) {
138
+ var _a, _b, _c, _d;
139
+ this.tokenLimit = 3000;
140
+ this.systemMessages = [];
141
+ this.functions = [];
142
+ this.messageModerators = [];
143
+ this.generatedCode = false;
144
+ this.generatedList = false;
145
+ this.params = params;
146
+ this.history = new MessageHistory_1.MessageHistory({
147
+ maxMessages: (_a = params.limits) === null || _a === void 0 ? void 0 : _a.maxMessagesInHistory,
148
+ enforceMessageLimit: (_b = params.limits) === null || _b === void 0 ? void 0 : _b.enforceLimits,
149
+ });
150
+ this.logger = new util_1.Logger(params.name, params.logLevel);
151
+ if (params.modules) {
152
+ this.addModules(params.modules);
67
153
  }
68
- Conversation.prototype.addModules = function (modules) {
69
- for (var _i = 0, modules_1 = modules; _i < modules_1.length; _i++) {
70
- var module_1 = modules_1[_i];
71
- if (module_1.getSystemMessages().length < 1)
72
- continue;
73
- this.addSystemMessagesToHistory([
74
- "The following are instructions from the ".concat(module_1.getName(), " module: ").concat(module_1.getSystemMessages().join('. ')),
75
- ]);
76
- this.addFunctions(module_1.getName(), module_1.getFunctions());
77
- this.addMessageModerators(module_1.getMessageModerators());
154
+ if (
155
+ typeof ((_c = params.limits) === null || _c === void 0 ? void 0 : _c.enforceLimits) === 'undefined' ||
156
+ params.limits.enforceLimits
157
+ ) {
158
+ this.addFunctions('Conversation', [(0, exports.summarizeConversationHistoryFunction)(this)]);
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;
78
189
  }
79
- };
80
- Conversation.prototype.addFunctions = function (moduleName, functions) {
81
- var _a;
82
- (_a = this.functions).push.apply(_a, functions);
83
- var functionInstructions = "The following are instructions from functions in the ".concat(moduleName, " module:");
84
- var functionInstructionsAdded = false;
85
- for (var _i = 0, functions_1 = functions; _i < functions_1.length; _i++) {
86
- var f = functions_1[_i];
87
- if (f.instructions) {
88
- if (!f.instructions || f.instructions.length < 1)
89
- continue;
90
- functionInstructionsAdded = true;
91
- var instructionsParagraph = f.instructions.join('. ');
92
- functionInstructions += " ".concat(f.definition.name, ": ").concat(instructionsParagraph, ".");
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
+ }
198
+ this.addSystemMessagesToHistory([functionInstructions]);
199
+ };
200
+ Conversation.prototype.addMessageModerators = function (messageModerators) {
201
+ var _a;
202
+ (_a = this.messageModerators).push.apply(_a, messageModerators);
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*/];
213
+ }
214
+ resolvedModel = model ? model : OpenAi_1.DEFAULT_MODEL;
215
+ encoder = (0, tiktoken_1.encoding_for_model)(resolvedModel);
216
+ conversation = this.history.toString() + messages.join('. ');
217
+ encoded = encoder.encode(conversation);
218
+ console.log('current tokens: '.concat(encoded.length));
219
+ if (encoded.length < this.tokenLimit) {
220
+ return [2 /*return*/];
93
221
  }
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*/];
94
258
  }
95
- if (!functionInstructionsAdded)
96
- return;
97
- this.addSystemMessagesToHistory([functionInstructions]);
98
- };
99
- Conversation.prototype.addMessageModerators = function (messageModerators) {
100
- var _a;
101
- (_a = this.messageModerators).push.apply(_a, messageModerators);
102
- };
103
- Conversation.prototype.enforceTokenLimit = function (messages, model) {
104
- var _a;
105
- return __awaiter(this, void 0, void 0, function () {
106
- var resolvedModel, encoder, conversation, encoded, summarizeConversationRequest, referenceSummaryRequest;
107
- return __generator(this, function (_b) {
108
- switch (_b.label) {
109
- case 0:
110
- if (((_a = this.params.limits) === null || _a === void 0 ? void 0 : _a.enforceLimits) === false)
111
- return [2 /*return*/];
112
- resolvedModel = model ? model : OpenAi_1.DEFAULT_MODEL;
113
- encoder = (0, tiktoken_1.encoding_for_model)(resolvedModel);
114
- conversation = this.history.toString() + messages.join('. ');
115
- encoded = encoder.encode(conversation);
116
- console.log("current tokens: ".concat(encoded.length));
117
- if (encoded.length < this.tokenLimit)
118
- return [2 /*return*/];
119
- summarizeConversationRequest = "First, call the ".concat(exports.summarizeConversationHistoryFunctionName, " function");
120
- return [4 /*yield*/, OpenAi_1.OpenAi.generateResponse([summarizeConversationRequest], model, this.history, this.functions, this.messageModerators, this.params.logLevel)];
121
- case 1:
122
- _b.sent();
123
- 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");
124
- return [4 /*yield*/, OpenAi_1.OpenAi.generateResponse([referenceSummaryRequest], model, this.history, this.functions, this.messageModerators, this.params.logLevel)];
125
- case 2:
126
- _b.sent();
127
- return [2 /*return*/];
128
- }
129
- });
130
- });
131
- };
132
- Conversation.prototype.summarizeConversationHistory = function (summary) {
133
- this.clearHistory();
134
- this.history.push([{ role: 'assistant', content: "Previous conversation summary: ".concat(summary) }]);
135
- };
136
- Conversation.prototype.clearHistory = function () {
137
- this.history = new MessageHistory_1.MessageHistory();
138
- this.history.push(this.systemMessages);
139
- };
140
- Conversation.prototype.addSystemMessagesToHistory = function (messages, unshift) {
141
- var _a, _b, _c;
142
- if (unshift === void 0) { unshift = false; }
143
- var chatCompletions = messages.map(function (message) { return { role: 'system', content: message }; });
144
- if (unshift) {
145
- (_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
146
- this.history.prune();
147
- (_b = this.systemMessages).unshift.apply(_b, chatCompletions);
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()];
148
338
  }
149
- else {
150
- this.history.push(chatCompletions);
151
- (_c = this.systemMessages).push.apply(_c, chatCompletions);
339
+ });
340
+ });
341
+ };
342
+ Conversation.prototype.generateCode = function (description, model) {
343
+ return __awaiter(this, void 0, void 0, function () {
344
+ var code;
345
+ return __generator(this, function (_a) {
346
+ switch (_a.label) {
347
+ case 0:
348
+ this.logger.info('Generating code for description:\n'.concat(description.join('\n')));
349
+ return [
350
+ 4 /*yield*/,
351
+ OpenAi_1.OpenAi.generateCode(
352
+ description,
353
+ model,
354
+ this.history,
355
+ this.functions,
356
+ this.messageModerators,
357
+ !this.generatedCode,
358
+ this.params.logLevel
359
+ ),
360
+ ];
361
+ case 1:
362
+ code = _a.sent();
363
+ this.logger.info('Generated code:\n'.concat(code.slice(0, 150)).concat(code.length > 150 ? '...' : ''));
364
+ this.generatedCode = true;
365
+ return [2 /*return*/, code];
152
366
  }
153
- };
154
- Conversation.prototype.addAssistantMessagesToHistory = function (messages, unshift) {
155
- var _a;
156
- if (unshift === void 0) { unshift = false; }
157
- var chatCompletions = messages.map(function (message) { return { role: 'assistant', content: message }; });
158
- if (unshift) {
159
- (_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
160
- this.history.prune();
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()];
161
403
  }
162
- else
163
- this.history.push(chatCompletions);
164
- };
165
- Conversation.prototype.addUserMessagesToHistory = function (messages, unshift) {
166
- var _a;
167
- if (unshift === void 0) { unshift = false; }
168
- var chatCompletions = messages.map(function (message) { return { role: 'user', content: message }; });
169
- if (unshift) {
170
- (_a = this.history.getMessages()).unshift.apply(_a, chatCompletions);
171
- this.history.prune();
404
+ });
405
+ });
406
+ };
407
+ Conversation.prototype.updateCode = function (code, description, model) {
408
+ return __awaiter(this, void 0, void 0, function () {
409
+ var updatedCode;
410
+ return __generator(this, function (_a) {
411
+ switch (_a.label) {
412
+ case 0:
413
+ this.logger.info(
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];
172
439
  }
173
- else
174
- this.history.push(chatCompletions);
175
- };
176
- Conversation.prototype.generateResponse = function (messages, model) {
177
- return __awaiter(this, void 0, void 0, function () {
178
- return __generator(this, function (_a) {
179
- switch (_a.label) {
180
- case 0: return [4 /*yield*/, this.enforceTokenLimit(messages, model)];
181
- case 1:
182
- _a.sent();
183
- return [4 /*yield*/, OpenAi_1.OpenAi.generateResponse(messages, model, this.history, this.functions, this.messageModerators, this.params.logLevel)];
184
- case 2: return [2 /*return*/, _a.sent()];
185
- }
186
- });
187
- });
188
- };
189
- Conversation.prototype.generateCode = function (description, model) {
190
- return __awaiter(this, void 0, void 0, function () {
191
- var code;
192
- return __generator(this, function (_a) {
193
- switch (_a.label) {
194
- case 0:
195
- this.logger.info("Generating code for description:\n".concat(description.join('\n')));
196
- return [4 /*yield*/, OpenAi_1.OpenAi.generateCode(description, model, this.history, this.functions, this.messageModerators, !this.generatedCode, this.params.logLevel)];
197
- case 1:
198
- code = _a.sent();
199
- this.logger.info("Generated code:\n".concat(code.slice(0, 150)).concat(code.length > 150 ? '...' : ''));
200
- this.generatedCode = true;
201
- return [2 /*return*/, code];
202
- }
203
- });
204
- });
205
- };
206
- Conversation.prototype.updateCodeFromFile = function (codeToUpdateFilePath, dependencyCodeFilePaths, description, model) {
207
- return __awaiter(this, void 0, void 0, function () {
208
- var codeToUpdate, dependencyDescription, _i, dependencyCodeFilePaths_1, dependencyCodeFilePath, dependencCode;
209
- return __generator(this, function (_a) {
210
- switch (_a.label) {
211
- case 0: return [4 /*yield*/, util_node_1.Fs.readFile(codeToUpdateFilePath)];
212
- case 1:
213
- codeToUpdate = _a.sent();
214
- dependencyDescription = "Assume the following exists:\n";
215
- _i = 0, dependencyCodeFilePaths_1 = dependencyCodeFilePaths;
216
- _a.label = 2;
217
- case 2:
218
- if (!(_i < dependencyCodeFilePaths_1.length)) return [3 /*break*/, 5];
219
- dependencyCodeFilePath = dependencyCodeFilePaths_1[_i];
220
- return [4 /*yield*/, util_node_1.Fs.readFile(dependencyCodeFilePath)];
221
- case 3:
222
- dependencCode = _a.sent();
223
- dependencyDescription += dependencCode + '\n\n';
224
- _a.label = 4;
225
- case 4:
226
- _i++;
227
- return [3 /*break*/, 2];
228
- case 5:
229
- this.logger.info("Updating code from file: ".concat(codeToUpdateFilePath));
230
- return [4 /*yield*/, this.updateCode(codeToUpdate, dependencyDescription + description, model)];
231
- case 6: return [2 /*return*/, _a.sent()];
232
- }
233
- });
234
- });
235
- };
236
- Conversation.prototype.updateCode = function (code, description, model) {
237
- return __awaiter(this, void 0, void 0, function () {
238
- var updatedCode;
239
- return __generator(this, function (_a) {
240
- switch (_a.label) {
241
- case 0:
242
- this.logger.info("Updating code:\n".concat(code.slice(0, 150)).concat(code.length > 150 ? '...' : '', "\nFrom description: ").concat(description));
243
- return [4 /*yield*/, OpenAi_1.OpenAi.updateCode(code, description, model, this.history, this.functions, this.messageModerators, !this.generatedCode, this.params.logLevel)];
244
- case 1:
245
- updatedCode = _a.sent();
246
- this.logger.info("Updated code:\n".concat(updatedCode.slice(0, 150)).concat(updatedCode.length > 150 ? '...' : ''));
247
- this.generatedCode = true;
248
- return [2 /*return*/, updatedCode];
249
- }
250
- });
251
- });
252
- };
253
- Conversation.prototype.generateList = function (description, model) {
254
- return __awaiter(this, void 0, void 0, function () {
255
- var list;
256
- return __generator(this, function (_a) {
257
- switch (_a.label) {
258
- case 0: return [4 /*yield*/, OpenAi_1.OpenAi.generateList(description, model, this.history, this.functions, this.messageModerators, !this.generatedList, this.params.logLevel)];
259
- case 1:
260
- list = _a.sent();
261
- this.generatedList = true;
262
- return [2 /*return*/, list];
263
- }
264
- });
265
- });
266
- };
267
- return Conversation;
268
- }());
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];
465
+ }
466
+ });
467
+ });
468
+ };
469
+ return Conversation;
470
+ })();
269
471
  exports.Conversation = Conversation;
270
472
  exports.summarizeConversationHistoryFunctionName = 'summarizeConversationHistory';
271
473
  var summarizeConversationHistoryFunction = function (conversation) {
272
- return {
273
- definition: {
274
- name: exports.summarizeConversationHistoryFunctionName,
275
- description: 'Clear the conversation history and summarize what was in it',
276
- parameters: {
277
- type: 'object',
278
- properties: {
279
- summary: {
280
- type: 'string',
281
- description: 'A 1-3 sentence summary of the current chat history',
282
- },
283
- },
284
- required: ['summary']
285
- },
474
+ return {
475
+ definition: {
476
+ name: exports.summarizeConversationHistoryFunctionName,
477
+ description: 'Clear the conversation history and summarize what was in it',
478
+ parameters: {
479
+ type: 'object',
480
+ properties: {
481
+ summary: {
482
+ type: 'string',
483
+ description: 'A 1-3 sentence summary of the current chat history',
484
+ },
286
485
  },
287
- call: function (params) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
288
- return [2 /*return*/, conversation.summarizeConversationHistory(params.summary)];
289
- }); }); },
290
- };
486
+ required: ['summary'],
487
+ },
488
+ },
489
+ call: function (params) {
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
+ };
291
497
  };
292
498
  exports.summarizeConversationHistoryFunction = summarizeConversationHistoryFunction;
293
- //# sourceMappingURL=Conversation.js.map
499
+ //# sourceMappingURL=Conversation.js.map