@agentica/core 0.27.2 → 0.28.0
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/README.md +17 -2
- package/lib/Agentica.d.ts +2 -1
- package/lib/Agentica.js +92 -60
- package/lib/Agentica.js.map +1 -1
- package/lib/MicroAgentica.d.ts +1 -0
- package/lib/MicroAgentica.js +88 -60
- package/lib/MicroAgentica.js.map +1 -1
- package/lib/constants/AgenticaDefaultPrompt.js +2 -18
- package/lib/constants/AgenticaDefaultPrompt.js.map +1 -1
- package/lib/context/AgenticaContext.d.ts +1 -1
- package/lib/context/MicroAgenticaContext.d.ts +2 -2
- package/lib/context/internal/AgenticaOperationComposer.spec.js +0 -2
- package/lib/context/internal/AgenticaOperationComposer.spec.js.map +1 -1
- package/lib/events/AgenticaEventBase.d.ts +9 -0
- package/lib/factory/events.d.ts +1 -2
- package/lib/factory/events.js +71 -7
- package/lib/factory/events.js.map +1 -1
- package/lib/factory/histories.js +27 -9
- package/lib/factory/histories.js.map +1 -1
- package/lib/functional/assertMcpController.d.ts +1 -1
- package/lib/functional/assertMcpController.js.map +1 -1
- package/lib/functional/validateMcpController.d.ts +1 -1
- package/lib/functional/validateMcpController.js.map +1 -1
- package/lib/histories/AgenticaCancelHistory.d.ts +1 -2
- package/lib/histories/AgenticaExecuteHistory.d.ts +0 -4
- package/lib/histories/AgenticaHistoryBase.d.ts +9 -0
- package/lib/histories/AgenticaSelectHistory.d.ts +1 -2
- package/lib/index.mjs +413 -380
- package/lib/index.mjs.map +1 -1
- package/lib/json/IAgenticaEventJson.d.ts +9 -1
- package/lib/json/IAgenticaHistoryJson.d.ts +11 -14
- package/lib/orchestrate/call.d.ts +2 -2
- package/lib/orchestrate/call.js +41 -70
- package/lib/orchestrate/call.js.map +1 -1
- package/lib/orchestrate/cancel.d.ts +1 -2
- package/lib/orchestrate/cancel.js +13 -38
- package/lib/orchestrate/cancel.js.map +1 -1
- package/lib/orchestrate/describe.d.ts +1 -2
- package/lib/orchestrate/describe.js +5 -17
- package/lib/orchestrate/describe.js.map +1 -1
- package/lib/orchestrate/execute.d.ts +1 -2
- package/lib/orchestrate/execute.js +9 -13
- package/lib/orchestrate/execute.js.map +1 -1
- package/lib/orchestrate/initialize.d.ts +1 -2
- package/lib/orchestrate/initialize.js +3 -11
- package/lib/orchestrate/initialize.js.map +1 -1
- package/lib/orchestrate/internal/{cancelFunction.js → cancelFunctionFromContext.js} +7 -7
- package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -0
- package/lib/orchestrate/internal/selectFunctionFromContext.js +24 -0
- package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -0
- package/lib/orchestrate/select.d.ts +1 -2
- package/lib/orchestrate/select.js +20 -51
- package/lib/orchestrate/select.js.map +1 -1
- package/lib/structures/IAgenticaConfig.d.ts +1 -3
- package/lib/structures/IAgenticaController.d.ts +123 -88
- package/lib/structures/IAgenticaExecutor.d.ts +6 -7
- package/lib/structures/IAgenticaVendor.d.ts +14 -0
- package/lib/structures/IMicroAgenticaExecutor.d.ts +2 -3
- package/lib/transformers/transformHistory.js +13 -8
- package/lib/transformers/transformHistory.js.map +1 -1
- package/lib/utils/ChatGptCompletionMessageUtil.js +3 -3
- package/package.json +13 -11
- package/src/Agentica.ts +127 -87
- package/src/MicroAgentica.ts +118 -81
- package/src/constants/AgenticaDefaultPrompt.ts +3 -20
- package/src/context/AgenticaContext.ts +1 -1
- package/src/context/MicroAgenticaContext.ts +2 -2
- package/src/context/internal/AgenticaOperationComposer.spec.ts +1 -2
- package/src/events/AgenticaEventBase.ts +12 -0
- package/src/factory/events.ts +78 -8
- package/src/factory/histories.ts +41 -11
- package/src/functional/assertMcpController.ts +1 -4
- package/src/functional/validateMcpController.ts +1 -4
- package/src/histories/AgenticaCancelHistory.ts +1 -2
- package/src/histories/AgenticaExecuteHistory.ts +0 -5
- package/src/histories/AgenticaHistoryBase.ts +12 -0
- package/src/histories/AgenticaSelectHistory.ts +1 -2
- package/src/json/IAgenticaEventJson.ts +11 -1
- package/src/json/IAgenticaHistoryJson.ts +14 -17
- package/src/orchestrate/call.ts +57 -107
- package/src/orchestrate/cancel.ts +76 -99
- package/src/orchestrate/describe.ts +16 -36
- package/src/orchestrate/execute.ts +17 -37
- package/src/orchestrate/initialize.ts +36 -49
- package/src/orchestrate/internal/{cancelFunction.ts → cancelFunctionFromContext.ts} +11 -11
- package/src/orchestrate/internal/{selectFunction.ts → selectFunctionFromContext.ts} +18 -13
- package/src/orchestrate/select.ts +112 -151
- package/src/structures/IAgenticaConfig.ts +1 -3
- package/src/structures/IAgenticaController.ts +4 -0
- package/src/structures/IAgenticaExecutor.ts +10 -8
- package/src/structures/IAgenticaVendor.ts +15 -0
- package/src/structures/IMicroAgenticaExecutor.ts +2 -3
- package/src/transformers/transformHistory.ts +19 -20
- package/lib/orchestrate/internal/cancelFunction.js.map +0 -1
- package/lib/orchestrate/internal/selectFunction.js +0 -35
- package/lib/orchestrate/internal/selectFunction.js.map +0 -1
- /package/lib/orchestrate/internal/{cancelFunction.d.ts → cancelFunctionFromContext.d.ts} +0 -0
- /package/lib/orchestrate/internal/{selectFunction.d.ts → selectFunctionFromContext.d.ts} +0 -0
package/src/orchestrate/call.ts
CHANGED
|
@@ -16,29 +16,25 @@ import {
|
|
|
16
16
|
import type { AgenticaContext } from "../context/AgenticaContext";
|
|
17
17
|
import type { AgenticaOperation } from "../context/AgenticaOperation";
|
|
18
18
|
import type { MicroAgenticaContext } from "../context/MicroAgenticaContext";
|
|
19
|
+
import type { AgenticaAssistantMessageEvent, AgenticaExecuteEvent } from "../events";
|
|
19
20
|
import type { AgenticaCallEvent } from "../events/AgenticaCallEvent";
|
|
20
|
-
import type { AgenticaAssistantMessageHistory } from "../histories/AgenticaAssistantMessageHistory";
|
|
21
|
-
import type { AgenticaCancelHistory } from "../histories/AgenticaCancelHistory";
|
|
22
|
-
import type { AgenticaExecuteHistory } from "../histories/AgenticaExecuteHistory";
|
|
23
|
-
import type { AgenticaHistory } from "../histories/AgenticaHistory";
|
|
24
21
|
import type { MicroAgenticaHistory } from "../histories/MicroAgenticaHistory";
|
|
25
22
|
|
|
26
23
|
import { AgenticaConstant } from "../constants/AgenticaConstant";
|
|
27
24
|
import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
|
|
28
25
|
import { AgenticaSystemPrompt } from "../constants/AgenticaSystemPrompt";
|
|
29
26
|
import { isAgenticaContext } from "../context/internal/isAgenticaContext";
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import { createOperationSelection } from "../factory/operations";
|
|
27
|
+
import { creatAssistantMessageEvent, createCallEvent, createExecuteEvent, createValidateEvent } from "../factory/events";
|
|
28
|
+
import { decodeHistory, decodeUserMessageContent } from "../factory/histories";
|
|
33
29
|
import { ChatGptCompletionMessageUtil } from "../utils/ChatGptCompletionMessageUtil";
|
|
34
30
|
import { StreamUtil, toAsyncGenerator } from "../utils/StreamUtil";
|
|
35
31
|
|
|
36
|
-
import {
|
|
32
|
+
import { cancelFunctionFromContext } from "./internal/cancelFunctionFromContext";
|
|
37
33
|
|
|
38
34
|
export async function call<Model extends ILlmSchema.Model>(
|
|
39
35
|
ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
|
|
40
36
|
operations: AgenticaOperation<Model>[],
|
|
41
|
-
): Promise<
|
|
37
|
+
): Promise<AgenticaExecuteEvent<Model>[]> {
|
|
42
38
|
// ----
|
|
43
39
|
// EXECUTE CHATGPT API
|
|
44
40
|
// ----
|
|
@@ -99,18 +95,9 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
99
95
|
// ----
|
|
100
96
|
// PROCESS COMPLETION
|
|
101
97
|
// ----
|
|
102
|
-
const closures: Array<
|
|
103
|
-
() => Promise<
|
|
104
|
-
Array<
|
|
105
|
-
| AgenticaExecuteHistory<Model>
|
|
106
|
-
| AgenticaCancelHistory<Model>
|
|
107
|
-
| AgenticaAssistantMessageHistory
|
|
108
|
-
>
|
|
109
|
-
>
|
|
110
|
-
> = [];
|
|
111
|
-
|
|
112
98
|
const chunks = await StreamUtil.readAll(completionStream);
|
|
113
99
|
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
100
|
+
const executes: AgenticaExecuteEvent<Model>[] = [];
|
|
114
101
|
|
|
115
102
|
for (const choice of completion.choices) {
|
|
116
103
|
for (const tc of choice.message.tool_calls ?? []) {
|
|
@@ -120,59 +107,34 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
120
107
|
if (operation === undefined) {
|
|
121
108
|
continue;
|
|
122
109
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const execute: AgenticaExecuteHistory<Model> = await propagate(
|
|
142
|
-
ctx,
|
|
143
|
-
call,
|
|
144
|
-
0,
|
|
145
|
-
);
|
|
146
|
-
ctx.dispatch(
|
|
147
|
-
createExecuteEvent({
|
|
148
|
-
id: call.id,
|
|
149
|
-
operation: call.operation,
|
|
150
|
-
arguments: execute.arguments,
|
|
151
|
-
value: execute.value,
|
|
152
|
-
}),
|
|
153
|
-
).catch(() => {});
|
|
154
|
-
|
|
155
|
-
if (isAgenticaContext(ctx)) {
|
|
156
|
-
cancelFunction(ctx, {
|
|
157
|
-
name: call.operation.name,
|
|
158
|
-
reason: "completed",
|
|
159
|
-
});
|
|
160
|
-
return [
|
|
161
|
-
execute,
|
|
162
|
-
createCancelHistory({
|
|
163
|
-
id: call.id,
|
|
164
|
-
selections: [
|
|
165
|
-
createOperationSelection({
|
|
166
|
-
operation: call.operation,
|
|
167
|
-
reason: "complete",
|
|
168
|
-
}),
|
|
169
|
-
],
|
|
170
|
-
}),
|
|
171
|
-
];
|
|
172
|
-
}
|
|
173
|
-
return [execute];
|
|
174
|
-
},
|
|
110
|
+
const call: AgenticaCallEvent<Model> = createCallEvent({
|
|
111
|
+
id: tc.id,
|
|
112
|
+
operation,
|
|
113
|
+
// @TODO add type assertion!
|
|
114
|
+
arguments: JSON.parse(tc.function.arguments) as Record<string, unknown>,
|
|
115
|
+
});
|
|
116
|
+
if (call.operation.protocol === "http") {
|
|
117
|
+
fillHttpArguments({
|
|
118
|
+
operation: call.operation,
|
|
119
|
+
arguments: call.arguments,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
ctx.dispatch(call);
|
|
123
|
+
|
|
124
|
+
const exec: AgenticaExecuteEvent<Model> = await propagate(
|
|
125
|
+
ctx,
|
|
126
|
+
call,
|
|
127
|
+
0,
|
|
175
128
|
);
|
|
129
|
+
ctx.dispatch(exec);
|
|
130
|
+
executes.push(exec);
|
|
131
|
+
|
|
132
|
+
if (isAgenticaContext(ctx)) {
|
|
133
|
+
cancelFunctionFromContext(ctx, {
|
|
134
|
+
name: call.operation.name,
|
|
135
|
+
reason: "completed",
|
|
136
|
+
});
|
|
137
|
+
}
|
|
176
138
|
}
|
|
177
139
|
}
|
|
178
140
|
if (
|
|
@@ -180,30 +142,24 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
180
142
|
&& choice.message.content != null
|
|
181
143
|
&& choice.message.content.length !== 0
|
|
182
144
|
) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
)
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
get: () => value.text,
|
|
190
|
-
done: () => true,
|
|
191
|
-
stream: toAsyncGenerator(value.text),
|
|
192
|
-
join: async () => Promise.resolve(value.text),
|
|
193
|
-
}),
|
|
194
|
-
).catch(() => {});
|
|
195
|
-
return [value];
|
|
145
|
+
const text: string = choice.message.content;
|
|
146
|
+
const event: AgenticaAssistantMessageEvent = creatAssistantMessageEvent({
|
|
147
|
+
get: () => text,
|
|
148
|
+
done: () => true,
|
|
149
|
+
stream: toAsyncGenerator(text),
|
|
150
|
+
join: async () => Promise.resolve(text),
|
|
196
151
|
});
|
|
152
|
+
ctx.dispatch(event);
|
|
197
153
|
}
|
|
198
154
|
}
|
|
199
|
-
return
|
|
155
|
+
return executes;
|
|
200
156
|
}
|
|
201
157
|
|
|
202
158
|
async function propagate<Model extends ILlmSchema.Model>(
|
|
203
159
|
ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
|
|
204
160
|
call: AgenticaCallEvent<Model>,
|
|
205
161
|
retry: number,
|
|
206
|
-
): Promise<
|
|
162
|
+
): Promise<AgenticaExecuteEvent<Model>> {
|
|
207
163
|
switch (call.operation.protocol) {
|
|
208
164
|
case "http": {
|
|
209
165
|
return propagateHttp({ ctx, operation: call.operation, call, retry });
|
|
@@ -228,7 +184,7 @@ async function propagateHttp<Model extends ILlmSchema.Model>(
|
|
|
228
184
|
call: AgenticaCallEvent<Model>;
|
|
229
185
|
retry: number;
|
|
230
186
|
},
|
|
231
|
-
): Promise<
|
|
187
|
+
): Promise<AgenticaExecuteEvent<Model>> {
|
|
232
188
|
// ----
|
|
233
189
|
// HTTP PROTOCOL
|
|
234
190
|
// ----
|
|
@@ -243,10 +199,10 @@ async function propagateHttp<Model extends ILlmSchema.Model>(
|
|
|
243
199
|
operation: props.operation,
|
|
244
200
|
result: check,
|
|
245
201
|
}),
|
|
246
|
-
)
|
|
202
|
+
);
|
|
247
203
|
|
|
248
204
|
if (props.retry++ < (props.ctx.config?.retry ?? AgenticaConstant.RETRY)) {
|
|
249
|
-
const trial:
|
|
205
|
+
const trial: AgenticaExecuteEvent<Model> | null = await correct(
|
|
250
206
|
props.ctx,
|
|
251
207
|
props.call,
|
|
252
208
|
props.retry,
|
|
@@ -273,9 +229,8 @@ async function propagateHttp<Model extends ILlmSchema.Model>(
|
|
|
273
229
|
(success === false
|
|
274
230
|
? await correct(props.ctx, props.call, props.retry, response.body)
|
|
275
231
|
: null)
|
|
276
|
-
??
|
|
232
|
+
?? createExecuteEvent({
|
|
277
233
|
operation: props.call.operation,
|
|
278
|
-
id: props.call.id,
|
|
279
234
|
arguments: props.call.arguments,
|
|
280
235
|
value: response,
|
|
281
236
|
})
|
|
@@ -283,9 +238,8 @@ async function propagateHttp<Model extends ILlmSchema.Model>(
|
|
|
283
238
|
}
|
|
284
239
|
catch (error) {
|
|
285
240
|
// DISPATCH ERROR
|
|
286
|
-
return
|
|
241
|
+
return createExecuteEvent({
|
|
287
242
|
operation: props.call.operation,
|
|
288
|
-
id: props.call.id,
|
|
289
243
|
arguments: props.call.arguments,
|
|
290
244
|
value: {
|
|
291
245
|
status: 500,
|
|
@@ -308,7 +262,7 @@ async function propagateClass<Model extends ILlmSchema.Model>(props: {
|
|
|
308
262
|
operation: AgenticaOperation.Class<Model>;
|
|
309
263
|
call: AgenticaCallEvent<Model>;
|
|
310
264
|
retry: number;
|
|
311
|
-
}): Promise<
|
|
265
|
+
}): Promise<AgenticaExecuteEvent<Model>> {
|
|
312
266
|
// ----
|
|
313
267
|
// CLASS FUNCTION
|
|
314
268
|
// ----
|
|
@@ -323,13 +277,12 @@ async function propagateClass<Model extends ILlmSchema.Model>(props: {
|
|
|
323
277
|
operation: props.call.operation,
|
|
324
278
|
result: check,
|
|
325
279
|
}),
|
|
326
|
-
)
|
|
280
|
+
);
|
|
327
281
|
return (
|
|
328
282
|
(props.retry++ < (props.ctx.config?.retry ?? AgenticaConstant.RETRY)
|
|
329
283
|
? await correct(props.ctx, props.call, props.retry, check.errors)
|
|
330
284
|
: null)
|
|
331
|
-
??
|
|
332
|
-
id: props.call.id,
|
|
285
|
+
?? createExecuteEvent({
|
|
333
286
|
operation: props.call.operation,
|
|
334
287
|
arguments: props.call.arguments,
|
|
335
288
|
value: {
|
|
@@ -343,16 +296,14 @@ async function propagateClass<Model extends ILlmSchema.Model>(props: {
|
|
|
343
296
|
// EXECUTE FUNCTION
|
|
344
297
|
try {
|
|
345
298
|
const value = await executeClassOperation(props.operation, props.call.arguments);
|
|
346
|
-
return
|
|
347
|
-
id: props.call.id,
|
|
299
|
+
return createExecuteEvent({
|
|
348
300
|
operation: props.call.operation,
|
|
349
301
|
arguments: props.call.arguments,
|
|
350
302
|
value,
|
|
351
303
|
});
|
|
352
304
|
}
|
|
353
305
|
catch (error) {
|
|
354
|
-
return
|
|
355
|
-
id: props.call.id,
|
|
306
|
+
return createExecuteEvent({
|
|
356
307
|
operation: props.call.operation,
|
|
357
308
|
arguments: props.call.arguments,
|
|
358
309
|
value:
|
|
@@ -372,23 +323,21 @@ async function propagateMcp<Model extends ILlmSchema.Model>(props: {
|
|
|
372
323
|
operation: AgenticaOperation.Mcp<Model>;
|
|
373
324
|
call: AgenticaCallEvent<Model>;
|
|
374
325
|
retry: number;
|
|
375
|
-
}): Promise<
|
|
326
|
+
}): Promise<AgenticaExecuteEvent<Model>> {
|
|
376
327
|
// ----
|
|
377
328
|
// MCP PROTOCOL
|
|
378
329
|
// ----
|
|
379
330
|
// @TODO: implement argument validation logic
|
|
380
331
|
try {
|
|
381
332
|
const value = await executeMcpOperation(props.operation, props.call.arguments);
|
|
382
|
-
return
|
|
383
|
-
id: props.call.id,
|
|
333
|
+
return createExecuteEvent({
|
|
384
334
|
operation: props.call.operation,
|
|
385
335
|
arguments: props.call.arguments,
|
|
386
336
|
value,
|
|
387
337
|
});
|
|
388
338
|
}
|
|
389
339
|
catch (error) {
|
|
390
|
-
return
|
|
391
|
-
id: props.call.id,
|
|
340
|
+
return createExecuteEvent({
|
|
392
341
|
operation: props.call.operation,
|
|
393
342
|
arguments: props.call.arguments,
|
|
394
343
|
value:
|
|
@@ -454,7 +403,7 @@ async function correct<Model extends ILlmSchema.Model>(
|
|
|
454
403
|
call: AgenticaCallEvent<Model>,
|
|
455
404
|
retry: number,
|
|
456
405
|
error: unknown,
|
|
457
|
-
): Promise<
|
|
406
|
+
): Promise<AgenticaExecuteEvent<Model> | null> {
|
|
458
407
|
// ----
|
|
459
408
|
// EXECUTE CHATGPT API
|
|
460
409
|
// ----
|
|
@@ -549,6 +498,7 @@ async function correct<Model extends ILlmSchema.Model>(
|
|
|
549
498
|
|
|
550
499
|
const chunks = await StreamUtil.readAll(completionStream);
|
|
551
500
|
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
501
|
+
|
|
552
502
|
// ----
|
|
553
503
|
// PROCESS COMPLETION
|
|
554
504
|
// ----
|
|
@@ -3,24 +3,23 @@ import type OpenAI from "openai";
|
|
|
3
3
|
import type { IValidation } from "typia";
|
|
4
4
|
|
|
5
5
|
import typia from "typia";
|
|
6
|
-
import { v4 } from "uuid";
|
|
7
6
|
|
|
8
7
|
import type { AgenticaContext } from "../context/AgenticaContext";
|
|
9
8
|
import type { AgenticaOperation } from "../context/AgenticaOperation";
|
|
10
9
|
import type { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
|
|
11
10
|
import type { __IChatCancelFunctionsApplication } from "../context/internal/__IChatCancelFunctionsApplication";
|
|
12
11
|
import type { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
|
|
12
|
+
import type { AgenticaCancelEvent } from "../events/AgenticaCancelEvent";
|
|
13
13
|
import type { AgenticaEvent } from "../events/AgenticaEvent";
|
|
14
|
-
import type { AgenticaCancelHistory } from "../histories/AgenticaCancelHistory";
|
|
15
14
|
|
|
16
15
|
import { AgenticaConstant } from "../constants/AgenticaConstant";
|
|
17
16
|
import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
|
|
18
17
|
import { AgenticaSystemPrompt } from "../constants/AgenticaSystemPrompt";
|
|
19
|
-
import {
|
|
18
|
+
import { decodeHistory, decodeUserMessageContent } from "../factory/histories";
|
|
20
19
|
import { ChatGptCompletionMessageUtil } from "../utils/ChatGptCompletionMessageUtil";
|
|
21
20
|
import { StreamUtil } from "../utils/StreamUtil";
|
|
22
21
|
|
|
23
|
-
import {
|
|
22
|
+
import { cancelFunctionFromContext } from "./internal/cancelFunctionFromContext";
|
|
24
23
|
|
|
25
24
|
const CONTAINER: ILlmApplication<"chatgpt"> = typia.llm.application<
|
|
26
25
|
__IChatCancelFunctionsApplication,
|
|
@@ -35,7 +34,7 @@ interface IFailure {
|
|
|
35
34
|
|
|
36
35
|
export async function cancel<Model extends ILlmSchema.Model>(
|
|
37
36
|
ctx: AgenticaContext<Model>,
|
|
38
|
-
): Promise<
|
|
37
|
+
): Promise<void> {
|
|
39
38
|
if (ctx.operations.divided === undefined) {
|
|
40
39
|
return step(ctx, ctx.operations.array, 0);
|
|
41
40
|
}
|
|
@@ -43,14 +42,15 @@ export async function cancel<Model extends ILlmSchema.Model>(
|
|
|
43
42
|
const stacks: AgenticaOperationSelection<Model>[][]
|
|
44
43
|
= ctx.operations.divided.map(() => []);
|
|
45
44
|
const events: AgenticaEvent<Model>[] = [];
|
|
46
|
-
|
|
45
|
+
await Promise.all(
|
|
47
46
|
ctx.operations.divided.map(async (operations, i) =>
|
|
48
47
|
step(
|
|
49
48
|
{
|
|
50
49
|
...ctx,
|
|
51
50
|
stack: stacks[i]!,
|
|
52
|
-
dispatch:
|
|
51
|
+
dispatch: (e) => {
|
|
53
52
|
events.push(e);
|
|
53
|
+
return e;
|
|
54
54
|
},
|
|
55
55
|
},
|
|
56
56
|
operations,
|
|
@@ -59,12 +59,11 @@ export async function cancel<Model extends ILlmSchema.Model>(
|
|
|
59
59
|
),
|
|
60
60
|
);
|
|
61
61
|
|
|
62
|
-
// NO FUNCTION SELECTION, SO THAT ONLY TEXT LEFT
|
|
63
|
-
if (stacks.every(s => s.length === 0)) {
|
|
64
|
-
return prompts[0]!;
|
|
65
|
-
}
|
|
66
62
|
// ELITICISM
|
|
67
|
-
|
|
63
|
+
if (
|
|
64
|
+
(ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true
|
|
65
|
+
&& stacks.some(s => s.length !== 0)
|
|
66
|
+
) {
|
|
68
67
|
return step(
|
|
69
68
|
ctx,
|
|
70
69
|
stacks
|
|
@@ -78,22 +77,12 @@ export async function cancel<Model extends ILlmSchema.Model>(
|
|
|
78
77
|
0,
|
|
79
78
|
);
|
|
80
79
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
87
|
-
for (const e of events) {
|
|
88
|
-
if (e.type === "select") {
|
|
89
|
-
collection.selections.push(e.selection);
|
|
90
|
-
cancelFunction(ctx, {
|
|
91
|
-
name: e.selection.operation.name,
|
|
92
|
-
reason: e.selection.reason,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
80
|
+
else {
|
|
81
|
+
const cancelled: AgenticaCancelEvent<Model>[]
|
|
82
|
+
= events.filter(e => e.type === "cancel");
|
|
83
|
+
(cancelled.length !== 0 ? cancelled : events)
|
|
84
|
+
.forEach(ctx.dispatch);
|
|
95
85
|
}
|
|
96
|
-
return [collection];
|
|
97
86
|
}
|
|
98
87
|
|
|
99
88
|
async function step<Model extends ILlmSchema.Model>(
|
|
@@ -101,64 +90,64 @@ async function step<Model extends ILlmSchema.Model>(
|
|
|
101
90
|
operations: AgenticaOperation<Model>[],
|
|
102
91
|
retry: number,
|
|
103
92
|
failures?: IFailure[],
|
|
104
|
-
): Promise<
|
|
93
|
+
): Promise<void> {
|
|
105
94
|
// ----
|
|
106
95
|
// EXECUTE CHATGPT API
|
|
107
96
|
// ----
|
|
108
97
|
const completionStream = await ctx.request("cancel", {
|
|
109
98
|
messages: [
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
},
|
|
99
|
+
// COMMON SYSTEM PROMPT
|
|
100
|
+
{
|
|
101
|
+
role: "system",
|
|
102
|
+
content: AgenticaDefaultPrompt.write(ctx.config),
|
|
103
|
+
} satisfies OpenAI.ChatCompletionSystemMessageParam,
|
|
104
|
+
// CANDIDATE FUNCTIONS
|
|
105
|
+
{
|
|
106
|
+
role: "assistant",
|
|
107
|
+
tool_calls: [
|
|
108
|
+
{
|
|
109
|
+
type: "function",
|
|
110
|
+
id: "getApiFunctions",
|
|
111
|
+
function: {
|
|
112
|
+
name: "getApiFunctions",
|
|
113
|
+
arguments: JSON.stringify({}),
|
|
126
114
|
},
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
),
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
role: "tool",
|
|
120
|
+
tool_call_id: "getApiFunctions",
|
|
121
|
+
content: JSON.stringify(
|
|
122
|
+
operations.map(op => ({
|
|
123
|
+
name: op.name,
|
|
124
|
+
description: op.function.description,
|
|
125
|
+
...(op.protocol === "http"
|
|
126
|
+
? {
|
|
127
|
+
method: op.function.method,
|
|
128
|
+
path: op.function.path,
|
|
129
|
+
tags: op.function.tags,
|
|
130
|
+
}
|
|
131
|
+
: {}),
|
|
132
|
+
})),
|
|
133
|
+
),
|
|
134
|
+
},
|
|
135
|
+
// PREVIOUS HISTORIES
|
|
136
|
+
...ctx.histories.map(decodeHistory).flat(),
|
|
137
|
+
// USER INPUT
|
|
138
|
+
{
|
|
139
|
+
role: "user",
|
|
140
|
+
content: ctx.prompt.contents.map(decodeUserMessageContent),
|
|
141
|
+
},
|
|
142
|
+
// SYSTEM PROMPT
|
|
143
|
+
{
|
|
144
|
+
role: "system",
|
|
145
|
+
content:
|
|
146
|
+
ctx.config?.systemPrompt?.cancel?.(ctx.histories)
|
|
147
|
+
?? AgenticaSystemPrompt.CANCEL,
|
|
148
|
+
},
|
|
149
|
+
// TYPE CORRECTIONS
|
|
150
|
+
...emendMessages(failures ?? []),
|
|
162
151
|
],
|
|
163
152
|
// STACK FUNCTIONS
|
|
164
153
|
tools: [{
|
|
@@ -200,9 +189,9 @@ async function step<Model extends ILlmSchema.Model>(
|
|
|
200
189
|
continue;
|
|
201
190
|
}
|
|
202
191
|
|
|
203
|
-
const input = JSON.parse(tc.function.arguments) as object;
|
|
192
|
+
const input: object = JSON.parse(tc.function.arguments) as object;
|
|
204
193
|
const validation: IValidation<__IChatFunctionReference.IProps>
|
|
205
|
-
|
|
194
|
+
= typia.validate<__IChatFunctionReference.IProps>(input);
|
|
206
195
|
if (validation.success === false) {
|
|
207
196
|
failures.push({
|
|
208
197
|
id: tc.id,
|
|
@@ -220,7 +209,6 @@ async function step<Model extends ILlmSchema.Model>(
|
|
|
220
209
|
// ----
|
|
221
210
|
// PROCESS COMPLETION
|
|
222
211
|
// ----
|
|
223
|
-
const prompts: AgenticaCancelHistory<Model>[] = [];
|
|
224
212
|
for (const choice of completion.choices) {
|
|
225
213
|
// TOOL CALLING HANDLER
|
|
226
214
|
if (choice.message.tool_calls != null) {
|
|
@@ -228,35 +216,24 @@ async function step<Model extends ILlmSchema.Model>(
|
|
|
228
216
|
if (tc.type !== "function") {
|
|
229
217
|
continue;
|
|
230
218
|
}
|
|
231
|
-
|
|
232
|
-
if (tc.function.name !== "cancelFunctions") {
|
|
219
|
+
else if (tc.function.name !== "cancelFunctions") {
|
|
233
220
|
continue;
|
|
234
221
|
}
|
|
235
222
|
|
|
236
|
-
const input
|
|
223
|
+
const input: __IChatFunctionReference.IProps | null
|
|
224
|
+
= typia.json.isParse<
|
|
225
|
+
__IChatFunctionReference.IProps
|
|
226
|
+
>(tc.function.arguments);
|
|
237
227
|
if (input === null) {
|
|
238
228
|
continue;
|
|
239
229
|
}
|
|
240
230
|
|
|
241
|
-
const collection: AgenticaCancelHistory<Model> = createCancelHistory({
|
|
242
|
-
id: tc.id,
|
|
243
|
-
selections: [],
|
|
244
|
-
});
|
|
245
|
-
|
|
246
231
|
for (const reference of input.functions) {
|
|
247
|
-
|
|
248
|
-
if (operation !== null) {
|
|
249
|
-
collection.selections.push(operation);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (collection.selections.length !== 0) {
|
|
254
|
-
prompts.push(collection);
|
|
232
|
+
cancelFunctionFromContext(ctx, reference);
|
|
255
233
|
}
|
|
256
234
|
}
|
|
257
235
|
}
|
|
258
236
|
}
|
|
259
|
-
return prompts;
|
|
260
237
|
}
|
|
261
238
|
|
|
262
239
|
function emendMessages(failures: IFailure[]): OpenAI.ChatCompletionMessageParam[] {
|
|
@@ -3,13 +3,13 @@ import type OpenAI from "openai";
|
|
|
3
3
|
|
|
4
4
|
import type { AgenticaContext } from "../context/AgenticaContext";
|
|
5
5
|
import type { MicroAgenticaContext } from "../context/MicroAgenticaContext";
|
|
6
|
-
import type {
|
|
6
|
+
import type { AgenticaDescribeEvent } from "../events";
|
|
7
7
|
import type { AgenticaExecuteHistory } from "../histories/AgenticaExecuteHistory";
|
|
8
8
|
|
|
9
9
|
import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
|
|
10
10
|
import { AgenticaSystemPrompt } from "../constants/AgenticaSystemPrompt";
|
|
11
11
|
import { createDescribeEvent } from "../factory/events";
|
|
12
|
-
import {
|
|
12
|
+
import { decodeHistory } from "../factory/histories";
|
|
13
13
|
import { ChatGptCompletionMessageUtil } from "../utils/ChatGptCompletionMessageUtil";
|
|
14
14
|
import { MPSC } from "../utils/MPSC";
|
|
15
15
|
import { streamDefaultReaderToAsyncGenerator, StreamUtil } from "../utils/StreamUtil";
|
|
@@ -17,9 +17,9 @@ import { streamDefaultReaderToAsyncGenerator, StreamUtil } from "../utils/Stream
|
|
|
17
17
|
export async function describe<Model extends ILlmSchema.Model>(
|
|
18
18
|
ctx: AgenticaContext<Model> | MicroAgenticaContext<Model>,
|
|
19
19
|
histories: AgenticaExecuteHistory<Model>[],
|
|
20
|
-
): Promise<
|
|
20
|
+
): Promise<void> {
|
|
21
21
|
if (histories.length === 0) {
|
|
22
|
-
return
|
|
22
|
+
return;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
const completionStream = await ctx.request("describe", {
|
|
@@ -46,7 +46,7 @@ export async function describe<Model extends ILlmSchema.Model>(
|
|
|
46
46
|
mpsc: MPSC<string>;
|
|
47
47
|
})[] = [];
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
await StreamUtil.reduce<
|
|
50
50
|
OpenAI.ChatCompletionChunk,
|
|
51
51
|
Promise<OpenAI.ChatCompletion>
|
|
52
52
|
>(completionStream, async (accPromise, chunk) => {
|
|
@@ -82,18 +82,17 @@ export async function describe<Model extends ILlmSchema.Model>(
|
|
|
82
82
|
};
|
|
83
83
|
mpsc.produce(choice.delta.content);
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
).catch(() => {});
|
|
85
|
+
const event: AgenticaDescribeEvent<Model> = createDescribeEvent({
|
|
86
|
+
executes: histories,
|
|
87
|
+
stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
|
|
88
|
+
done: () => mpsc.done(),
|
|
89
|
+
get: () => describeContext[choice.index]?.content ?? "",
|
|
90
|
+
join: async () => {
|
|
91
|
+
await mpsc.waitClosed();
|
|
92
|
+
return describeContext[choice.index]!.content;
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
ctx.dispatch(event);
|
|
97
96
|
}
|
|
98
97
|
};
|
|
99
98
|
|
|
@@ -105,25 +104,6 @@ export async function describe<Model extends ILlmSchema.Model>(
|
|
|
105
104
|
registerContext(chunk.choices);
|
|
106
105
|
return ChatGptCompletionMessageUtil.accumulate(acc, chunk);
|
|
107
106
|
});
|
|
108
|
-
|
|
109
|
-
if (completion == null) {
|
|
110
|
-
throw new Error("No completion received");
|
|
111
|
-
}
|
|
112
|
-
const descriptions: AgenticaDescribeHistory<Model>[] = completion.choices
|
|
113
|
-
.map(choice =>
|
|
114
|
-
choice.message.role === "assistant"
|
|
115
|
-
? choice.message.content
|
|
116
|
-
: null,
|
|
117
|
-
)
|
|
118
|
-
.filter(str => str !== null)
|
|
119
|
-
.map(
|
|
120
|
-
content =>
|
|
121
|
-
createDescribeHistory({
|
|
122
|
-
executes: histories,
|
|
123
|
-
text: content,
|
|
124
|
-
}),
|
|
125
|
-
);
|
|
126
|
-
return descriptions;
|
|
127
107
|
}
|
|
128
108
|
|
|
129
109
|
export const ChatGptDescribeFunctionAgent = {
|