@aigne/core 0.0.3 → 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 +3 -4
  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,668 +0,0 @@
1
- import { hash } from 'crypto';
2
- import { memoize } from '@blocklet/quickjs';
3
- import { call } from '@blocklet/sdk/lib/component';
4
- import config from '@blocklet/sdk/lib/config';
5
- import Joi from 'joi';
6
- import jsonStableStringify from 'json-stable-stringify';
7
- import isEmpty from 'lodash/isEmpty';
8
- import isNil from 'lodash/isNil';
9
- import pick from 'lodash/pick';
10
- import toLower from 'lodash/toLower';
11
- import { parseIdentity, stringifyIdentity } from '../common/aid';
12
- import { AIGNE_RUNTIME_COMPONENT_DID } from '../constants';
13
- import { issueVC } from '../libs/blocklet/vc';
14
- import logger from '../logger';
15
- import { AssistantResponseType, ExecutionPhase, RuntimeOutputVariable, isUserInputParameter, outputVariablesToJoiSchema, } from '../types';
16
- import { HISTORY_API_ID, KNOWLEDGE_API_ID, MEMORY_API_ID, getBlockletAgent } from '../utils/get-blocklet-agent';
17
- import { isNonNullable } from '../utils/is-non-nullable';
18
- import { renderMessage } from '../utils/render-message';
19
- import { nextId } from '../utils/task-id';
20
- function isPlainObject(value) {
21
- return typeof value === 'object' && value !== null && !Array.isArray(value);
22
- }
23
- export class ExecutorContext {
24
- constructor(options) {
25
- this.entry = options.entry;
26
- this.getAgent = memoize(options.getAgent, {
27
- keyGenerator: (o) => [o.aid, o.working].filter(isNonNullable).join('/'),
28
- });
29
- this.callAI = options.callAI;
30
- this.callAIImage = options.callAIImage;
31
- this.callback = options.callback;
32
- this.getMemoryVariables = memoize(options.getMemoryVariables, {
33
- keyGenerator: (o) => [o.blockletDid, o.projectId, o.projectRef, o.working].filter(isNonNullable).join('/'),
34
- });
35
- this.user = options.user;
36
- this.sessionId = options.sessionId;
37
- this.messageId = options.messageId;
38
- this.clientTime = options.clientTime;
39
- this.executor = options.executor;
40
- this.entryProjectId = options.entryProjectId;
41
- this.queryCache = options.queryCache;
42
- this.setCache = options.setCache;
43
- }
44
- entry;
45
- getAgent;
46
- callAI;
47
- callAIImage;
48
- queryCache;
49
- setCache;
50
- callback;
51
- sessionId;
52
- messageId;
53
- clientTime;
54
- promise;
55
- entryProjectId;
56
- user;
57
- getMemoryVariables;
58
- maxRetries = 5;
59
- executor;
60
- async execute(agent, options) {
61
- return this.executor(agent, options).execute();
62
- }
63
- copy(options) {
64
- return new ExecutorContext({ ...this, ...options });
65
- }
66
- async getBlockletAgent(agentId) {
67
- this.promise ??= getBlockletAgent();
68
- const { openApis, agentsMap } = await this.promise;
69
- return { agent: agentsMap[agentId], openApis };
70
- }
71
- }
72
- export class AgentExecutorBase {
73
- context;
74
- agent;
75
- options;
76
- constructor(context, agent, options) {
77
- this.context = context;
78
- this.agent = agent;
79
- this.options = options;
80
- }
81
- async execute() {
82
- const { agent, options } = this;
83
- this.context.callback?.({
84
- type: AssistantResponseType.INPUT,
85
- assistantId: agent.id,
86
- taskId: options.taskId,
87
- parentTaskId: options.parentTaskId,
88
- assistantName: agent.name,
89
- inputParameters: this.hideSecretInputs(options.inputs || {}, agent),
90
- });
91
- this.finalInputs = await this.prepareInputs();
92
- const partial = await this.validateOutputs({ inputs: this.finalInputs, partial: true });
93
- if (!isEmpty(partial)) {
94
- this.context.callback?.({
95
- type: AssistantResponseType.CHUNK,
96
- taskId: options.taskId,
97
- assistantId: agent.id,
98
- delta: { object: this.hideSecretInputs(partial, agent) },
99
- });
100
- }
101
- this.context.callback?.({
102
- type: AssistantResponseType.EXECUTE,
103
- taskId: options.taskId,
104
- parentTaskId: options.parentTaskId,
105
- assistantId: agent.id,
106
- assistantName: agent.name,
107
- execution: { currentPhase: ExecutionPhase.EXECUTE_ASSISTANT_RUNNING },
108
- });
109
- this.context.callback?.({
110
- type: AssistantResponseType.INPUT,
111
- assistantId: agent.id,
112
- taskId: options.taskId,
113
- parentTaskId: options.parentTaskId,
114
- assistantName: agent.name,
115
- inputParameters: this.hideSecretInputs(this.finalInputs, agent),
116
- });
117
- let result;
118
- // query cache if the agent has cache enabled
119
- const cacheKey = this.globalContext.$cache.key;
120
- if (agent.cache?.enable && agent.identity) {
121
- try {
122
- const cache = await this.context.queryCache({ aid: agent.identity.aid, cacheKey });
123
- if (cache) {
124
- result = await this.validateOutputs({ inputs: this.finalInputs, outputs: cache.outputs });
125
- if (isEmpty(result))
126
- result = undefined;
127
- }
128
- }
129
- catch (error) {
130
- logger.error('query and validate cache error', { error });
131
- }
132
- if (typeof result?.$text === 'string') {
133
- this.context.callback?.({
134
- type: AssistantResponseType.CHUNK,
135
- taskId: options.taskId,
136
- assistantId: agent.id,
137
- delta: { content: result.$text },
138
- });
139
- }
140
- }
141
- if (result === undefined) {
142
- const outputs = await this.process({ inputs: this.finalInputs });
143
- result = await this.validateOutputs({ inputs: this.finalInputs, outputs, processCallAgentOutputs: true });
144
- // set cache if needed
145
- if (!isEmpty(result) && agent.cache?.enable && agent.identity) {
146
- await this.context.setCache({ aid: agent.identity.aid, cacheKey, inputs: this.finalInputs, outputs: result });
147
- }
148
- }
149
- await this.postProcessOutputs(agent, { outputs: result });
150
- this.context.callback?.({
151
- type: AssistantResponseType.CHUNK,
152
- taskId: options.taskId,
153
- assistantId: agent.id,
154
- delta: { object: result },
155
- });
156
- this.context.callback?.({
157
- type: AssistantResponseType.EXECUTE,
158
- taskId: options.taskId,
159
- parentTaskId: options.parentTaskId,
160
- assistantId: agent.id,
161
- assistantName: agent.name,
162
- execution: { currentPhase: ExecutionPhase.EXECUTE_ASSISTANT_END },
163
- });
164
- return result;
165
- }
166
- _finalInputs;
167
- get finalInputs() {
168
- if (!this._finalInputs)
169
- throw new Error('Final inputs is not ready');
170
- return this._finalInputs;
171
- }
172
- set finalInputs(value) {
173
- this._finalInputs = value;
174
- }
175
- cacheKey({ userRelated, onlyUserInputs } = {}) {
176
- // TODO: support custom cache key by specifying inputs of agent
177
- const i = Object.fromEntries((this.agent.parameters ?? [])
178
- .filter((i) => !!i.key && !i.hidden && (onlyUserInputs ? isUserInputParameter(i) : true))
179
- .map((i) => [i.key, this.finalInputs[i.key]]));
180
- return hash('md5', jsonStableStringify({ ...i, '$sys.user.did': userRelated ? this.globalContext.$sys.user?.did : undefined }), 'hex');
181
- }
182
- get globalContext() {
183
- const executor = this;
184
- const { agent } = this;
185
- return {
186
- $sys: {
187
- sessionId: this.context.sessionId,
188
- messageId: this.context.messageId,
189
- clientTime: this.context.clientTime,
190
- user: this.context.user,
191
- env: pick(config.env, 'appId', 'appName', 'appDescription', 'appUrl'),
192
- },
193
- $storage: {
194
- async getItem(key, { scope = 'global', onlyOne } = {}) {
195
- return executor.getMemory({ key, agentId: agent.id, scope, onlyOne });
196
- },
197
- async setItem(key, value, { scope = 'global', onlyOne } = {}) {
198
- return executor.setMemory({ key, data: value, agentId: agent.id, scope, reset: onlyOne });
199
- },
200
- },
201
- $cache: {
202
- get key() {
203
- return executor.cacheKey({ onlyUserInputs: true });
204
- },
205
- async getItem(key, { agentId } = {}) {
206
- if (agent.type === 'blocklet')
207
- throw new Error('Unsupported calling query cache in blocklet agent');
208
- const identity = parseIdentity(agent.identity.aid, { rejectWhenError: true });
209
- return executor.context.queryCache({
210
- aid: stringifyIdentity({ ...identity, agentId: agentId || identity.agentId }),
211
- cacheKey: key,
212
- });
213
- },
214
- },
215
- $blocklet: {
216
- issueVC: (args) => {
217
- const userDid = executor.context.user?.did;
218
- if (!userDid)
219
- throw new Error('Issue VC requires user did');
220
- return issueVC({
221
- ...args,
222
- userDid,
223
- context: this.context,
224
- });
225
- },
226
- },
227
- };
228
- }
229
- async prepareInputs() {
230
- const { agent, options: { inputs, taskId, variables }, } = this;
231
- const inputParameters = Object.fromEntries(await Promise.all((agent.parameters || [])
232
- .filter((i) => !!i.key && i.type !== 'source' && !i.hidden)
233
- .map(async (i) => {
234
- const inputValue = inputs?.[i.key];
235
- if (typeof inputValue === 'string') {
236
- const template = String(inputValue || '').trim();
237
- return [
238
- i.key,
239
- template ? await this.renderMessage(template, variables, { stringify: false }) : inputValue,
240
- ];
241
- }
242
- if (isPlainObject(inputValue)) {
243
- const resolvedEntries = await Promise.all(Object.entries(inputValue).map(async ([key, value]) => {
244
- return [
245
- key,
246
- typeof value === 'string'
247
- ? await this.renderMessage(value, variables, { stringify: false })
248
- : value,
249
- ];
250
- }));
251
- return [i.key, Object.fromEntries(resolvedEntries)];
252
- }
253
- if (Array.isArray(inputValue) && inputValue.length) {
254
- const resolvedArray = await Promise.all(inputValue.map(async (item) => {
255
- if (isPlainObject(item)) {
256
- return Object.fromEntries(await Promise.all(Object.entries(item).map(async ([key, value]) => [
257
- key,
258
- typeof value === 'string'
259
- ? await this.renderMessage(value, variables, { stringify: false })
260
- : value,
261
- ])));
262
- }
263
- return await this.renderMessage(item, variables, { stringify: false });
264
- }));
265
- return [i.key, resolvedArray];
266
- }
267
- return [i.key, variables?.[i.key] || inputs?.[i.key]];
268
- }) ?? []));
269
- const inputVariables = { ...(inputs || {}), ...inputParameters };
270
- const partial = await this.validateOutputs({ inputs: inputVariables, partial: true });
271
- if (!isEmpty(partial)) {
272
- this.context.callback?.({
273
- type: AssistantResponseType.CHUNK,
274
- taskId,
275
- assistantId: agent.id,
276
- delta: { object: partial },
277
- });
278
- }
279
- const { callback } = this.context;
280
- const cb = (taskId) => (args) => {
281
- if (args.type === AssistantResponseType.CHUNK && args.taskId === taskId) {
282
- callback({ ...args });
283
- return;
284
- }
285
- callback(args);
286
- };
287
- const parameters = (agent.parameters || []).filter((i) => !i.hidden);
288
- for (const parameter of parameters) {
289
- if (!parameter.key)
290
- continue;
291
- if (parameter.type === 'source') {
292
- if (!agent.project) {
293
- throw new Error('Agent project not found.');
294
- }
295
- if (parameter.source?.variableFrom === 'secret') {
296
- const secret = inputs?.[parameter.key] || config.env[(parameter.key || '').toLocaleUpperCase()];
297
- if (!secret)
298
- throw new Error(`Missing required agent secret ${parameter.key}`);
299
- inputVariables[parameter.key] = secret;
300
- }
301
- else if (parameter.source?.variableFrom === 'tool' && parameter.source.agent) {
302
- const currentTaskId = nextId();
303
- const { agent: tool } = parameter.source;
304
- const identity = parseIdentity(agent.identity.aid, { rejectWhenError: true });
305
- const toolAgent = await this.context.getAgent({
306
- aid: stringifyIdentity({
307
- blockletDid: tool.blockletDid || identity.blockletDid,
308
- projectId: tool.projectId || identity.projectId,
309
- projectRef: identity.projectRef,
310
- agentId: tool.id,
311
- }),
312
- working: agent.identity.working,
313
- });
314
- if (!toolAgent)
315
- continue;
316
- const result = await this.context
317
- .executor(toolAgent, {
318
- inputs: tool.parameters,
319
- variables: { ...inputParameters, ...inputVariables },
320
- taskId: currentTaskId,
321
- parentTaskId: taskId,
322
- })
323
- .execute();
324
- inputVariables[parameter.key] = result ?? parameter.defaultValue;
325
- }
326
- else if (parameter.source?.variableFrom === 'datastore') {
327
- const currentTaskId = nextId();
328
- const key = toLower(parameter.source.variable?.key) || toLower(parameter.key);
329
- const scope = parameter.source.variable?.scope || 'session';
330
- const blocklet = await this.context.getBlockletAgent(MEMORY_API_ID);
331
- if (!blocklet.agent) {
332
- throw new Error('Blocklet agent api not found.');
333
- }
334
- const data = await this.context
335
- .executor(blocklet.agent, {
336
- inputs: {
337
- projectId: this.context.entryProjectId,
338
- sessionId: this.context.sessionId,
339
- scope,
340
- key,
341
- },
342
- taskId: currentTaskId,
343
- parentTaskId: taskId,
344
- })
345
- .execute();
346
- const m = await this.context.getMemoryVariables({
347
- ...parseIdentity(agent.identity.aid, { rejectWhenError: true }),
348
- working: agent.identity.working,
349
- });
350
- const list = (data.datastores || []).map((x) => x?.data).filter((x) => x);
351
- const storageVariable = m.find((x) => toLower(x.key || '') === toLower(key || '') && x.scope === scope);
352
- let result = list?.length > 0
353
- ? list
354
- : [
355
- storageVariable?.type?.type === 'number' && typeof storageVariable?.defaultValue === 'string'
356
- ? Number(storageVariable.defaultValue)
357
- : storageVariable?.defaultValue,
358
- ];
359
- if (storageVariable?.reset) {
360
- result = (result?.length > 1 ? result : result[0]) ?? '';
361
- }
362
- inputVariables[parameter.key] = result ?? parameter.defaultValue;
363
- }
364
- else if (parameter.source?.variableFrom === 'knowledge' && parameter.source.knowledge) {
365
- const currentTaskId = nextId();
366
- const tool = parameter.source.knowledge;
367
- const blockletDid = parameter.source.knowledge.blockletDid ||
368
- parseIdentity(agent.identity.aid, { rejectWhenError: true }).blockletDid;
369
- const blocklet = await this.context.getBlockletAgent(KNOWLEDGE_API_ID);
370
- if (!blocklet.agent) {
371
- throw new Error('Blocklet agent api not found.');
372
- }
373
- const data = await this.context
374
- .copy({ callback: cb(currentTaskId) })
375
- .executor(blocklet.agent, {
376
- inputs: tool?.parameters,
377
- variables: { ...inputVariables, blockletDid, datasetId: tool.id, knowledgeId: tool.id },
378
- taskId: currentTaskId,
379
- parentTaskId: taskId,
380
- })
381
- .execute();
382
- inputVariables[parameter.key] = JSON.stringify(data?.docs || []) ?? parameter.defaultValue;
383
- }
384
- else if (parameter.source?.variableFrom === 'history' && parameter.source.chatHistory) {
385
- const currentTaskId = nextId();
386
- const chat = parameter.source.chatHistory;
387
- const blocklet = await this.context.getBlockletAgent(HISTORY_API_ID);
388
- if (!blocklet.agent) {
389
- throw new Error('Blocklet agent api not found.');
390
- }
391
- const result = await this.context
392
- .executor(blocklet.agent, {
393
- inputs: {
394
- sessionId: this.context.sessionId,
395
- limit: chat.limit || 50,
396
- keyword: await this.renderMessage(chat.keyword || '', inputVariables, { stringify: false }),
397
- },
398
- taskId: currentTaskId,
399
- parentTaskId: taskId,
400
- })
401
- .execute();
402
- const memories = Array.isArray(result?.messages)
403
- ? result.messages
404
- : [];
405
- const agentIds = new Set(memories.map((i) => i.agentId).filter((i) => !!i));
406
- const identity = parseIdentity(agent.identity.aid, { rejectWhenError: true });
407
- const assistantNameMap = Object.fromEntries((await Promise.all([...agentIds].map((agentId) => this.context
408
- .getAgent({ aid: stringifyIdentity({ ...identity, agentId }), working: agent.identity.working })
409
- .catch((error) => {
410
- logger.error('get assistant in conversation history error', { error });
411
- return null;
412
- }))))
413
- .filter(isNonNullable)
414
- .map((i) => [
415
- i.id,
416
- i.outputVariables?.find((j) => j.name === RuntimeOutputVariable.profile)
417
- ?.initialValue?.name ||
418
- i.name ||
419
- i.id,
420
- ]));
421
- inputVariables[parameter.key] = memories.map((i) => ({
422
- ...i,
423
- name: i.agentId && assistantNameMap[i.agentId],
424
- }));
425
- }
426
- else if (parameter.source?.variableFrom === 'blockletAPI' && parameter.source.api) {
427
- const currentTaskId = nextId();
428
- const blocklet = await this.context.getBlockletAgent(parameter.source.api.id);
429
- if (!blocklet.agent) {
430
- throw new Error('Blocklet agent api not found.');
431
- }
432
- const result = await this.context
433
- .copy({ callback: cb?.(currentTaskId) })
434
- .executor(blocklet.agent, {
435
- inputs: parameter.source.api.parameters,
436
- variables: inputVariables,
437
- taskId: currentTaskId,
438
- parentTaskId: taskId,
439
- })
440
- .execute();
441
- inputVariables[parameter.key] = result;
442
- }
443
- }
444
- else if (['llmInputMessages', 'llmInputTools', 'llmInputToolChoice', 'llmInputResponseFormat'].includes(parameter.type)) {
445
- const v = inputs?.[parameter.key];
446
- const tryParse = (s) => {
447
- try {
448
- return JSON.parse(s);
449
- }
450
- catch {
451
- // ignore
452
- }
453
- return undefined;
454
- };
455
- const schema = {
456
- llmInputMessages: Joi.array().items(Joi.object({
457
- role: Joi.string().valid('system', 'user', 'assistant').empty([null, '']).default('user'),
458
- content: Joi.string().required(),
459
- name: Joi.string().empty([null, '']),
460
- })),
461
- llmInputTools: Joi.array()
462
- .items(Joi.object({
463
- type: Joi.string().valid('function').required(),
464
- function: Joi.object({
465
- name: Joi.string().required(),
466
- description: Joi.string().empty([null, '']),
467
- parameters: Joi.object().pattern(Joi.string(), Joi.any()).required(),
468
- }).required(),
469
- }))
470
- .empty(Joi.array().length(0)),
471
- llmInputToolChoice: Joi.alternatives()
472
- .try(Joi.string().valid('auto', 'none', 'required'), Joi.object({
473
- type: Joi.string().valid('function').required(),
474
- function: Joi.object({
475
- name: Joi.string().required(),
476
- description: Joi.string(),
477
- }).required(),
478
- }))
479
- .empty([null, ''])
480
- .optional(),
481
- llmInputResponseFormat: Joi.object({
482
- type: Joi.string().valid('text', 'json_object', 'json_schema').empty([null, '']),
483
- }).when(Joi.object({ type: Joi.valid('json_schema') }), {
484
- then: Joi.object({
485
- jsonSchema: Joi.object({
486
- name: Joi.string().required(),
487
- description: Joi.string().empty([null, '']),
488
- schema: Joi.object().pattern(Joi.string(), Joi.any()).required(),
489
- strict: Joi.boolean().empty([null, '']),
490
- }),
491
- }),
492
- }),
493
- }[parameter.type];
494
- const val = parameter.type === 'llmInputMessages'
495
- ? await schema.validateAsync((Array.isArray(v) ? v : tryParse(v)) ?? [
496
- { role: 'user', content: typeof v === 'string' ? v : JSON.stringify(v) },
497
- ], { stripUnknown: true })
498
- : parameter.type === 'llmInputTools'
499
- ? await schema.validateAsync(Array.isArray(v) ? v : tryParse(v), { stripUnknown: true })
500
- : await schema.validateAsync(tryParse(v) || v, { stripUnknown: true });
501
- inputVariables[parameter.key] = val;
502
- }
503
- else if (parameter.type === 'boolean') {
504
- const val = inputVariables[parameter.key];
505
- inputVariables[parameter.key] = Boolean(isNil(val) ? parameter.defaultValue : val);
506
- }
507
- else if (parameter.type === 'number') {
508
- const val = inputVariables[parameter.key];
509
- const parsedValue = val && typeof val === 'number' ? val : (Number(val) ?? parameter.defaultValue ?? null);
510
- inputVariables[parameter.key] = Number.isNaN(parsedValue) ? (parameter.defaultValue ?? null) : parsedValue;
511
- }
512
- else {
513
- const val = inputVariables[parameter.key];
514
- if (!isNil(val))
515
- inputVariables[parameter.key] = val || null;
516
- }
517
- }
518
- return inputVariables;
519
- }
520
- renderMessage = async (template, variables, options) => {
521
- return renderMessage(template, { ...this._finalInputs, ...variables, ...this.globalContext }, options);
522
- };
523
- async validateOutputs({ inputs, outputs, partial, processCallAgentOutputs, }) {
524
- const { agent } = this;
525
- const joiSchema = outputVariablesToJoiSchema(agent, {
526
- partial,
527
- variables: agent.identity
528
- ? await this.context.getMemoryVariables({
529
- ...parseIdentity(agent.identity.aid, { rejectWhenError: true }),
530
- working: agent.identity.working,
531
- })
532
- : [],
533
- });
534
- const outputVariables = (agent.outputVariables ?? []).filter((i) => !i.hidden);
535
- const outputInputs = outputVariables.reduce((res, output) => {
536
- let input;
537
- if (output.from?.type === 'input') {
538
- const fromId = output.from.id;
539
- input = agent.parameters?.find((input) => input.id === fromId && !input.hidden);
540
- }
541
- if (input?.key && output.name) {
542
- const val = inputs?.[input.key];
543
- if (!isNil(val))
544
- return { ...res, [output.name]: val };
545
- }
546
- return res;
547
- }, {});
548
- const result = await joiSchema.validateAsync({ ...outputs, ...outputInputs }, { stripUnknown: true });
549
- if (processCallAgentOutputs && this.agent.outputVariables) {
550
- const callAgentOutputs = this.agent.outputVariables.filter((i) => i.from?.type === 'callAgent' && !!i.from.callAgent?.agentId && !!i.name);
551
- const templateOutputs = this.agent.outputVariables.filter((i) => !!i.valueTemplate?.trim() && !!i.name);
552
- const isOutputActive = async (output) => {
553
- if (!output.activeWhen?.trim())
554
- return true;
555
- return Joi.boolean().validate(await this.renderMessage(output.activeWhen, { ...outputs, ...result }, { stringify: false })).value;
556
- };
557
- const v = Object.fromEntries((await Promise.all(callAgentOutputs.map(async (i) => {
558
- if (!(await isOutputActive(i)))
559
- return null;
560
- return [
561
- i.name,
562
- await (async () => {
563
- if (!this.agent.identity)
564
- throw new Error('Agent identity not found');
565
- const identity = parseIdentity(this.agent.identity.aid, { rejectWhenError: true });
566
- const toolAgent = await this.context.getAgent({
567
- aid: stringifyIdentity({
568
- blockletDid: i.from.callAgent.blockletDid || identity.blockletDid,
569
- projectId: i.from.callAgent.projectId || identity.projectId,
570
- projectRef: identity.projectRef,
571
- agentId: i.from.callAgent.agentId,
572
- }),
573
- working: this.agent.identity.working,
574
- });
575
- if (!toolAgent)
576
- throw new Error('Tool agent not found');
577
- const currentTaskId = nextId();
578
- return await this.context
579
- .executor(toolAgent, {
580
- inputs: i.from.callAgent.inputs,
581
- variables: { ...inputs, ...outputs },
582
- taskId: currentTaskId,
583
- parentTaskId: this.options.taskId,
584
- })
585
- .execute();
586
- })(),
587
- ];
588
- }))).filter(isNonNullable));
589
- Object.assign(result, v);
590
- const v1 = Object.fromEntries((await Promise.all(templateOutputs.map(async (i) => {
591
- if (!(await isOutputActive(i)))
592
- return null;
593
- return [i.name, await this.renderMessage(i.valueTemplate, { ...outputs, ...v }, { stringify: false })];
594
- }))).filter(isNonNullable));
595
- Object.assign(result, v1);
596
- }
597
- return result;
598
- }
599
- async postProcessOutputs(agent, { outputs }) {
600
- const memoryVariables = agent.identity
601
- ? await this.context.getMemoryVariables({
602
- ...parseIdentity(agent.identity.aid, { rejectWhenError: true }),
603
- working: agent.identity.working,
604
- })
605
- : [];
606
- const outputVariables = (agent.outputVariables ?? []).filter((i) => !i.hidden);
607
- for (const output of outputVariables) {
608
- if (!output?.variable?.key || !output?.name)
609
- continue;
610
- const value = outputs[output.name];
611
- if (isNil(value))
612
- continue;
613
- const variable = memoryVariables.find((x) => toLower(x.key || '') === toLower(output.variable?.key || '') && x.scope === output.variable?.scope);
614
- await this.setMemory({
615
- key: toLower(output.variable.key),
616
- data: value,
617
- scope: output.variable.scope,
618
- agentId: agent.id,
619
- reset: variable?.reset,
620
- });
621
- }
622
- }
623
- async setMemory({ key, data, scope, agentId, reset, }) {
624
- const res = await call({
625
- name: AIGNE_RUNTIME_COMPONENT_DID,
626
- path: '/api/memories',
627
- method: 'POST',
628
- params: {
629
- userId: this.context.user?.did,
630
- projectId: this.context.entryProjectId,
631
- agentId,
632
- sessionId: this.context.sessionId,
633
- reset,
634
- },
635
- data: { key, data, scope },
636
- });
637
- return res.data;
638
- }
639
- async getMemory({ key, scope, onlyOne }) {
640
- const res = await call({
641
- name: AIGNE_RUNTIME_COMPONENT_DID,
642
- path: '/api/memories/variable-by-query',
643
- method: 'GET',
644
- params: {
645
- userId: this.context.user?.did,
646
- projectId: this.context.entryProjectId,
647
- sessionId: this.context.sessionId,
648
- key,
649
- scope,
650
- },
651
- });
652
- const list = res.data?.datastores;
653
- if (!Array.isArray(list) || !list.length)
654
- return null;
655
- return onlyOne ? list.at(-1).data : list.map((i) => i.data);
656
- }
657
- hideSecretInputs(partial, agent) {
658
- const authInputs = (agent.parameters || []).filter((i) => i.key && i.type === 'source' && i.source?.variableFrom === 'secret' && !i.hidden);
659
- const secretInputs = authInputs.reduce((res, i) => {
660
- res[i.key] = '******';
661
- return res;
662
- }, {});
663
- return {
664
- ...(partial || {}),
665
- ...secretInputs,
666
- };
667
- }
668
- }
@@ -1,21 +0,0 @@
1
- import { callBlockletApi } from '../libs/openapi/request';
2
- import { AgentExecutorBase } from './base';
3
- export class BlockletAgentExecutor extends AgentExecutorBase {
4
- async process({ inputs }) {
5
- const blocklet = await this.context.getBlockletAgent(this.agent.id);
6
- if (!blocklet.agent) {
7
- throw new Error('Blocklet agent api not found.');
8
- }
9
- if (!blocklet.agent.openApi) {
10
- throw new Error('Blocklet agent api not found.');
11
- }
12
- const params = {
13
- userId: this.context.user?.did,
14
- projectId: this.context.entryProjectId,
15
- sessionId: this.context.sessionId,
16
- assistantId: this.agent.id,
17
- };
18
- const response = await callBlockletApi(blocklet.agent.openApi, inputs || {}, { user: this.context.user, params });
19
- return response.data;
20
- }
21
- }