@aigne/core 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/lib/cjs/index.js +1 -1
  2. package/lib/cjs/types/index.js +2 -4
  3. package/lib/esm/index.js +1 -1
  4. package/lib/esm/types/index.js +2 -4
  5. package/lib/types/index.d.ts +1 -1
  6. package/lib/types/types/agent.d.ts +5 -0
  7. package/lib/types/types/index.d.ts +2 -4
  8. package/lib/types/types/runnable.d.ts +20 -0
  9. package/package.json +6 -7
  10. package/lib/cjs/assistant/generate-output.js +0 -101
  11. package/lib/cjs/assistant/select-agent.js +0 -76
  12. package/lib/cjs/assistant/type.js +0 -11
  13. package/lib/cjs/common/aid.js +0 -42
  14. package/lib/cjs/common/index.js +0 -238
  15. package/lib/cjs/common/resource-manager.js +0 -199
  16. package/lib/cjs/constants.js +0 -9
  17. package/lib/cjs/executor/agent.js +0 -10
  18. package/lib/cjs/executor/aigc.js +0 -28
  19. package/lib/cjs/executor/api.js +0 -64
  20. package/lib/cjs/executor/base.js +0 -676
  21. package/lib/cjs/executor/blocklet.js +0 -25
  22. package/lib/cjs/executor/call-agent.js +0 -105
  23. package/lib/cjs/executor/decision.js +0 -478
  24. package/lib/cjs/executor/image-blender.js +0 -32
  25. package/lib/cjs/executor/index.js +0 -81
  26. package/lib/cjs/executor/llm.js +0 -379
  27. package/lib/cjs/executor/logic.js +0 -167
  28. package/lib/cjs/libs/blocklet/vc.js +0 -92
  29. package/lib/cjs/libs/openapi/request/index.js +0 -24
  30. package/lib/cjs/libs/openapi/request/util.js +0 -146
  31. package/lib/cjs/libs/openapi/types/index.js +0 -17
  32. package/lib/cjs/libs/openapi/util/call.js +0 -15
  33. package/lib/cjs/libs/openapi/util/check-schema.js +0 -67
  34. package/lib/cjs/libs/openapi/util/convert-schema.js +0 -44
  35. package/lib/cjs/libs/openapi/util/flatten-open-api.js +0 -21
  36. package/lib/cjs/libs/openapi/util/get-open-api-i18n-text.js +0 -7
  37. package/lib/cjs/runtime/resource-blocklet.js +0 -5
  38. package/lib/cjs/runtime/runtime.js +0 -143
  39. package/lib/cjs/types/assistant/index.js +0 -31
  40. package/lib/cjs/types/assistant/mustache/ReadableMustache.js +0 -69
  41. package/lib/cjs/types/assistant/mustache/directive.js +0 -35
  42. package/lib/cjs/types/assistant/mustache/mustache.js +0 -688
  43. package/lib/cjs/types/resource/index.js +0 -47
  44. package/lib/cjs/types/resource/project.js +0 -35
  45. package/lib/cjs/types/runtime/error.js +0 -18
  46. package/lib/cjs/types/runtime/index.js +0 -37
  47. package/lib/cjs/types/runtime/runtime-resource-blocklet-state.js +0 -4
  48. package/lib/cjs/types/runtime/schema.js +0 -259
  49. package/lib/cjs/utils/cron-job.js +0 -48
  50. package/lib/cjs/utils/extract-metadata-transform.js +0 -58
  51. package/lib/cjs/utils/extract-metadata-transform.test.js +0 -61
  52. package/lib/cjs/utils/fs.js +0 -49
  53. package/lib/cjs/utils/get-blocklet-agent.js +0 -351
  54. package/lib/cjs/utils/geti.js +0 -37
  55. package/lib/cjs/utils/is-non-nullable.js +0 -20
  56. package/lib/cjs/utils/render-message.js +0 -23
  57. package/lib/cjs/utils/resolve-secret-inputs.js +0 -49
  58. package/lib/cjs/utils/retry.js +0 -19
  59. package/lib/cjs/utils/task-id.js +0 -7
  60. package/lib/cjs/utils/tool-calls-transform.js +0 -18
  61. package/lib/esm/assistant/generate-output.js +0 -91
  62. package/lib/esm/assistant/select-agent.js +0 -71
  63. package/lib/esm/assistant/type.js +0 -7
  64. package/lib/esm/common/aid.js +0 -38
  65. package/lib/esm/common/index.js +0 -232
  66. package/lib/esm/common/resource-manager.js +0 -192
  67. package/lib/esm/constants.js +0 -6
  68. package/lib/esm/executor/agent.js +0 -6
  69. package/lib/esm/executor/aigc.js +0 -24
  70. package/lib/esm/executor/api.js +0 -34
  71. package/lib/esm/executor/base.js +0 -668
  72. package/lib/esm/executor/blocklet.js +0 -21
  73. package/lib/esm/executor/call-agent.js +0 -98
  74. package/lib/esm/executor/decision.js +0 -471
  75. package/lib/esm/executor/image-blender.js +0 -25
  76. package/lib/esm/executor/index.js +0 -74
  77. package/lib/esm/executor/llm.js +0 -372
  78. package/lib/esm/executor/logic.js +0 -160
  79. package/lib/esm/libs/blocklet/vc.js +0 -85
  80. package/lib/esm/libs/openapi/request/index.js +0 -20
  81. package/lib/esm/libs/openapi/request/util.js +0 -136
  82. package/lib/esm/libs/openapi/types/index.js +0 -1
  83. package/lib/esm/libs/openapi/util/call.js +0 -8
  84. package/lib/esm/libs/openapi/util/check-schema.js +0 -62
  85. package/lib/esm/libs/openapi/util/convert-schema.js +0 -42
  86. package/lib/esm/libs/openapi/util/flatten-open-api.js +0 -19
  87. package/lib/esm/libs/openapi/util/get-open-api-i18n-text.js +0 -5
  88. package/lib/esm/runtime/resource-blocklet.js +0 -2
  89. package/lib/esm/runtime/runtime.js +0 -136
  90. package/lib/esm/types/assistant/index.js +0 -9
  91. package/lib/esm/types/assistant/mustache/ReadableMustache.js +0 -63
  92. package/lib/esm/types/assistant/mustache/directive.js +0 -29
  93. package/lib/esm/types/assistant/mustache/mustache.js +0 -686
  94. package/lib/esm/types/resource/index.js +0 -26
  95. package/lib/esm/types/resource/project.js +0 -29
  96. package/lib/esm/types/runtime/error.js +0 -14
  97. package/lib/esm/types/runtime/index.js +0 -20
  98. package/lib/esm/types/runtime/runtime-resource-blocklet-state.js +0 -1
  99. package/lib/esm/types/runtime/schema.js +0 -249
  100. package/lib/esm/utils/cron-job.js +0 -44
  101. package/lib/esm/utils/extract-metadata-transform.js +0 -54
  102. package/lib/esm/utils/extract-metadata-transform.test.js +0 -59
  103. package/lib/esm/utils/fs.js +0 -41
  104. package/lib/esm/utils/get-blocklet-agent.js +0 -344
  105. package/lib/esm/utils/geti.js +0 -30
  106. package/lib/esm/utils/is-non-nullable.js +0 -13
  107. package/lib/esm/utils/render-message.js +0 -20
  108. package/lib/esm/utils/resolve-secret-inputs.js +0 -46
  109. package/lib/esm/utils/retry.js +0 -16
  110. package/lib/esm/utils/task-id.js +0 -3
  111. package/lib/esm/utils/tool-calls-transform.js +0 -15
  112. package/lib/types/assistant/generate-output.d.ts +0 -29
  113. package/lib/types/assistant/select-agent.d.ts +0 -14
  114. package/lib/types/assistant/type.d.ts +0 -61
  115. package/lib/types/common/aid.d.ts +0 -18
  116. package/lib/types/common/index.d.ts +0 -7
  117. package/lib/types/common/resource-manager.d.ts +0 -88
  118. package/lib/types/constants.d.ts +0 -6
  119. package/lib/types/executor/agent.d.ts +0 -5
  120. package/lib/types/executor/aigc.d.ts +0 -9
  121. package/lib/types/executor/api.d.ts +0 -9
  122. package/lib/types/executor/base.d.ts +0 -209
  123. package/lib/types/executor/blocklet.d.ts +0 -9
  124. package/lib/types/executor/call-agent.d.ts +0 -12
  125. package/lib/types/executor/decision.d.ts +0 -20
  126. package/lib/types/executor/image-blender.d.ts +0 -9
  127. package/lib/types/executor/index.d.ts +0 -8
  128. package/lib/types/executor/llm.d.ts +0 -38
  129. package/lib/types/executor/logic.d.ts +0 -9
  130. package/lib/types/libs/blocklet/vc.d.ts +0 -17
  131. package/lib/types/libs/openapi/request/index.d.ts +0 -17
  132. package/lib/types/libs/openapi/request/util.d.ts +0 -40
  133. package/lib/types/libs/openapi/types/index.d.ts +0 -20
  134. package/lib/types/libs/openapi/util/call.d.ts +0 -2
  135. package/lib/types/libs/openapi/util/check-schema.d.ts +0 -3
  136. package/lib/types/libs/openapi/util/convert-schema.d.ts +0 -8
  137. package/lib/types/libs/openapi/util/flatten-open-api.d.ts +0 -3
  138. package/lib/types/libs/openapi/util/get-open-api-i18n-text.d.ts +0 -2
  139. package/lib/types/runtime/resource-blocklet.d.ts +0 -2
  140. package/lib/types/runtime/runtime.d.ts +0 -20
  141. package/lib/types/types/assistant/index.d.ts +0 -405
  142. package/lib/types/types/assistant/mustache/ReadableMustache.d.ts +0 -2
  143. package/lib/types/types/assistant/mustache/directive.d.ts +0 -6
  144. package/lib/types/types/assistant/mustache/mustache.d.ts +0 -2
  145. package/lib/types/types/common/index.d.ts +0 -45
  146. package/lib/types/types/resource/index.d.ts +0 -17
  147. package/lib/types/types/resource/project.d.ts +0 -41
  148. package/lib/types/types/runtime/agent.d.ts +0 -33
  149. package/lib/types/types/runtime/error.d.ts +0 -10
  150. package/lib/types/types/runtime/index.d.ts +0 -116
  151. package/lib/types/types/runtime/runtime-resource-blocklet-state.d.ts +0 -5
  152. package/lib/types/types/runtime/schema.d.ts +0 -110
  153. package/lib/types/utils/cron-job.d.ts +0 -22
  154. package/lib/types/utils/extract-metadata-transform.d.ts +0 -16
  155. package/lib/types/utils/extract-metadata-transform.test.d.ts +0 -1
  156. package/lib/types/utils/fs.d.ts +0 -9
  157. package/lib/types/utils/get-blocklet-agent.d.ts +0 -219
  158. package/lib/types/utils/geti.d.ts +0 -1
  159. package/lib/types/utils/is-non-nullable.d.ts +0 -2
  160. package/lib/types/utils/render-message.d.ts +0 -6
  161. package/lib/types/utils/resolve-secret-inputs.d.ts +0 -11
  162. package/lib/types/utils/retry.d.ts +0 -1
  163. package/lib/types/utils/task-id.d.ts +0 -1
  164. package/lib/types/utils/tool-calls-transform.d.ts +0 -2
  165. /package/lib/cjs/types/{runtime/agent.js → agent.js} +0 -0
  166. /package/lib/cjs/types/{common/index.js → runnable.js} +0 -0
  167. /package/lib/esm/types/{runtime/agent.js → agent.js} +0 -0
  168. /package/lib/esm/types/{common/index.js → runnable.js} +0 -0
