plugin-agent-orchestrator 1.0.38 → 1.1.2
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/dist/client/index.js +1 -1
- package/dist/client-v2/253.9f0537713cee9e6c.js +10 -0
- package/dist/client-v2/418.ce8f5b2b8a229d1d.js +10 -0
- package/dist/client-v2/53.7c29c28043e9112d.js +10 -0
- package/dist/client-v2/794.9bbb8d3a61b2bdf2.js +10 -0
- package/dist/client-v2/993.82778b6fd6db3cb1.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +6 -6
- package/dist/locale/en-US.json +56 -2
- package/dist/locale/vi-VN.json +56 -2
- package/dist/locale/zh-CN.json +56 -2
- package/dist/node_modules/ajv/.tonic_example.js +20 -0
- package/dist/node_modules/ajv/LICENSE +22 -0
- package/dist/node_modules/ajv/dist/ajv.bundle.js +7189 -0
- package/dist/node_modules/ajv/dist/ajv.min.js +3 -0
- package/dist/node_modules/ajv/lib/ajv.d.ts +397 -0
- package/dist/node_modules/ajv/lib/ajv.js +3 -0
- package/dist/node_modules/ajv/lib/cache.js +26 -0
- package/dist/node_modules/ajv/lib/compile/async.js +90 -0
- package/dist/node_modules/ajv/lib/compile/equal.js +5 -0
- package/dist/node_modules/ajv/lib/compile/error_classes.js +34 -0
- package/dist/node_modules/ajv/lib/compile/formats.js +142 -0
- package/dist/node_modules/ajv/lib/compile/index.js +387 -0
- package/dist/node_modules/ajv/lib/compile/resolve.js +270 -0
- package/dist/node_modules/ajv/lib/compile/rules.js +66 -0
- package/dist/node_modules/ajv/lib/compile/schema_obj.js +9 -0
- package/dist/node_modules/ajv/lib/compile/ucs2length.js +20 -0
- package/dist/node_modules/ajv/lib/compile/util.js +239 -0
- package/dist/node_modules/ajv/lib/data.js +49 -0
- package/dist/node_modules/ajv/lib/definition_schema.js +37 -0
- package/dist/node_modules/ajv/lib/dot/_limit.jst +113 -0
- package/dist/node_modules/ajv/lib/dot/_limitItems.jst +12 -0
- package/dist/node_modules/ajv/lib/dot/_limitLength.jst +12 -0
- package/dist/node_modules/ajv/lib/dot/_limitProperties.jst +12 -0
- package/dist/node_modules/ajv/lib/dot/allOf.jst +32 -0
- package/dist/node_modules/ajv/lib/dot/anyOf.jst +46 -0
- package/dist/node_modules/ajv/lib/dot/coerce.def +51 -0
- package/dist/node_modules/ajv/lib/dot/comment.jst +9 -0
- package/dist/node_modules/ajv/lib/dot/const.jst +11 -0
- package/dist/node_modules/ajv/lib/dot/contains.jst +55 -0
- package/dist/node_modules/ajv/lib/dot/custom.jst +191 -0
- package/dist/node_modules/ajv/lib/dot/defaults.def +47 -0
- package/dist/node_modules/ajv/lib/dot/definitions.def +203 -0
- package/dist/node_modules/ajv/lib/dot/dependencies.jst +79 -0
- package/dist/node_modules/ajv/lib/dot/enum.jst +30 -0
- package/dist/node_modules/ajv/lib/dot/errors.def +194 -0
- package/dist/node_modules/ajv/lib/dot/format.jst +106 -0
- package/dist/node_modules/ajv/lib/dot/if.jst +73 -0
- package/dist/node_modules/ajv/lib/dot/items.jst +98 -0
- package/dist/node_modules/ajv/lib/dot/missing.def +39 -0
- package/dist/node_modules/ajv/lib/dot/multipleOf.jst +22 -0
- package/dist/node_modules/ajv/lib/dot/not.jst +43 -0
- package/dist/node_modules/ajv/lib/dot/oneOf.jst +54 -0
- package/dist/node_modules/ajv/lib/dot/pattern.jst +14 -0
- package/dist/node_modules/ajv/lib/dot/properties.jst +245 -0
- package/dist/node_modules/ajv/lib/dot/propertyNames.jst +52 -0
- package/dist/node_modules/ajv/lib/dot/ref.jst +85 -0
- package/dist/node_modules/ajv/lib/dot/required.jst +108 -0
- package/dist/node_modules/ajv/lib/dot/uniqueItems.jst +62 -0
- package/dist/node_modules/ajv/lib/dot/validate.jst +276 -0
- package/dist/node_modules/ajv/lib/dotjs/_limit.js +163 -0
- package/dist/node_modules/ajv/lib/dotjs/_limitItems.js +80 -0
- package/dist/node_modules/ajv/lib/dotjs/_limitLength.js +85 -0
- package/dist/node_modules/ajv/lib/dotjs/_limitProperties.js +80 -0
- package/dist/node_modules/ajv/lib/dotjs/allOf.js +42 -0
- package/dist/node_modules/ajv/lib/dotjs/anyOf.js +73 -0
- package/dist/node_modules/ajv/lib/dotjs/comment.js +14 -0
- package/dist/node_modules/ajv/lib/dotjs/const.js +56 -0
- package/dist/node_modules/ajv/lib/dotjs/contains.js +81 -0
- package/dist/node_modules/ajv/lib/dotjs/custom.js +228 -0
- package/dist/node_modules/ajv/lib/dotjs/dependencies.js +168 -0
- package/dist/node_modules/ajv/lib/dotjs/enum.js +66 -0
- package/dist/node_modules/ajv/lib/dotjs/format.js +150 -0
- package/dist/node_modules/ajv/lib/dotjs/if.js +103 -0
- package/dist/node_modules/ajv/lib/dotjs/index.js +33 -0
- package/dist/node_modules/ajv/lib/dotjs/items.js +140 -0
- package/dist/node_modules/ajv/lib/dotjs/multipleOf.js +80 -0
- package/dist/node_modules/ajv/lib/dotjs/not.js +84 -0
- package/dist/node_modules/ajv/lib/dotjs/oneOf.js +73 -0
- package/dist/node_modules/ajv/lib/dotjs/pattern.js +75 -0
- package/dist/node_modules/ajv/lib/dotjs/properties.js +335 -0
- package/dist/node_modules/ajv/lib/dotjs/propertyNames.js +81 -0
- package/dist/node_modules/ajv/lib/dotjs/ref.js +124 -0
- package/dist/node_modules/ajv/lib/dotjs/required.js +270 -0
- package/dist/node_modules/ajv/lib/dotjs/uniqueItems.js +86 -0
- package/dist/node_modules/ajv/lib/dotjs/validate.js +482 -0
- package/dist/node_modules/ajv/lib/keyword.js +146 -0
- package/dist/node_modules/ajv/lib/refs/data.json +17 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-draft-04.json +149 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-draft-06.json +154 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-draft-07.json +168 -0
- package/dist/node_modules/ajv/lib/refs/json-schema-secure.json +94 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/.eslintrc.yml +27 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/.travis.yml +8 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/index.js +89 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/package.json +43 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/.eslintrc.yml +6 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/fixtures/schema.js +125 -0
- package/dist/node_modules/ajv/node_modules/json-schema-traverse/spec/index.spec.js +171 -0
- package/dist/node_modules/ajv/package.json +1 -0
- package/dist/node_modules/ajv/scripts/.eslintrc.yml +3 -0
- package/dist/node_modules/ajv/scripts/bundle.js +61 -0
- package/dist/node_modules/ajv/scripts/compile-dots.js +73 -0
- package/dist/node_modules/ajv/scripts/info +10 -0
- package/dist/node_modules/ajv/scripts/prepare-tests +12 -0
- package/dist/node_modules/ajv/scripts/publish-built-version +32 -0
- package/dist/node_modules/ajv/scripts/travis-gh-pages +23 -0
- package/dist/server/collections/skill-definitions.js +31 -0
- package/dist/server/collections/skill-executions.js +19 -0
- package/dist/server/collections/skill-registry-installations.js +70 -0
- package/dist/server/migrations/20260621000000-native-policy-profile-defaults.js +0 -3
- package/dist/server/migrations/20260727000000-remove-unused-knowledge-scopes.js +51 -0
- package/dist/server/migrations/20260727010000-backfill-skill-tool-names.js +66 -0
- package/dist/server/plugin.js +107 -2
- package/dist/server/resources/agent-knowledge-insights.js +273 -0
- package/dist/server/services/AgentExecutionContext.js +45 -0
- package/dist/server/services/AgentMemoryContextService.js +57 -19
- package/dist/server/services/CodeValidator.js +1 -0
- package/dist/server/services/ExecutionSpanService.js +10 -2
- package/dist/server/services/InputSchemaValidator.js +83 -0
- package/dist/server/services/NativeSubAgentObserver.js +63 -8
- package/dist/server/services/SandboxRunner.js +6 -2
- package/dist/server/services/SkillAccessService.js +109 -0
- package/dist/server/services/SkillManager.js +2 -0
- package/dist/server/services/SkillRepositoryService.js +70 -0
- package/dist/server/skill-hub/actions/git-import.js +479 -216
- package/dist/server/skill-hub/mcp/McpController.js +8 -5
- package/dist/server/skill-hub/middlewares/git-import-request-method-policy.js +69 -0
- package/dist/server/skill-hub/plugin.js +68 -14
- package/dist/server/skill-hub/services/RegistrySkillInstallationService.js +249 -0
- package/dist/server/skill-hub/services/RegistrySkillSnapshotService.js +228 -0
- package/dist/server/skill-hub/tasks/SkillExecutionTask.js +15 -3
- package/dist/server/tools/external-rag-search.js +152 -12
- package/dist/server/tools/skill-execute.js +23 -2
- package/dist/server/utils/ctx-utils.js +2 -2
- package/dist/server/utils/skill-tool-name.js +73 -0
- package/package.json +9 -3
- package/src/client-v2/components/AgentBindingsTab.tsx +269 -0
- package/src/client-v2/components/HarnessProfilesTab.tsx +1 -2
- package/src/client-v2/components/KnowledgeAccessTab.tsx +116 -0
- package/src/client-v2/components/MemoryInspectorTab.tsx +125 -0
- package/src/client-v2/components/RetrievalTraceTab.tsx +162 -0
- package/src/client-v2/pages/AgentBindingsPage.tsx +13 -0
- package/src/client-v2/pages/KnowledgeAccessPage.tsx +10 -0
- package/src/client-v2/pages/MemoryInspectorPage.tsx +13 -0
- package/src/client-v2/pages/RetrievalTracePage.tsx +10 -0
- package/src/client-v2/plugin.tsx +28 -0
- package/src/locale/en-US.json +56 -2
- package/src/locale/vi-VN.json +56 -2
- package/src/locale/zh-CN.json +56 -2
- package/src/server/__tests__/agent-execution-context.test.ts +18 -0
- package/src/server/__tests__/agent-knowledge-insights.test.ts +123 -0
- package/src/server/__tests__/external-rag-search.test.ts +107 -0
- package/src/server/__tests__/input-schema-validator.test.ts +26 -0
- package/src/server/__tests__/native-sub-agent-observer.test.ts +141 -9
- package/src/server/__tests__/skill-access-service.test.ts +64 -0
- package/src/server/__tests__/skill-execute.test.ts +37 -0
- package/src/server/__tests__/skill-tool-name.test.ts +23 -0
- package/src/server/collections/skill-definitions.ts +31 -0
- package/src/server/collections/skill-executions.ts +19 -0
- package/src/server/collections/skill-registry-installations.ts +41 -0
- package/src/server/migrations/20260621000000-native-policy-profile-defaults.ts +0 -3
- package/src/server/migrations/20260727000000-remove-unused-knowledge-scopes.ts +25 -0
- package/src/server/migrations/20260727010000-backfill-skill-tool-names.ts +45 -0
- package/src/server/plugin.ts +107 -1
- package/src/server/resources/agent-knowledge-insights.ts +304 -0
- package/src/server/services/AgentExecutionContext.ts +23 -0
- package/src/server/services/AgentMemoryContextService.ts +63 -19
- package/src/server/services/CodeValidator.ts +1 -0
- package/src/server/services/ExecutionSpanService.ts +119 -110
- package/src/server/services/InputSchemaValidator.ts +44 -0
- package/src/server/services/NativeSubAgentObserver.ts +592 -528
- package/src/server/services/SandboxRunner.ts +6 -0
- package/src/server/services/SkillAccessService.ts +100 -0
- package/src/server/services/SkillManager.ts +2 -0
- package/src/server/services/SkillRepositoryService.ts +98 -2
- package/src/server/services/__tests__/SkillRepositoryService.test.ts +33 -0
- package/src/server/skill-hub/actions/__tests__/git-import.test.ts +501 -0
- package/src/server/skill-hub/actions/git-import.ts +682 -236
- package/src/server/skill-hub/mcp/McpController.ts +8 -14
- package/src/server/skill-hub/middlewares/git-import-request-method-policy.ts +51 -0
- package/src/server/skill-hub/plugin.ts +79 -13
- package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +281 -0
- package/src/server/skill-hub/services/RegistrySkillSnapshotService.ts +239 -0
- package/src/server/skill-hub/services/__tests__/RegistrySkillInstallationService.test.ts +295 -0
- package/src/server/skill-hub/services/__tests__/RegistrySkillSnapshotService.test.ts +63 -0
- package/src/server/skill-hub/tasks/SkillExecutionTask.ts +14 -2
- package/src/server/tools/external-rag-search.ts +215 -14
- package/src/server/tools/skill-execute.ts +24 -2
- package/src/server/utils/ctx-utils.ts +1 -1
- package/src/server/utils/skill-tool-name.ts +58 -0
- package/dist/client-v2/418.29e713f79131eece.js +0 -10
|
@@ -1,528 +1,592 @@
|
|
|
1
|
-
import { createHash } from 'crypto';
|
|
2
|
-
import { ExecutionSpanService } from './ExecutionSpanService';
|
|
3
|
-
import { AgentMemoryContextService } from './AgentMemoryContextService';
|
|
4
|
-
import { TokenTracker } from './TokenTracker';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
normalizeString(readValue(model, '
|
|
84
|
-
normalizeString(readValue(model, '
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
.
|
|
94
|
-
.
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
},
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
await
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
this.
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
const
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
:
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
const
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (!
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
const
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import { ExecutionSpanService } from './ExecutionSpanService';
|
|
3
|
+
import { AgentMemoryContextService } from './AgentMemoryContextService';
|
|
4
|
+
import { TokenTracker } from './TokenTracker';
|
|
5
|
+
import { runWithAgentExecutionContext } from './AgentExecutionContext';
|
|
6
|
+
import { asObject, currentUserId, toPlain, trimText } from '../utils/ctx-utils';
|
|
7
|
+
|
|
8
|
+
const WRAP_STATE_KEY = Symbol.for('plugin-agent-orchestrator.nativeSubAgentObserver');
|
|
9
|
+
const NATIVE_SOURCE = 'native-plugin-ai';
|
|
10
|
+
|
|
11
|
+
type NativeWriter = (chunk: Record<string, unknown>) => unknown;
|
|
12
|
+
|
|
13
|
+
type NativeSubAgentTask = {
|
|
14
|
+
ctx: {
|
|
15
|
+
action?: { params?: { values?: Record<string, unknown> } };
|
|
16
|
+
auth?: { user?: { id?: string | number } };
|
|
17
|
+
db?: { getRepository?: (name: string) => unknown };
|
|
18
|
+
app?: unknown;
|
|
19
|
+
};
|
|
20
|
+
sessionId: string;
|
|
21
|
+
employee: unknown;
|
|
22
|
+
model?: unknown;
|
|
23
|
+
question: string;
|
|
24
|
+
skillSettings?: Record<string, unknown>;
|
|
25
|
+
writer?: NativeWriter;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type DispatchToolMessage = {
|
|
29
|
+
id?: string | number;
|
|
30
|
+
messageId?: string | number;
|
|
31
|
+
toolCallId?: string;
|
|
32
|
+
sessionId?: string;
|
|
33
|
+
status?: string;
|
|
34
|
+
invokeStatus?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type SpanRecord = {
|
|
38
|
+
id?: string | number;
|
|
39
|
+
rootRunId?: string;
|
|
40
|
+
metadata?: Record<string, unknown>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type ToolSpanState = {
|
|
44
|
+
spanId?: string | number;
|
|
45
|
+
startedAt: number;
|
|
46
|
+
ready: Promise<void>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type RunState = {
|
|
50
|
+
rootRunId: string;
|
|
51
|
+
rootSpanId?: string | number;
|
|
52
|
+
rootStartedAt: number;
|
|
53
|
+
rootMetadata: Record<string, unknown>;
|
|
54
|
+
parentSessionId?: string;
|
|
55
|
+
subSessionId?: string;
|
|
56
|
+
toolCallId?: string;
|
|
57
|
+
leaderUsername?: string;
|
|
58
|
+
employeeUsername?: string;
|
|
59
|
+
userId?: string | number;
|
|
60
|
+
toolSpans: Map<string, ToolSpanState>;
|
|
61
|
+
pending: Set<Promise<void>>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
type DispatcherWithRun = {
|
|
65
|
+
run: (task: NativeSubAgentTask) => Promise<string>;
|
|
66
|
+
[WRAP_STATE_KEY]?: {
|
|
67
|
+
originalRun: (task: NativeSubAgentTask) => Promise<string>;
|
|
68
|
+
wrappedRun: (task: NativeSubAgentTask) => Promise<string>;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
function readValue(record: unknown, key: string) {
|
|
73
|
+
const model = record as { get?: (name: string) => unknown; [key: string]: unknown };
|
|
74
|
+
return typeof model?.get === 'function' ? model.get(key) : model?.[key];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeString(value: unknown) {
|
|
78
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function modelUsername(model: unknown) {
|
|
82
|
+
return (
|
|
83
|
+
normalizeString(readValue(model, 'username')) ||
|
|
84
|
+
normalizeString(readValue(model, 'aiEmployeeUsername')) ||
|
|
85
|
+
normalizeString(readValue(model, 'name')) ||
|
|
86
|
+
undefined
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function makeNativeRunId(parts: Array<string | number | undefined>) {
|
|
91
|
+
const source = parts.map((part) => String(part || '')).join('|');
|
|
92
|
+
return `native_${createHash('sha1')
|
|
93
|
+
.update(source || String(Date.now()))
|
|
94
|
+
.digest('hex')
|
|
95
|
+
.slice(0, 24)}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function errorMessage(error: unknown) {
|
|
99
|
+
return error instanceof Error ? error.message : String(error || 'Unknown error');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function chunkBody(chunk: Record<string, unknown>) {
|
|
103
|
+
return asObject(chunk.body);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function chunkCurrentConversation(chunk: Record<string, unknown>) {
|
|
107
|
+
return asObject(chunk.currentConversation);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function readSkillExecutionId(result: Record<string, unknown>): string | number | undefined {
|
|
111
|
+
const nestedResult = asObject(result.result);
|
|
112
|
+
const direct = nestedResult.execId || result.execId;
|
|
113
|
+
if (typeof direct === 'string' || typeof direct === 'number') return direct;
|
|
114
|
+
|
|
115
|
+
if (typeof result.content !== 'string') return undefined;
|
|
116
|
+
try {
|
|
117
|
+
const content = asObject(JSON.parse(result.content));
|
|
118
|
+
return typeof content.execId === 'string' || typeof content.execId === 'number' ? content.execId : undefined;
|
|
119
|
+
} catch {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export class NativeSubAgentObserver {
|
|
125
|
+
private readonly spanService: ExecutionSpanService;
|
|
126
|
+
private readonly memoryService: AgentMemoryContextService;
|
|
127
|
+
private readonly tokenTracker: TokenTracker;
|
|
128
|
+
|
|
129
|
+
constructor(private readonly plugin: { app: any; db: any; name?: string }) {
|
|
130
|
+
this.spanService = new ExecutionSpanService(plugin);
|
|
131
|
+
this.memoryService = new AgentMemoryContextService(plugin);
|
|
132
|
+
this.tokenTracker = new TokenTracker(plugin);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
install() {
|
|
136
|
+
const aiPlugin = this.plugin.app.pm?.get?.('ai');
|
|
137
|
+
const dispatcher = aiPlugin?.subAgentsDispatcher as DispatcherWithRun | undefined;
|
|
138
|
+
if (!dispatcher?.run) {
|
|
139
|
+
this.plugin.app.logger?.warn?.(
|
|
140
|
+
'[AgentOrchestrator] plugin-ai subAgentsDispatcher not available; observer skipped.',
|
|
141
|
+
);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const existing = dispatcher[WRAP_STATE_KEY];
|
|
146
|
+
if (existing?.wrappedRun === dispatcher.run) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const originalRun = existing?.originalRun || dispatcher.run.bind(dispatcher);
|
|
151
|
+
const observer = this;
|
|
152
|
+
const wrappedRun = async function wrappedNativeSubAgentRun(task: NativeSubAgentTask) {
|
|
153
|
+
return observer.runObserved(originalRun, task);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
dispatcher.run = wrappedRun;
|
|
157
|
+
dispatcher[WRAP_STATE_KEY] = {
|
|
158
|
+
originalRun,
|
|
159
|
+
wrappedRun,
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
this.plugin.app.logger?.info?.('[AgentOrchestrator] Native plugin-ai sub-agent observer installed.');
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
uninstall() {
|
|
167
|
+
const aiPlugin = this.plugin.app.pm?.get?.('ai');
|
|
168
|
+
const dispatcher = aiPlugin?.subAgentsDispatcher as DispatcherWithRun | undefined;
|
|
169
|
+
const existing = dispatcher?.[WRAP_STATE_KEY];
|
|
170
|
+
if (!dispatcher?.run || !existing || existing.wrappedRun !== dispatcher.run) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
dispatcher.run = existing.originalRun;
|
|
175
|
+
delete dispatcher[WRAP_STATE_KEY];
|
|
176
|
+
this.plugin.app.logger?.info?.('[AgentOrchestrator] Native plugin-ai sub-agent observer uninstalled.');
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private async runObserved(originalRun: (task: NativeSubAgentTask) => Promise<string>, task: NativeSubAgentTask) {
|
|
181
|
+
const settings = await this.safeResolvePolicy(task);
|
|
182
|
+
if (settings.nativeObserverEnabled === false) {
|
|
183
|
+
return originalRun(task);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const parentSessionId = this.resolveParentSessionId(task) || undefined;
|
|
187
|
+
const subSessionId = normalizeString(task.sessionId) || undefined;
|
|
188
|
+
const employeeUsername = modelUsername(task.employee);
|
|
189
|
+
const userId = currentUserId(task.ctx) || task.ctx?.auth?.user?.id;
|
|
190
|
+
const dispatchToolMessage = await this.resolveDispatchToolMessage(task.ctx, parentSessionId, subSessionId);
|
|
191
|
+
const toolCallId = dispatchToolMessage?.toolCallId;
|
|
192
|
+
const leaderUsername = await this.resolveLeaderUsername(task.ctx, parentSessionId);
|
|
193
|
+
const rootRunId = makeNativeRunId([parentSessionId, subSessionId, toolCallId, employeeUsername]);
|
|
194
|
+
const memory = await this.safeBuildMemoryContext({
|
|
195
|
+
userId,
|
|
196
|
+
aiEmployeeUsername: employeeUsername,
|
|
197
|
+
settings,
|
|
198
|
+
});
|
|
199
|
+
const observedTask: NativeSubAgentTask = {
|
|
200
|
+
...task,
|
|
201
|
+
// Native dispatch passes the leader conversation's skillSettings to the
|
|
202
|
+
// sub-agent. Clear that inherited filter so plugin-ai resolves tools from
|
|
203
|
+
// the sub-agent employee's own bindings instead.
|
|
204
|
+
skillSettings: undefined,
|
|
205
|
+
question: memory.context ? `${memory.context}\n\n<agent_task>\n${task.question}\n</agent_task>` : task.question,
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const state: RunState = {
|
|
209
|
+
rootRunId,
|
|
210
|
+
rootStartedAt: Date.now(),
|
|
211
|
+
rootMetadata: {
|
|
212
|
+
source: NATIVE_SOURCE,
|
|
213
|
+
parentSessionId,
|
|
214
|
+
subSessionId,
|
|
215
|
+
toolCallId,
|
|
216
|
+
dispatchToolMessageId: dispatchToolMessage?.id,
|
|
217
|
+
dispatchMessageId: dispatchToolMessage?.messageId,
|
|
218
|
+
memoryContextApplied: Boolean(memory.context),
|
|
219
|
+
memoryScopes: memory.appliedScopes,
|
|
220
|
+
memoryContextChars: memory.chars,
|
|
221
|
+
harnessTag: settings.harnessTag || 'default',
|
|
222
|
+
},
|
|
223
|
+
parentSessionId,
|
|
224
|
+
subSessionId,
|
|
225
|
+
toolCallId,
|
|
226
|
+
leaderUsername,
|
|
227
|
+
employeeUsername,
|
|
228
|
+
userId,
|
|
229
|
+
toolSpans: new Map(),
|
|
230
|
+
pending: new Set(),
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
await this.safeCreateRootSpan(state, task.question);
|
|
234
|
+
|
|
235
|
+
const originalWriter = task.writer;
|
|
236
|
+
observedTask.writer = (chunk: Record<string, unknown>) => {
|
|
237
|
+
const pending = this.handleWriterChunk(state, chunk).catch((error) => {
|
|
238
|
+
this.plugin.app.logger?.warn?.('[AgentOrchestrator] Native writer observer failed', error);
|
|
239
|
+
});
|
|
240
|
+
state.pending.add(pending);
|
|
241
|
+
return originalWriter?.(chunk);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
const result = await runWithAgentExecutionContext(
|
|
246
|
+
{
|
|
247
|
+
rootRunId,
|
|
248
|
+
spanId: state.rootSpanId ? String(state.rootSpanId) : undefined,
|
|
249
|
+
toolCallId,
|
|
250
|
+
leaderUsername,
|
|
251
|
+
employeeUsername,
|
|
252
|
+
sessionId: subSessionId,
|
|
253
|
+
},
|
|
254
|
+
() => this.runWithKnowledgeBaseAgentContext(employeeUsername, () => originalRun(observedTask)),
|
|
255
|
+
);
|
|
256
|
+
await this.flushPending(state);
|
|
257
|
+
await this.spanService.finish(state.rootSpanId, 'success', state.rootStartedAt, {
|
|
258
|
+
output: trimText(result, 20000),
|
|
259
|
+
metadata: {
|
|
260
|
+
...state.rootMetadata,
|
|
261
|
+
completedAt: new Date().toISOString(),
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
await this.tokenTracker.estimateAndTrack(
|
|
265
|
+
state.rootSpanId,
|
|
266
|
+
task.question,
|
|
267
|
+
result,
|
|
268
|
+
state.rootMetadata.agentLoopRunId ? Number(state.rootMetadata.agentLoopRunId) : undefined,
|
|
269
|
+
);
|
|
270
|
+
return result;
|
|
271
|
+
} catch (error) {
|
|
272
|
+
await this.flushPending(state);
|
|
273
|
+
await this.spanService.finish(state.rootSpanId, 'error', state.rootStartedAt, {
|
|
274
|
+
error: trimText(errorMessage(error), 10000),
|
|
275
|
+
metadata: {
|
|
276
|
+
...state.rootMetadata,
|
|
277
|
+
failedAt: new Date().toISOString(),
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
await this.tokenTracker.estimateAndTrack(
|
|
281
|
+
state.rootSpanId,
|
|
282
|
+
task.question,
|
|
283
|
+
errorMessage(error),
|
|
284
|
+
state.rootMetadata.agentLoopRunId ? Number(state.rootMetadata.agentLoopRunId) : undefined,
|
|
285
|
+
);
|
|
286
|
+
throw error;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private async safeResolvePolicy(task: NativeSubAgentTask) {
|
|
291
|
+
try {
|
|
292
|
+
return await this.memoryService.resolvePolicySettings(task);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
this.plugin.app.logger?.warn?.('[AgentOrchestrator] Failed to resolve native observer policy', error);
|
|
295
|
+
return { harnessTag: 'default' };
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private async safeBuildMemoryContext(options: {
|
|
300
|
+
userId?: string | number;
|
|
301
|
+
aiEmployeeUsername?: string;
|
|
302
|
+
settings: Record<string, unknown>;
|
|
303
|
+
}) {
|
|
304
|
+
try {
|
|
305
|
+
return await this.memoryService.buildContext(options);
|
|
306
|
+
} catch (error) {
|
|
307
|
+
this.plugin.app.logger?.warn?.('[AgentOrchestrator] Failed to build agent memory context', error);
|
|
308
|
+
return { context: '', appliedScopes: [] as string[], chars: 0 };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private async safeCreateRootSpan(state: RunState, question: string) {
|
|
313
|
+
const span = (await this.spanService.create({
|
|
314
|
+
rootRunId: state.rootRunId,
|
|
315
|
+
parentSpanId: undefined,
|
|
316
|
+
source: NATIVE_SOURCE,
|
|
317
|
+
parentSessionId: state.parentSessionId,
|
|
318
|
+
subSessionId: state.subSessionId,
|
|
319
|
+
toolCallId: state.toolCallId,
|
|
320
|
+
type: 'sub_agent',
|
|
321
|
+
status: 'running',
|
|
322
|
+
leaderUsername: state.leaderUsername,
|
|
323
|
+
employeeUsername: state.employeeUsername,
|
|
324
|
+
toolName: 'dispatch-sub-agent-task',
|
|
325
|
+
title: `${state.leaderUsername || 'main'} -> ${state.employeeUsername || 'sub-agent'}`,
|
|
326
|
+
input: {
|
|
327
|
+
question,
|
|
328
|
+
parentSessionId: state.parentSessionId,
|
|
329
|
+
subSessionId: state.subSessionId,
|
|
330
|
+
},
|
|
331
|
+
metadata: state.rootMetadata,
|
|
332
|
+
userId: state.userId,
|
|
333
|
+
})) as SpanRecord | null;
|
|
334
|
+
|
|
335
|
+
state.rootSpanId = span?.id;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
private runWithKnowledgeBaseAgentContext<T>(employeeUsername: string | undefined, fn: () => Promise<T>) {
|
|
339
|
+
if (!employeeUsername) return fn();
|
|
340
|
+
const knowledgeBasePlugin = this.plugin.app.pm?.get?.('plugin-knowledge-base') as
|
|
341
|
+
| { runWithAgentContext?: <TResult>(username: string, callback: () => Promise<TResult>) => Promise<TResult> }
|
|
342
|
+
| undefined;
|
|
343
|
+
return knowledgeBasePlugin?.runWithAgentContext
|
|
344
|
+
? knowledgeBasePlugin.runWithAgentContext(employeeUsername, fn)
|
|
345
|
+
: fn();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
private async handleWriterChunk(state: RunState, chunk: Record<string, unknown>) {
|
|
349
|
+
const action = chunk.action;
|
|
350
|
+
if (action === 'beforeToolCall') {
|
|
351
|
+
await this.handleBeforeToolCall(state, chunk);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (action === 'afterToolCall') {
|
|
355
|
+
await this.handleAfterToolCall(state, chunk, false);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (action === 'afterToolCallError') {
|
|
359
|
+
await this.handleAfterToolCall(state, chunk, true);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (action === 'afterSubAgentInvoke') {
|
|
363
|
+
state.rootMetadata = {
|
|
364
|
+
...state.rootMetadata,
|
|
365
|
+
afterSubAgentInvokeAt: new Date().toISOString(),
|
|
366
|
+
};
|
|
367
|
+
await this.spanService.update(state.rootSpanId, { metadata: state.rootMetadata });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
private async handleBeforeToolCall(state: RunState, chunk: Record<string, unknown>) {
|
|
372
|
+
const body = chunkBody(chunk);
|
|
373
|
+
const toolCall = asObject(body.toolCall);
|
|
374
|
+
const toolCallId = normalizeString(toolCall.id);
|
|
375
|
+
if (!toolCallId || state.toolSpans.has(toolCallId)) return;
|
|
376
|
+
|
|
377
|
+
const conversation = chunkCurrentConversation(chunk);
|
|
378
|
+
const startedAt = Date.now();
|
|
379
|
+
const toolSpan: ToolSpanState = {
|
|
380
|
+
startedAt,
|
|
381
|
+
ready: Promise.resolve(),
|
|
382
|
+
};
|
|
383
|
+
state.toolSpans.set(toolCallId, toolSpan);
|
|
384
|
+
|
|
385
|
+
const toolName = normalizeString(toolCall.name);
|
|
386
|
+
toolSpan.ready = this.spanService
|
|
387
|
+
.create({
|
|
388
|
+
rootRunId: state.rootRunId,
|
|
389
|
+
parentSpanId: state.rootSpanId ? String(state.rootSpanId) : undefined,
|
|
390
|
+
source: NATIVE_SOURCE,
|
|
391
|
+
parentSessionId: state.parentSessionId || undefined,
|
|
392
|
+
subSessionId: normalizeString(conversation.sessionId) || state.subSessionId || undefined,
|
|
393
|
+
toolCallId,
|
|
394
|
+
type:
|
|
395
|
+
toolName === 'dispatch-sub-agent-task'
|
|
396
|
+
? 'dispatch'
|
|
397
|
+
: toolName === 'skill_hub_execute' || toolName.startsWith('skill_hub_')
|
|
398
|
+
? 'skill'
|
|
399
|
+
: 'tool',
|
|
400
|
+
status: 'running',
|
|
401
|
+
leaderUsername: state.leaderUsername,
|
|
402
|
+
employeeUsername: normalizeString(conversation.username) || state.employeeUsername,
|
|
403
|
+
toolName,
|
|
404
|
+
title: toolName || 'tool call',
|
|
405
|
+
input: asObject(toolCall.args),
|
|
406
|
+
metadata: {
|
|
407
|
+
source: NATIVE_SOURCE,
|
|
408
|
+
currentConversation: conversation,
|
|
409
|
+
messageId: toolCall.messageId,
|
|
410
|
+
},
|
|
411
|
+
userId: state.userId,
|
|
412
|
+
})
|
|
413
|
+
.then((span) => {
|
|
414
|
+
toolSpan.spanId = (span as SpanRecord | null)?.id;
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
await toolSpan.ready;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private async handleAfterToolCall(state: RunState, chunk: Record<string, unknown>, forceError: boolean) {
|
|
421
|
+
const body = chunkBody(chunk);
|
|
422
|
+
const toolCall = asObject(body.toolCall);
|
|
423
|
+
const toolCallId = normalizeString(toolCall.id);
|
|
424
|
+
const existing = toolCallId ? state.toolSpans.get(toolCallId) : undefined;
|
|
425
|
+
if (!existing) return;
|
|
426
|
+
await existing.ready;
|
|
427
|
+
|
|
428
|
+
const result = asObject(toPlain(body.toolCallResult));
|
|
429
|
+
const error = body.error;
|
|
430
|
+
const status = forceError || result.status === 'error' ? 'error' : 'success';
|
|
431
|
+
const output = result.content ?? result.result ?? result;
|
|
432
|
+
const toolName = normalizeString(toolCall.name);
|
|
433
|
+
const skillExecutionId =
|
|
434
|
+
toolName === 'skill_hub_execute' || toolName.startsWith('skill_hub_') ? readSkillExecutionId(result) : undefined;
|
|
435
|
+
const invokeStart = Number(result.invokeStartTime);
|
|
436
|
+
const invokeEnd = Number(result.invokeEndTime);
|
|
437
|
+
const durationMs =
|
|
438
|
+
Number.isFinite(invokeStart) && Number.isFinite(invokeEnd) && invokeEnd >= invokeStart
|
|
439
|
+
? invokeEnd - invokeStart
|
|
440
|
+
: Date.now() - existing.startedAt;
|
|
441
|
+
|
|
442
|
+
await this.spanService.update(existing.spanId, {
|
|
443
|
+
status,
|
|
444
|
+
endedAt: new Date(),
|
|
445
|
+
durationMs,
|
|
446
|
+
output: forceError ? undefined : trimText(output, 20000),
|
|
447
|
+
error: forceError ? trimText(errorMessage(error), 10000) : undefined,
|
|
448
|
+
skillExecutionId,
|
|
449
|
+
metadata: {
|
|
450
|
+
source: NATIVE_SOURCE,
|
|
451
|
+
toolCallResultStatus: result.status,
|
|
452
|
+
invokeStatus: result.invokeStatus,
|
|
453
|
+
messageId: toolCall.messageId,
|
|
454
|
+
},
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
if (skillExecutionId && existing.spanId) {
|
|
458
|
+
await this.linkExecutionToSpan(skillExecutionId, existing.spanId);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
await this.tokenTracker.estimateAndTrack(
|
|
462
|
+
existing.spanId,
|
|
463
|
+
normalizeString(toolCall.name),
|
|
464
|
+
typeof output === 'string' ? output : JSON.stringify(output),
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
private async linkExecutionToSpan(skillExecutionId: string | number, spanId: string | number) {
|
|
469
|
+
try {
|
|
470
|
+
const repo = this.plugin.db.getRepository('skillExecutions');
|
|
471
|
+
if (!repo) return;
|
|
472
|
+
await repo.update({
|
|
473
|
+
filterByTk: skillExecutionId,
|
|
474
|
+
values: { orchestratorSpanId: spanId },
|
|
475
|
+
});
|
|
476
|
+
} catch (error) {
|
|
477
|
+
this.plugin.app.logger?.warn?.('[AgentOrchestrator] Failed to link skill execution to native span', error);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
private resolveParentSessionId(task: NativeSubAgentTask) {
|
|
482
|
+
const values = task.ctx?.action?.params?.values || {};
|
|
483
|
+
return normalizeString(values.sessionId) || undefined;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
private async resolveLeaderUsername(ctx: NativeSubAgentTask['ctx'], parentSessionId?: string) {
|
|
487
|
+
const values = ctx?.action?.params?.values || {};
|
|
488
|
+
const direct =
|
|
489
|
+
normalizeString(values.aiEmployeeUsername) ||
|
|
490
|
+
normalizeString((asObject(values.aiEmployee) as Record<string, unknown>).username);
|
|
491
|
+
if (direct) return direct;
|
|
492
|
+
if (!parentSessionId) return undefined;
|
|
493
|
+
|
|
494
|
+
try {
|
|
495
|
+
const repo = ctx.db?.getRepository?.('aiConversations') as
|
|
496
|
+
| { findOne?: (options: Record<string, unknown>) => Promise<unknown> }
|
|
497
|
+
| undefined;
|
|
498
|
+
const conversation = await repo?.findOne?.({
|
|
499
|
+
filter: { sessionId: parentSessionId },
|
|
500
|
+
});
|
|
501
|
+
return normalizeString(readValue(conversation, 'aiEmployeeUsername')) || undefined;
|
|
502
|
+
} catch {
|
|
503
|
+
return undefined;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
private async resolveDispatchToolMessage(
|
|
508
|
+
ctx: NativeSubAgentTask['ctx'],
|
|
509
|
+
parentSessionId?: string,
|
|
510
|
+
subSessionId?: string,
|
|
511
|
+
) {
|
|
512
|
+
if (!parentSessionId) return null;
|
|
513
|
+
try {
|
|
514
|
+
const fromMetadata = await this.resolveDispatchToolMessageFromMetadata(ctx, parentSessionId, subSessionId);
|
|
515
|
+
if (fromMetadata) {
|
|
516
|
+
return fromMetadata;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const repo = ctx.db?.getRepository?.('aiToolMessages') as
|
|
520
|
+
| { findOne?: (options: Record<string, unknown>) => Promise<unknown> }
|
|
521
|
+
| undefined;
|
|
522
|
+
const record = await repo?.findOne?.({
|
|
523
|
+
filter: {
|
|
524
|
+
sessionId: parentSessionId,
|
|
525
|
+
toolName: 'dispatch-sub-agent-task',
|
|
526
|
+
invokeStatus: {
|
|
527
|
+
$ne: 'confirmed',
|
|
528
|
+
},
|
|
529
|
+
},
|
|
530
|
+
sort: ['-id'],
|
|
531
|
+
});
|
|
532
|
+
return toPlain(record) as DispatchToolMessage | null;
|
|
533
|
+
} catch {
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
private async resolveDispatchToolMessageFromMetadata(
|
|
539
|
+
ctx: NativeSubAgentTask['ctx'],
|
|
540
|
+
parentSessionId?: string,
|
|
541
|
+
subSessionId?: string,
|
|
542
|
+
) {
|
|
543
|
+
if (!parentSessionId || !subSessionId) return null;
|
|
544
|
+
|
|
545
|
+
const messageRepo = ctx.db?.getRepository?.('aiMessages') as
|
|
546
|
+
| { find?: (options: Record<string, unknown>) => Promise<unknown[]> }
|
|
547
|
+
| undefined;
|
|
548
|
+
const toolRepo = ctx.db?.getRepository?.('aiToolMessages') as
|
|
549
|
+
| { findOne?: (options: Record<string, unknown>) => Promise<unknown> }
|
|
550
|
+
| undefined;
|
|
551
|
+
const messages = await messageRepo?.find?.({
|
|
552
|
+
filter: { sessionId: parentSessionId },
|
|
553
|
+
sort: ['-messageId'],
|
|
554
|
+
limit: 20,
|
|
555
|
+
});
|
|
556
|
+
if (!Array.isArray(messages)) return null;
|
|
557
|
+
|
|
558
|
+
for (const rawMessage of messages) {
|
|
559
|
+
const message = toPlain(rawMessage);
|
|
560
|
+
const conversations = asObject(message?.metadata).subAgentConversations;
|
|
561
|
+
if (!Array.isArray(conversations)) continue;
|
|
562
|
+
|
|
563
|
+
const matched = conversations.find((item) => normalizeString(item?.sessionId) === subSessionId);
|
|
564
|
+
const toolCallId = normalizeString(matched?.toolCallId);
|
|
565
|
+
if (!toolCallId) continue;
|
|
566
|
+
|
|
567
|
+
const toolMessage = await toolRepo?.findOne?.({
|
|
568
|
+
filter: {
|
|
569
|
+
sessionId: parentSessionId,
|
|
570
|
+
toolCallId,
|
|
571
|
+
},
|
|
572
|
+
});
|
|
573
|
+
return {
|
|
574
|
+
...(toPlain(toolMessage) || {}),
|
|
575
|
+
toolCallId,
|
|
576
|
+
messageId: readValue(toolMessage, 'messageId') || message?.messageId,
|
|
577
|
+
sessionId: parentSessionId,
|
|
578
|
+
status: matched?.status,
|
|
579
|
+
} as DispatchToolMessage;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
private async flushPending(state: RunState) {
|
|
586
|
+
const pending = Array.from(state.pending);
|
|
587
|
+
if (!pending.length) return;
|
|
588
|
+
await Promise.allSettled(pending);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
export { NATIVE_SOURCE, makeNativeRunId };
|