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