@agentica/core 0.12.21 → 0.13.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/README.md +50 -47
- package/lib/Agentica.d.ts +9 -9
- package/lib/Agentica.js +23 -16
- package/lib/Agentica.js.map +1 -1
- package/lib/chatgpt/ChatGptAgent.d.ts +7 -7
- package/lib/chatgpt/ChatGptAgent.js +17 -12
- package/lib/chatgpt/ChatGptAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +7 -6
- package/lib/chatgpt/ChatGptCallFunctionAgent.js +120 -81
- package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +9 -7
- package/lib/chatgpt/ChatGptCancelFunctionAgent.js +177 -145
- package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptCompletionMessageUtil.d.ts +13 -8
- package/lib/chatgpt/ChatGptCompletionMessageUtil.js +499 -489
- package/lib/chatgpt/ChatGptCompletionMessageUtil.js.map +1 -1
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +7 -6
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +33 -32
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +7 -6
- package/lib/chatgpt/ChatGptHistoryDecoder.js +60 -56
- package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +7 -6
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +121 -116
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +20 -5
- package/lib/chatgpt/ChatGptSelectFunctionAgent.js +183 -150
- package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptUsageAggregator.d.ts +9 -6
- package/lib/chatgpt/ChatGptUsageAggregator.js +46 -41
- package/lib/chatgpt/ChatGptUsageAggregator.js.map +1 -1
- package/lib/context/AgenticaCancelPrompt.d.ts +3 -3
- package/lib/context/AgenticaCancelPrompt.js +1 -1
- package/lib/context/AgenticaCancelPrompt.js.map +1 -1
- package/lib/context/AgenticaClassOperation.d.ts +1 -1
- package/lib/context/AgenticaClassOperation.js.map +1 -1
- package/lib/context/AgenticaContext.d.ts +9 -9
- package/lib/context/AgenticaHttpOperation.d.ts +1 -1
- package/lib/context/AgenticaHttpOperation.js.map +1 -1
- package/lib/context/AgenticaOperation.d.ts +4 -4
- package/lib/context/AgenticaOperationBase.d.ts +1 -1
- package/lib/context/AgenticaOperationCollection.d.ts +2 -2
- package/lib/context/AgenticaOperationSelection.d.ts +3 -3
- package/lib/context/AgenticaTokenUsage.d.ts +1 -1
- package/lib/context/AgenticaTokenUsage.js.map +1 -1
- package/lib/context/internal/AgenticaTokenUsageAggregator.d.ts +11 -10
- package/lib/context/internal/AgenticaTokenUsageAggregator.js +42 -43
- package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -1
- package/lib/context/internal/__IChatCancelFunctionsApplication.d.ts +2 -2
- package/lib/context/internal/__IChatInitialApplication.d.ts +2 -2
- package/lib/context/internal/__IChatSelectFunctionsApplication.d.ts +2 -2
- package/lib/events/AgenticaCallEvent.d.ts +3 -3
- package/lib/events/AgenticaCancelEvent.d.ts +3 -3
- package/lib/events/AgenticaDescribeEvent.d.ts +3 -3
- package/lib/events/AgenticaDescribeEvent.js +13 -2
- package/lib/events/AgenticaDescribeEvent.js.map +1 -1
- package/lib/events/AgenticaEvent.d.ts +12 -12
- package/lib/events/AgenticaExecuteEvent.d.ts +7 -7
- package/lib/events/AgenticaInitializeEvent.d.ts +1 -1
- package/lib/events/AgenticaRequestEvent.d.ts +3 -3
- package/lib/events/AgenticaRequestEvent.js.map +1 -1
- package/lib/events/AgenticaResponseEvent.d.ts +2 -2
- package/lib/events/AgenticaResponseEvent.js.map +1 -1
- package/lib/events/AgenticaSelectEvent.d.ts +3 -3
- package/lib/events/AgenticaSelectEvent.js.map +1 -1
- package/lib/events/AgenticaTextEvent.d.ts +1 -1
- package/lib/events/AgenticaTextEvent.js +12 -1
- package/lib/events/AgenticaTextEvent.js.map +1 -1
- package/lib/functional/assertHttpLlmApplication.d.ts +4 -3
- package/lib/functional/assertHttpLlmApplication.js +9339 -9338
- package/lib/functional/assertHttpLlmApplication.js.map +1 -1
- package/lib/functional/validateHttpLlmApplication.d.ts +5 -4
- package/lib/functional/validateHttpLlmApplication.js +19 -19
- package/lib/functional/validateHttpLlmApplication.js.map +1 -1
- package/lib/index.d.ts +19 -19
- package/lib/index.js +21 -21
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +19210 -18997
- package/lib/index.mjs.map +1 -1
- package/lib/internal/AgenticaDefaultPrompt.d.ts +6 -5
- package/lib/internal/AgenticaDefaultPrompt.js +39 -19
- package/lib/internal/AgenticaDefaultPrompt.js.map +1 -1
- package/lib/internal/AgenticaOperationComposer.d.ts +11 -10
- package/lib/internal/AgenticaOperationComposer.js +60 -59
- package/lib/internal/AgenticaOperationComposer.js.map +1 -1
- package/lib/internal/AgenticaSystemPrompt.d.ts +8 -8
- package/lib/internal/AgenticaSystemPrompt.js +9 -9
- package/lib/internal/AgenticaSystemPrompt.js.map +1 -1
- package/lib/internal/AsyncQueue.d.ts +15 -0
- package/lib/internal/AsyncQueue.js +82 -0
- package/lib/internal/AsyncQueue.js.map +1 -0
- package/lib/internal/ByteArrayUtil.d.ts +1 -1
- package/lib/internal/ByteArrayUtil.js +3 -2
- package/lib/internal/ByteArrayUtil.js.map +1 -1
- package/lib/internal/MPSC.d.ts +10 -0
- package/lib/internal/MPSC.js +49 -0
- package/lib/internal/MPSC.js.map +1 -0
- package/lib/internal/Singleton.js +6 -5
- package/lib/internal/Singleton.js.map +1 -1
- package/lib/internal/StreamUtil.d.ts +16 -6
- package/lib/internal/StreamUtil.js +43 -29
- package/lib/internal/StreamUtil.js.map +1 -1
- package/lib/internal/__map_take.js +5 -5
- package/lib/internal/__map_take.js.map +1 -1
- package/lib/json/IAgenticaEventJson.d.ts +7 -7
- package/lib/json/IAgenticaOperationSelectionJson.d.ts +1 -1
- package/lib/json/IAgenticaPromptJson.d.ts +2 -2
- package/lib/prompts/AgenticaCancelPrompt.d.ts +3 -3
- package/lib/prompts/AgenticaCancelPrompt.js +1 -1
- package/lib/prompts/AgenticaCancelPrompt.js.map +1 -1
- package/lib/prompts/AgenticaDescribePrompt.d.ts +3 -3
- package/lib/prompts/AgenticaDescribePrompt.js +1 -1
- package/lib/prompts/AgenticaDescribePrompt.js.map +1 -1
- package/lib/prompts/AgenticaExecutePrompt.d.ts +5 -5
- package/lib/prompts/AgenticaExecutePrompt.js.map +1 -1
- package/lib/prompts/AgenticaPrompt.d.ts +6 -6
- package/lib/prompts/AgenticaSelectPrompt.d.ts +3 -3
- package/lib/prompts/AgenticaSelectPrompt.js +1 -1
- package/lib/prompts/AgenticaSelectPrompt.js.map +1 -1
- package/lib/prompts/AgenticaTextPrompt.d.ts +1 -1
- package/lib/structures/IAgenticaConfig.d.ts +5 -5
- package/lib/structures/IAgenticaController.d.ts +1 -1
- package/lib/structures/IAgenticaExecutor.d.ts +4 -4
- package/lib/structures/IAgenticaProps.d.ts +5 -5
- package/lib/structures/IAgenticaSystemPrompt.d.ts +4 -4
- package/lib/structures/IAgenticaVendor.d.ts +1 -1
- package/lib/transformers/AgenticaEventTransformer.d.ts +55 -37
- package/lib/transformers/AgenticaEventTransformer.js +105 -58
- package/lib/transformers/AgenticaEventTransformer.js.map +1 -1
- package/lib/transformers/AgenticaPromptTransformer.d.ts +35 -29
- package/lib/transformers/AgenticaPromptTransformer.js +74 -45
- package/lib/transformers/AgenticaPromptTransformer.js.map +1 -1
- package/package.json +20 -23
- package/prompts/cancel.md +1 -1
- package/prompts/common.md +1 -1
- package/prompts/describe.md +1 -1
- package/prompts/execute.md +1 -1
- package/prompts/initialize.md +1 -1
- package/prompts/select.md +1 -1
- package/src/Agentica.ts +41 -31
- package/src/chatgpt/ChatGptAgent.ts +63 -57
- package/src/chatgpt/ChatGptCallFunctionAgent.ts +361 -337
- package/src/chatgpt/ChatGptCancelFunctionAgent.ts +188 -164
- package/src/chatgpt/ChatGptCompletionMessageUtil.ts +136 -137
- package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +105 -101
- package/src/chatgpt/ChatGptHistoryDecoder.ts +69 -65
- package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +128 -123
- package/src/chatgpt/ChatGptSelectFunctionAgent.ts +215 -189
- package/src/chatgpt/ChatGptUsageAggregator.ts +49 -54
- package/src/context/AgenticaCancelPrompt.ts +4 -4
- package/src/context/AgenticaClassOperation.ts +5 -5
- package/src/context/AgenticaContext.ts +15 -15
- package/src/context/AgenticaHttpOperation.ts +5 -5
- package/src/context/AgenticaOperation.ts +4 -4
- package/src/context/AgenticaOperationBase.ts +1 -1
- package/src/context/AgenticaOperationCollection.ts +2 -2
- package/src/context/AgenticaOperationSelection.ts +3 -3
- package/src/context/AgenticaTokenUsage.ts +4 -3
- package/src/context/internal/AgenticaTokenUsageAggregator.ts +59 -59
- package/src/context/internal/__IChatCancelFunctionsApplication.ts +2 -2
- package/src/context/internal/__IChatInitialApplication.ts +2 -2
- package/src/context/internal/__IChatSelectFunctionsApplication.ts +2 -2
- package/src/events/AgenticaCallEvent.ts +3 -3
- package/src/events/AgenticaCancelEvent.ts +3 -3
- package/src/events/AgenticaDescribeEvent.ts +5 -5
- package/src/events/AgenticaEvent.ts +12 -12
- package/src/events/AgenticaExecuteEvent.ts +7 -7
- package/src/events/AgenticaInitializeEvent.ts +1 -1
- package/src/events/AgenticaRequestEvent.ts +3 -3
- package/src/events/AgenticaResponseEvent.ts +2 -2
- package/src/events/AgenticaSelectEvent.ts +4 -4
- package/src/events/AgenticaTextEvent.ts +2 -2
- package/src/functional/assertHttpLlmApplication.ts +9 -6
- package/src/functional/validateHttpLlmApplication.ts +14 -8
- package/src/index.ts +24 -24
- package/src/internal/AgenticaDefaultPrompt.ts +42 -28
- package/src/internal/AgenticaOperationComposer.ts +63 -61
- package/src/internal/AgenticaSystemPrompt.ts +15 -14
- package/src/internal/AsyncQueue.ts +67 -0
- package/src/internal/ByteArrayUtil.ts +2 -2
- package/src/internal/MPSC.ts +40 -0
- package/src/internal/Singleton.ts +8 -6
- package/src/internal/StreamUtil.ts +62 -53
- package/src/internal/__map_take.ts +5 -7
- package/src/json/IAgenticaEventJson.ts +7 -8
- package/src/json/IAgenticaOperationSelectionJson.ts +1 -1
- package/src/json/IAgenticaPromptJson.ts +2 -2
- package/src/prompts/AgenticaCancelPrompt.ts +4 -4
- package/src/prompts/AgenticaDescribePrompt.ts +4 -4
- package/src/prompts/AgenticaExecutePrompt.ts +6 -5
- package/src/prompts/AgenticaPrompt.ts +6 -6
- package/src/prompts/AgenticaSelectPrompt.ts +4 -4
- package/src/prompts/AgenticaTextPrompt.ts +1 -1
- package/src/structures/IAgenticaConfig.ts +5 -5
- package/src/structures/IAgenticaController.ts +14 -14
- package/src/structures/IAgenticaExecutor.ts +4 -4
- package/src/structures/IAgenticaProps.ts +5 -5
- package/src/structures/IAgenticaSystemPrompt.ts +4 -4
- package/src/structures/IAgenticaVendor.ts +1 -1
- package/src/transformers/AgenticaEventTransformer.ts +169 -136
- package/src/transformers/AgenticaPromptTransformer.ts +125 -103
- package/lib/internal/MPSCUtil.d.ts +0 -35
- package/lib/internal/MPSCUtil.js +0 -105
- package/lib/internal/MPSCUtil.js.map +0 -1
- package/src/internal/MPSCUtil.ts +0 -111
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { ILlmApplication, ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
import OpenAI from "openai";
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
1
|
+
import type { ILlmApplication, ILlmSchema } from "@samchon/openapi";
|
|
2
|
+
import type OpenAI from "openai";
|
|
3
|
+
import type { IValidation } from "typia";
|
|
4
|
+
import type { AgenticaContext } from "../context/AgenticaContext";
|
|
5
|
+
import type { AgenticaOperation } from "../context/AgenticaOperation";
|
|
5
6
|
|
|
7
|
+
import type { __IChatCancelFunctionsApplication } from "../context/internal/__IChatCancelFunctionsApplication";
|
|
8
|
+
import type { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
|
|
9
|
+
import type { AgenticaEvent } from "../events/AgenticaEvent";
|
|
10
|
+
import typia from "typia";
|
|
11
|
+
import { v4 } from "uuid";
|
|
6
12
|
import { AgenticaCancelPrompt } from "../context/AgenticaCancelPrompt";
|
|
7
|
-
import { AgenticaContext } from "../context/AgenticaContext";
|
|
8
|
-
import { AgenticaOperation } from "../context/AgenticaOperation";
|
|
9
13
|
import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
|
|
10
|
-
import { __IChatCancelFunctionsApplication } from "../context/internal/__IChatCancelFunctionsApplication";
|
|
11
|
-
import { __IChatFunctionReference } from "../context/internal/__IChatFunctionReference";
|
|
12
14
|
import { AgenticaCancelEvent } from "../events/AgenticaCancelEvent";
|
|
13
|
-
import { AgenticaEvent } from "../events/AgenticaEvent";
|
|
14
15
|
import { AgenticaConstant } from "../internal/AgenticaConstant";
|
|
15
16
|
import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
|
|
16
17
|
import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
|
|
@@ -18,98 +19,105 @@ import { StreamUtil } from "../internal/StreamUtil";
|
|
|
18
19
|
import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
|
|
19
20
|
import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (ctx.operations.divided === undefined)
|
|
26
|
-
return step(ctx, ctx.operations.array, 0);
|
|
22
|
+
const CONTAINER: ILlmApplication<"chatgpt"> = typia.llm.application<
|
|
23
|
+
__IChatCancelFunctionsApplication,
|
|
24
|
+
"chatgpt"
|
|
25
|
+
>();
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
interface IFailure {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
validation: IValidation.IFailure;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function execute<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>): Promise<AgenticaCancelPrompt<Model>[]> {
|
|
34
|
+
if (ctx.operations.divided === undefined) {
|
|
35
|
+
return step(ctx, ctx.operations.array, 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const stacks: AgenticaOperationSelection<Model>[][]
|
|
39
|
+
= ctx.operations.divided.map(() => []);
|
|
40
|
+
const events: AgenticaEvent<Model>[] = [];
|
|
41
|
+
const prompts: AgenticaCancelPrompt<Model>[][] = await Promise.all(
|
|
42
|
+
ctx.operations.divided.map(async (operations, i) =>
|
|
43
|
+
step(
|
|
44
|
+
{
|
|
45
|
+
...ctx,
|
|
46
|
+
stack: stacks[i]!,
|
|
47
|
+
dispatch: async (e) => {
|
|
48
|
+
events.push(e);
|
|
40
49
|
},
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
},
|
|
51
|
+
operations,
|
|
52
|
+
0,
|
|
44
53
|
),
|
|
45
|
-
)
|
|
54
|
+
),
|
|
55
|
+
);
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
// NO FUNCTION SELECTION, SO THAT ONLY TEXT LEFT
|
|
58
|
+
if (stacks.every(s => s.length === 0)) {
|
|
59
|
+
return prompts[0]!;
|
|
60
|
+
}
|
|
61
|
+
// ELITICISM
|
|
62
|
+
else if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true) {
|
|
63
|
+
return step(
|
|
64
|
+
ctx,
|
|
65
|
+
stacks
|
|
66
|
+
.flat()
|
|
67
|
+
.map(
|
|
68
|
+
s =>
|
|
69
|
+
ctx.operations.group
|
|
70
|
+
.get(s.operation.controller.name)!
|
|
71
|
+
.get(s.operation.function.name)!,
|
|
72
|
+
),
|
|
73
|
+
0,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
63
76
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
// RE-COLLECT SELECT FUNCTION EVENTS
|
|
78
|
+
const collection: AgenticaCancelPrompt<Model> = new AgenticaCancelPrompt({
|
|
79
|
+
id: v4(),
|
|
80
|
+
selections: [],
|
|
81
|
+
});
|
|
82
|
+
for (const e of events) {
|
|
83
|
+
if (e.type === "select") {
|
|
84
|
+
collection.selections.push(e.selection);
|
|
85
|
+
await cancelFunction(ctx, {
|
|
86
|
+
name: e.selection.operation.name,
|
|
87
|
+
reason: e.selection.reason,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return [collection];
|
|
92
|
+
}
|
|
79
93
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
reference
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (index === -1) return null;
|
|
94
|
+
export async function cancelFunction<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>, reference: __IChatFunctionReference): Promise<AgenticaOperationSelection<Model> | null> {
|
|
95
|
+
const index: number = ctx.stack.findIndex(
|
|
96
|
+
item => item.operation.name === reference.name,
|
|
97
|
+
);
|
|
98
|
+
if (index === -1) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
88
101
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}),
|
|
102
|
+
const item: AgenticaOperationSelection<Model> = ctx.stack[index]!;
|
|
103
|
+
ctx.stack.splice(index, 1);
|
|
104
|
+
await ctx.dispatch(
|
|
105
|
+
new AgenticaCancelEvent({
|
|
106
|
+
selection: new AgenticaOperationSelection({
|
|
107
|
+
operation: item.operation,
|
|
108
|
+
reason: reference.reason,
|
|
97
109
|
}),
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
return item;
|
|
113
|
+
}
|
|
101
114
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
//----
|
|
109
|
-
// EXECUTE CHATGPT API
|
|
110
|
-
//----
|
|
111
|
-
const completionStream = await ctx.request("cancel", {
|
|
112
|
-
messages: [
|
|
115
|
+
async function step<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>, operations: AgenticaOperation<Model>[], retry: number, failures?: IFailure[]): Promise<AgenticaCancelPrompt<Model>[]> {
|
|
116
|
+
// ----
|
|
117
|
+
// EXECUTE CHATGPT API
|
|
118
|
+
// ----
|
|
119
|
+
const completionStream = await ctx.request("cancel", {
|
|
120
|
+
messages: [
|
|
113
121
|
// COMMON SYSTEM PROMPT
|
|
114
122
|
{
|
|
115
123
|
role: "system",
|
|
@@ -133,7 +141,7 @@ export namespace ChatGptCancelFunctionAgent {
|
|
|
133
141
|
role: "tool",
|
|
134
142
|
tool_call_id: "getApiFunctions",
|
|
135
143
|
content: JSON.stringify(
|
|
136
|
-
operations.map(
|
|
144
|
+
operations.map(op => ({
|
|
137
145
|
name: op.name,
|
|
138
146
|
description: op.function.description,
|
|
139
147
|
...(op.protocol === "http"
|
|
@@ -157,87 +165,109 @@ export namespace ChatGptCancelFunctionAgent {
|
|
|
157
165
|
{
|
|
158
166
|
role: "system",
|
|
159
167
|
content:
|
|
160
|
-
ctx.config?.systemPrompt?.cancel?.(ctx.histories)
|
|
161
|
-
AgenticaSystemPrompt.CANCEL,
|
|
168
|
+
ctx.config?.systemPrompt?.cancel?.(ctx.histories)
|
|
169
|
+
?? AgenticaSystemPrompt.CANCEL,
|
|
162
170
|
},
|
|
163
171
|
// TYPE CORRECTIONS
|
|
164
172
|
...emendMessages(failures ?? []),
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
173
|
+
],
|
|
174
|
+
// STACK FUNCTIONS
|
|
175
|
+
tools: CONTAINER.functions.map(
|
|
176
|
+
func =>
|
|
169
177
|
({
|
|
170
178
|
type: "function",
|
|
171
179
|
function: {
|
|
172
180
|
name: func.name,
|
|
173
181
|
description: func.description,
|
|
174
|
-
|
|
182
|
+
/**
|
|
183
|
+
* @TODO fix it
|
|
184
|
+
* The property and value have a type mismatch, but it works.
|
|
185
|
+
*/
|
|
186
|
+
parameters: func.parameters as unknown as Record<string, unknown>,
|
|
175
187
|
},
|
|
176
188
|
}) satisfies OpenAI.ChatCompletionTool,
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
189
|
+
),
|
|
190
|
+
tool_choice: "auto",
|
|
191
|
+
parallel_tool_calls: true,
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const chunks = await StreamUtil.readAll(completionStream);
|
|
195
|
+
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
181
196
|
|
|
182
|
-
|
|
183
|
-
|
|
197
|
+
// ----
|
|
198
|
+
// VALIDATION
|
|
199
|
+
// ----
|
|
200
|
+
if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
|
|
201
|
+
const failures: IFailure[] = [];
|
|
202
|
+
for (const choice of completion.choices) {
|
|
203
|
+
for (const tc of choice.message.tool_calls ?? []) {
|
|
204
|
+
if (tc.function.name !== "cancelFunctions") {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
184
207
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const validation: IValidation<__IChatFunctionReference.IProps> =
|
|
195
|
-
typia.validate<__IChatFunctionReference.IProps>(input);
|
|
196
|
-
if (validation.success === false)
|
|
197
|
-
failures.push({
|
|
198
|
-
id: tc.id,
|
|
199
|
-
name: tc.function.name,
|
|
200
|
-
validation,
|
|
201
|
-
});
|
|
208
|
+
const input = JSON.parse(tc.function.arguments) as object;
|
|
209
|
+
const validation: IValidation<__IChatFunctionReference.IProps>
|
|
210
|
+
= typia.validate<__IChatFunctionReference.IProps>(input);
|
|
211
|
+
if (validation.success === false) {
|
|
212
|
+
failures.push({
|
|
213
|
+
id: tc.id,
|
|
214
|
+
name: tc.function.name,
|
|
215
|
+
validation,
|
|
216
|
+
});
|
|
202
217
|
}
|
|
203
|
-
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (failures.length > 0) {
|
|
221
|
+
return step(ctx, operations, retry, failures);
|
|
204
222
|
}
|
|
223
|
+
}
|
|
205
224
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
225
|
+
// ----
|
|
226
|
+
// PROCESS COMPLETION
|
|
227
|
+
// ----
|
|
228
|
+
const prompts: AgenticaCancelPrompt<Model>[] = [];
|
|
229
|
+
for (const choice of completion.choices) {
|
|
230
|
+
// TOOL CALLING HANDLER
|
|
231
|
+
if (choice.message.tool_calls != null) {
|
|
232
|
+
for (const tc of choice.message.tool_calls) {
|
|
233
|
+
if (tc.type !== "function") {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (tc.function.name !== "cancelFunctions") {
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const input = typia.json.isParse<__IChatFunctionReference.IProps>(tc.function.arguments);
|
|
242
|
+
if (input === null) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const collection: AgenticaCancelPrompt<Model>
|
|
247
|
+
= new AgenticaCancelPrompt({
|
|
222
248
|
id: tc.id,
|
|
223
249
|
selections: [],
|
|
224
250
|
});
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
251
|
+
|
|
252
|
+
for (const reference of input.functions) {
|
|
253
|
+
const operation = await cancelFunction(ctx, reference);
|
|
254
|
+
if (operation !== null) {
|
|
255
|
+
collection.selections.push(operation);
|
|
230
256
|
}
|
|
231
257
|
}
|
|
258
|
+
|
|
259
|
+
if (collection.selections.length !== 0) {
|
|
260
|
+
prompts.push(collection);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
232
263
|
}
|
|
233
|
-
|
|
234
|
-
|
|
264
|
+
}
|
|
265
|
+
return prompts;
|
|
266
|
+
}
|
|
235
267
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
failures
|
|
240
|
-
.map((f) => [
|
|
268
|
+
function emendMessages(failures: IFailure[]): OpenAI.ChatCompletionMessageParam[] {
|
|
269
|
+
return failures
|
|
270
|
+
.map(f => [
|
|
241
271
|
{
|
|
242
272
|
role: "assistant",
|
|
243
273
|
tool_calls: [
|
|
@@ -264,17 +294,11 @@ export namespace ChatGptCancelFunctionAgent {
|
|
|
264
294
|
"Correct it at the next function calling.",
|
|
265
295
|
].join("\n"),
|
|
266
296
|
} satisfies OpenAI.ChatCompletionSystemMessageParam,
|
|
267
|
-
|
|
268
|
-
|
|
297
|
+
])
|
|
298
|
+
.flat();
|
|
269
299
|
}
|
|
270
300
|
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
interface IFailure {
|
|
277
|
-
id: string;
|
|
278
|
-
name: string;
|
|
279
|
-
validation: IValidation.IFailure;
|
|
280
|
-
}
|
|
301
|
+
export const ChatGptCancelFunctionAgent = {
|
|
302
|
+
execute,
|
|
303
|
+
cancelFunction,
|
|
304
|
+
};
|