@@ -1,351 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getBlockletAgent = exports.buildInOpenAPI = exports.MEMORY_API_ID = exports.KNOWLEDGE_API_ID = exports.HISTORY_API_ID = void 0;
7
- const config_1 = __importDefault(require("@blocklet/sdk/lib/config"));
8
- const axios_1 = __importDefault(require("axios"));
9
- const js_base64_1 = require("js-base64");
10
- const ufo_1 = require("ufo");
11
- const constants_1 = require("../constants");
12
- const util_1 = require("../libs/openapi/request/util");
13
- const flatten_open_api_1 = __importDefault(require("../libs/openapi/util/flatten-open-api"));
14
- const task_id_1 = require("./task-id");
15
- exports.HISTORY_API_ID = js_base64_1.Base64.encodeURI(['/api/messages', 'get'].join('/'));
16
- exports.KNOWLEDGE_API_ID = js_base64_1.Base64.encodeURI(['/api/datasets/{datasetId}/search', 'get'].join('/'));
17
- exports.MEMORY_API_ID = js_base64_1.Base64.encodeURI(['/api/memories/variable-by-query', 'get'].join('/'));
18
- // 内置的 OpenAPI 接口
19
- exports.buildInOpenAPI = {
20
- '/api/messages': {
21
- get: {
22
- summary: 'Get history messages',
23
- 'x-summary-zh': '获取历史信息',
24
- description: 'Retrieve messages based on sessionId, last N messages, or keyword',
25
- 'x-description-zh': '根据 sessionId、最后N条消息或关键字检索历史消息',
26
- tags: ['AIGNE Studio'],
27
- parameters: [
28
- {
29
- in: 'query',
30
- name: 'sessionId',
31
- schema: {
32
- type: 'string',
33
- },
34
- description: 'Session Id',
35
- },
36
- {
37
- in: 'query',
38
- name: 'limit',
39
- schema: {
40
- type: 'number',
41
- },
42
- description: 'Number of last messages to retrieve',
43
- 'x-description-zh': '检索的消息的数目',
44
- },
45
- {
46
- in: 'query',
47
- name: 'keyword',
48
- schema: {
49
- type: 'string',
50
- },
51
- description: 'Keyword to search in messages',
52
- 'x-description-zh': '在消息中搜索的关键字',
53
- },
54
- ],
55
- responses: {
56
- '200': {
57
- description: 'A list of history messages',
58
- 'x-description-zh': '检索历史消息列表',
59
- content: {
60
- 'application/json': {
61
- schema: {
62
- type: 'object',
63
- properties: {
64
- messages: {
65
- type: 'array',
66
- items: {
67
- type: 'object',
68
- properties: {
69
- role: {
70
- type: 'string',
71
- },
72
- content: {
73
- type: 'string',
74
- },
75
- agentId: {
76
- type: 'string',
77
- },
78
- },
79
- },
80
- },
81
- },
82
- },
83
- },
84
- },
85
- },
86
- },
87
- 'x-id': exports.HISTORY_API_ID,
88
- 'x-did': constants_1.AIGNE_RUNTIME_COMPONENT_DID,
89
- 'x-path': '/api/messages',
90
- 'x-method': 'get',
91
- },
92
- },
93
- '/api/datasets/{datasetId}/search': {
94
- get: {
95
- summary: 'Search the knowledge',
96
- 'x-summary-zh': '搜索知识库信息',
97
- parameters: [
98
- {
99
- name: 'datasetId',
100
- in: 'path',
101
- required: true,
102
- schema: {
103
- type: 'string',
104
- },
105
- description: 'The ID of the dataset to search',
106
- },
107
- {
108
- name: 'blockletDid',
109
- in: 'query',
110
- required: false,
111
- schema: {
112
- type: 'string',
113
- },
114
- description: 'The Blocklet DID to search for',
115
- },
116
- {
117
- name: 'searchAll',
118
- in: 'query',
119
- required: false,
120
- schema: {
121
- type: 'boolean',
122
- },
123
- description: 'Whether to search all contents',
124
- },
125
- {
126
- name: 'message',
127
- in: 'query',
128
- required: false,
129
- schema: {
130
- type: 'string',
131
- },
132
- description: 'The search message',
133
- },
134
- {
135
- name: 'n',
136
- in: 'query',
137
- required: false,
138
- schema: {
139
- type: 'number',
140
- default: 10,
141
- },
142
- description: 'The number of results to return',
143
- },
144
- ],
145
- responses: {
146
- '200': {
147
- description: 'A list of search results',
148
- content: {
149
- 'application/json': {
150
- schema: {
151
- type: 'object',
152
- properties: {
153
- docs: {
154
- type: 'array',
155
- items: {
156
- type: 'object',
157
- properties: {
158
- title: {
159
- type: 'string',
160
- description: 'The title of the document',
161
- },
162
- content: {
163
- type: 'string',
164
- description: 'The content of the document',
165
- },
166
- },
167
- },
168
- },
169
- },
170
- },
171
- },
172
- },
173
- },
174
- },
175
- 'x-id': exports.KNOWLEDGE_API_ID,
176
- 'x-did': constants_1.AIGNE_RUNTIME_COMPONENT_DID,
177
- 'x-path': '/api/datasets/{datasetId}/search',
178
- 'x-method': 'get',
179
- },
180
- },
181
- '/api/memories/variable-by-query': {
182
- get: {
183
- summary: 'Get memory variables by query',
184
- 'x-summary-zh': '知识库信息',
185
- parameters: [
186
- {
187
- name: 'key',
188
- in: 'query',
189
- required: false,
190
- schema: {
191
- type: 'string',
192
- },
193
- description: 'The key of the variable',
194
- },
195
- {
196
- name: 'projectId',
197
- in: 'query',
198
- required: false,
199
- schema: {
200
- type: 'string',
201
- },
202
- description: 'The ID of the project',
203
- },
204
- {
205
- name: 'scope',
206
- in: 'query',
207
- required: false,
208
- schema: {
209
- type: 'string',
210
- enum: ['session', 'user', 'global'],
211
- },
212
- description: 'The scope of the variable',
213
- },
214
- {
215
- name: 'sessionId',
216
- in: 'query',
217
- required: false,
218
- schema: {
219
- type: 'string',
220
- },
221
- description: 'The ID of the session',
222
- },
223
- ],
224
- responses: {
225
- '200': {
226
- description: 'A list of memory variables',
227
- content: {
228
- 'application/json': {
229
- schema: {
230
- type: 'object',
231
- properties: {
232
- datastores: {
233
- type: 'array',
234
- items: {
235
- type: 'object',
236
- properties: {
237
- id: {
238
- type: 'string',
239
- description: 'The ID of the memory entry',
240
- },
241
- key: {
242
- type: 'string',
243
- description: 'The key of the memory entry',
244
- },
245
- data: {
246
- type: 'object',
247
- description: 'The value of the memory entry',
248
- },
249
- userId: {
250
- type: 'string',
251
- description: 'The ID of the user',
252
- },
253
- projectId: {
254
- type: 'string',
255
- description: 'The ID of the project',
256
- },
257
- sessionId: {
258
- type: 'string',
259
- description: 'The ID of the session',
260
- },
261
- scope: {
262
- type: 'string',
263
- description: 'The scope of the memory entry',
264
- enum: ['session', 'user', 'global'],
265
- },
266
- },
267
- },
268
- },
269
- },
270
- },
271
- },
272
- },
273
- },
274
- },
275
- 'x-id': exports.MEMORY_API_ID,
276
- 'x-did': constants_1.AIGNE_RUNTIME_COMPONENT_DID,
277
- 'x-path': '/api/memories/variable-by-query',
278
- 'x-method': 'get',
279
- },
280
- },
281
- };
282
- function convertSchemaToVariableType(schema) {
283
- switch (schema.type) {
284
- case 'string':
285
- return { type: 'string', defaultValue: '' };
286
- case 'integer':
287
- case 'number':
288
- return { type: 'number', defaultValue: undefined };
289
- case 'boolean':
290
- return { type: 'boolean', defaultValue: undefined };
291
- case 'object':
292
- return {
293
- type: 'object',
294
- properties: schema.properties
295
- ? Object.entries(schema.properties).map(([key, value]) => ({
296
- id: key,
297
- name: key,
298
- ...convertSchemaToVariableType(value),
299
- }))
300
- : [],
301
- };
302
- case 'array':
303
- return {
304
- type: 'array',
305
- element: schema.items ? convertSchemaToVariableType(schema.items) : undefined,
306
- };
307
- default:
308
- throw new Error(`Unsupported schema type: ${schema.type}`);
309
- }
310
- }
311
- const getBlockletAgent = async () => {
312
- let list = {};
313
- try {
314
- const result = await axios_1.default.get((0, ufo_1.joinURL)(config_1.default.env.appUrl, '/.well-known/service/openapi.json'));
315
- list = result.data;
316
- }
317
- catch (error) {
318
- list = {};
319
- }
320
- const openApis = [...(0, flatten_open_api_1.default)(list), ...(0, flatten_open_api_1.default)({ paths: exports.buildInOpenAPI })];
321
- const agents = openApis.map((i) => {
322
- const properties = i?.responses?.['200']?.content?.['application/json']?.schema?.properties || {};
323
- return {
324
- type: 'blocklet',
325
- id: i.id,
326
- name: i?.summary,
327
- description: i?.description,
328
- parameters: (0, util_1.getAllParameters)(i)
329
- .map((i) => {
330
- return {
331
- id: (0, task_id_1.nextId)(),
332
- type: 'string',
333
- key: i.name,
334
- };
335
- })
336
- .filter((i) => i.key),
337
- outputVariables: Object.entries(properties).map(([key, value]) => ({
338
- id: key,
339
- name: key,
340
- ...convertSchemaToVariableType(value),
341
- })),
342
- openApi: i,
343
- };
344
- });
345
- const agentsMap = agents.reduce((acc, cur) => {
346
- acc[cur.id] = cur;
347
- return acc;
348
- }, {});
349
- return { agents, agentsMap, openApis };
350
- };
351
- exports.getBlockletAgent = getBlockletAgent;
@@ -1,37 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.geti = void 0;
7
- // @ts-ignore
8
- const _castPath_1 = __importDefault(require("lodash/_castPath"));
9
- // @ts-ignore
10
- const _toKey_1 = __importDefault(require("lodash/_toKey"));
11
- const toLower_1 = __importDefault(require("lodash/toLower"));
12
- const geti = (object, path, defaultValue) => {
13
- if (!object)
14
- return defaultValue === undefined ? null : defaultValue;
15
- const paths = (0, _castPath_1.default)(path, object);
16
- const { length } = paths;
17
- let index = 0;
18
- let iterator = object;
19
- while (iterator !== undefined && index < length) {
20
- const key = (0, _toKey_1.default)(paths[index]).toLowerCase();
21
- iterator = findLowercaseKey(iterator, key);
22
- index += 1;
23
- }
24
- return index && index === length && iterator !== undefined ? iterator : defaultValue;
25
- };
26
- exports.geti = geti;
27
- const findLowercaseKey = (value, key) => {
28
- return Object.keys(value).reduce((a, k) => {
29
- if (a !== undefined) {
30
- return a;
31
- }
32
- if ((0, toLower_1.default)(k) === key) {
33
- return value[k];
34
- }
35
- return undefined;
36
- }, undefined);
37
- };
@@ -1,20 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.isNonNullable = isNonNullable;
7
- exports.isPropsNonNullable = isPropsNonNullable;
8
- const isNil_1 = __importDefault(require("lodash/isNil"));
9
- function isNonNullable(value) {
10
- return !(0, isNil_1.default)(value);
11
- }
12
- function isPropsNonNullable(...props) {
13
- return (value) => {
14
- for (const prop of props.flat()) {
15
- if ((0, isNil_1.default)(value?.[prop]))
16
- return false;
17
- }
18
- return true;
19
- };
20
- }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.renderMessage = renderMessage;
4
- const lodash_1 = require("lodash");
5
- const assistant_1 = require("../types/assistant");
6
- async function renderMessage(message, parameters, { stringify = true, escapeJsonSymbols } = {}) {
7
- const spans = assistant_1.Mustache.parse(message.trim());
8
- if (!stringify && spans.length === 1) {
9
- const span = spans[0];
10
- if (span[0] === 'name') {
11
- return (0, lodash_1.get)(parameters, span[1]);
12
- }
13
- }
14
- return assistant_1.Mustache.render(message, parameters, undefined, {
15
- escape: (v) => {
16
- const r = typeof v === 'object' ? JSON.stringify(v) : v;
17
- if (typeof r === 'string' && escapeJsonSymbols) {
18
- return JSON.stringify(r).slice(1, -1);
19
- }
20
- return r;
21
- },
22
- });
23
- }
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveSecretInputs = resolveSecretInputs;
4
- const lodash_1 = require("lodash");
5
- const aid_1 = require("../common/aid");
6
- const types_1 = require("../types");
7
- const is_non_nullable_1 = require("./is-non-nullable");
8
- async function resolveSecretInputs(agent, { getAgent }) {
9
- if (!agent.project)
10
- return [];
11
- const secretInputs = (agent.parameters ?? [])
12
- .filter((i) => !!i.key && i.type === 'source' && i.source?.variableFrom === 'secret' && !i.hidden)
13
- .map((input) => ({ agent, input }));
14
- const outputVariables = (agent.outputVariables ?? []).filter((i) => !i.hidden);
15
- const children = outputVariables.find((i) => i.name === types_1.RuntimeOutputVariable.children)
16
- ?.initialValue;
17
- const referencedAgents = [
18
- ...(children?.agents ?? []).map((i) => ({ id: i.id, projectId: agent.project.id, blockletDid: undefined })),
19
- ...(agent.parameters ?? []).map((i) => {
20
- if (i.hidden)
21
- return null;
22
- if (i.type === 'source' && i.source?.variableFrom === 'tool' && i.source.agent?.id) {
23
- return {
24
- id: i.source.agent.id,
25
- blockletDid: i.source.agent.blockletDid,
26
- projectId: i.source.agent.projectId,
27
- };
28
- }
29
- return null;
30
- }),
31
- ...(agent.executor?.agent?.id ? [agent.executor.agent] : []),
32
- ].filter(is_non_nullable_1.isNonNullable);
33
- const identity = (0, aid_1.parseIdentity)(agent.identity.aid, { rejectWhenError: true });
34
- const nestedSecretInputs = (await Promise.all(referencedAgents.map(async (i) => {
35
- const res = await getAgent({
36
- aid: (0, aid_1.stringifyIdentity)({
37
- blockletDid: i.blockletDid || identity.blockletDid,
38
- projectId: i.projectId || identity.projectId,
39
- projectRef: identity.projectRef || identity.projectRef,
40
- agentId: i.id,
41
- }),
42
- working: agent.identity.working,
43
- });
44
- return res && resolveSecretInputs(res, { getAgent });
45
- })))
46
- .flat()
47
- .filter(is_non_nullable_1.isNonNullable);
48
- return (0, lodash_1.uniqBy)([...secretInputs, ...nestedSecretInputs], (i) => `${i.input.id}-${i.agent.id}-${i.agent.project?.id}`);
49
- }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = retry;
4
- async function retry(fn, retries) {
5
- let i = 0;
6
- for (;;) {
7
- try {
8
- return await fn();
9
- }
10
- catch (error) {
11
- if (i < retries) {
12
- i++;
13
- }
14
- else {
15
- throw error;
16
- }
17
- }
18
- }
19
- }
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nextId = void 0;
4
- const snowflake_uuid_1 = require("snowflake-uuid");
5
- const idGenerator = new snowflake_uuid_1.Worker();
6
- const nextId = () => idGenerator.nextId().toString();
7
- exports.nextId = nextId;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toolCallsTransform = toolCallsTransform;
4
- function toolCallsTransform(calls, chunk) {
5
- const { toolCalls } = chunk.delta;
6
- toolCalls?.forEach((item) => {
7
- const targetCall = item?.id ? calls.find((call) => call.id === item?.id) : calls.at(-1);
8
- // 如果 item 有 id, 且 calls 中没有这个 id, 则直接 push
9
- // chatgpt adapter 和 claude adapter 经过改造一定有 id
10
- if (item?.id && !targetCall) {
11
- calls.push(item);
12
- }
13
- else if (targetCall?.function) {
14
- targetCall.function.name += item.function?.name || '';
15
- targetCall.function.arguments = (targetCall.function.arguments || '') + (item.function?.arguments || '');
16
- }
17
- });
18
- }
@@ -1,91 +0,0 @@
1
- import { TransformStream } from 'stream/web';
2
- import { isChatCompletionChunk } from '@blocklet/ai-kit/api/types/chat';
3
- import { outputVariablesToJoiSchema, outputVariablesToJsonSchema } from '../types/runtime/schema';
4
- import { ExtractMetadataTransform } from '../utils/extract-metadata-transform';
5
- import retry from '../utils/retry';
6
- export const metadataPrefix = '```metadata';
7
- export const metadataSuffix = '```';
8
- export const metadataOutputFormatPrompt = (schema) => `\
9
- ## Metadata Schema
10
- Here is the metadata json schema, inside the <metadata-schema></metadata-schema> XML tags:
11
- <metadata-schema>
12
- ${schema}
13
- </metadata-schema>
14
-
15
- ## Output Rules
16
- - You have to generate a json metadata base on the metadata schema above, inside the <metadata-schema></metadata-schema> XML tags
17
- - Don't make any explanation
18
-
19
- ## Output Format
20
- [Generate a json data based on the above metadata schema, put the json string into the following code block]
21
- ${metadataPrefix}
22
- ${metadataSuffix}
23
-
24
- ## Output Example
25
- ${metadataPrefix}
26
- {"key": "value"}
27
- ${metadataSuffix}
28
- `;
29
- export const metadataStreamOutputFormatPrompt = (schema) => `\
30
- ## Metadata Schema
31
- Here is the metadata json schema, inside the <metadata-schema></metadata-schema> XML tags:
32
- <metadata-schema>
33
- ${schema}
34
- </metadata-schema>
35
-
36
- ## Output Rules
37
- - You have to generate a text content and a json metadata base on the metadata schema above, inside the <metadata-schema></metadata-schema> XML tags
38
- - Don't make any explanation
39
-
40
- ## Output Format
41
- [Your text content here]
42
- [Generate a json data based on the above metadata schema, put the json string into the following code block]
43
- ${metadataPrefix}
44
- ${metadataSuffix}
45
-
46
- ## Output Example
47
- [Here is your answer to the the user's question]
48
- ${metadataPrefix}
49
- {"key": "value"}
50
- ${metadataSuffix}
51
- `;
52
- export async function generateOutput({ assistant, messages, callAI, maxRetries = 0, datastoreVariables, }) {
53
- const jsonSchema = outputVariablesToJsonSchema(assistant, { variables: datastoreVariables });
54
- const joiSchema = outputVariablesToJoiSchema(assistant, { variables: datastoreVariables });
55
- const outputSchema = JSON.stringify(jsonSchema, null, 2);
56
- messages.push({
57
- role: 'system',
58
- content: metadataOutputFormatPrompt(outputSchema),
59
- });
60
- return retry(async () => {
61
- const result = await callAI({
62
- assistant,
63
- input: {
64
- stream: true,
65
- messages,
66
- },
67
- });
68
- let text = '';
69
- for await (const chunk of extractMetadataFromStream(result, true)) {
70
- if (chunk.type === 'match')
71
- text += chunk.text;
72
- }
73
- const json = JSON.parse(text);
74
- return await joiSchema.validateAsync(json);
75
- }, maxRetries);
76
- }
77
- export function extractMetadataFromStream(input, extract = true) {
78
- return input
79
- .pipeThrough(new TransformStream({
80
- transform: (chunk, controller) => {
81
- if (isChatCompletionChunk(chunk))
82
- if (chunk.delta.content)
83
- controller.enqueue(chunk.delta.content);
84
- },
85
- }))
86
- .pipeThrough(new ExtractMetadataTransform({
87
- // Pass empty string to disable extractor
88
- start: extract ? metadataPrefix : '',
89
- end: extract ? metadataSuffix : '',
90
- }));
91
- }