@agentica/core 0.10.3 → 0.11.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 +2 -2
- package/lib/Agentica.d.ts +14 -14
- package/lib/Agentica.js +54 -29
- package/lib/Agentica.js.map +1 -1
- package/lib/chatgpt/ChatGptAgent.d.ts +3 -3
- package/lib/chatgpt/ChatGptAgent.js +1 -1
- package/lib/chatgpt/ChatGptAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +3 -3
- package/lib/chatgpt/ChatGptCallFunctionAgent.js +61 -63
- package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +6 -6
- package/lib/chatgpt/ChatGptCancelFunctionAgent.js +28 -30
- package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptCompletionMessageUtil.d.ts +8 -0
- package/lib/chatgpt/ChatGptCompletionMessageUtil.js +536 -0
- package/lib/chatgpt/ChatGptCompletionMessageUtil.js.map +1 -0
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +4 -3
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +56 -6
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +2 -2
- package/lib/chatgpt/ChatGptHistoryDecoder.js +8 -8
- package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +3 -3
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +11 -5
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +3 -3
- package/lib/chatgpt/ChatGptSelectFunctionAgent.js +39 -42
- package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
- package/lib/chatgpt/ChatGptUsageAggregator.d.ts +6 -0
- package/lib/chatgpt/ChatGptUsageAggregator.js +48 -0
- package/lib/chatgpt/ChatGptUsageAggregator.js.map +1 -0
- package/lib/context/AgenticaCancelPrompt.d.ts +16 -0
- package/lib/context/AgenticaCancelPrompt.js +20 -0
- package/lib/context/AgenticaCancelPrompt.js.map +1 -0
- package/lib/context/AgenticaClassOperation.d.ts +8 -0
- package/lib/context/AgenticaClassOperation.js +11 -0
- package/lib/context/AgenticaClassOperation.js.map +1 -0
- package/lib/{structures/IAgenticaContext.d.ts → context/AgenticaContext.d.ts} +15 -14
- package/lib/{structures/IAgenticaPrompt.js → context/AgenticaContext.js} +1 -1
- package/lib/context/AgenticaContext.js.map +1 -0
- package/lib/context/AgenticaHttpOperation.d.ts +8 -0
- package/lib/context/AgenticaHttpOperation.js +11 -0
- package/lib/context/AgenticaHttpOperation.js.map +1 -0
- package/lib/context/AgenticaOperation.d.ts +46 -0
- package/lib/{structures/IAgenticaContext.js → context/AgenticaOperation.js} +1 -1
- package/lib/context/AgenticaOperation.js.map +1 -0
- package/lib/context/AgenticaOperationBase.d.ts +29 -0
- package/lib/context/AgenticaOperationBase.js +21 -0
- package/lib/context/AgenticaOperationBase.js.map +1 -0
- package/lib/{structures/IAgenticaOperationCollection.d.ts → context/AgenticaOperationCollection.d.ts} +6 -6
- package/lib/{structures/IAgenticaOperationSelection.js → context/AgenticaOperationCollection.js} +1 -1
- package/lib/context/AgenticaOperationCollection.js.map +1 -0
- package/lib/context/AgenticaOperationSelection.d.ts +15 -0
- package/lib/context/AgenticaOperationSelection.js +17 -0
- package/lib/context/AgenticaOperationSelection.js.map +1 -0
- package/lib/context/AgenticaTokenUsage.d.ts +82 -0
- package/lib/context/AgenticaTokenUsage.js +97 -0
- package/lib/context/AgenticaTokenUsage.js.map +1 -0
- package/lib/context/internal/AgenticaTokenUsageAggregator.d.ts +10 -0
- package/lib/context/internal/AgenticaTokenUsageAggregator.js +47 -0
- package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -0
- package/lib/context/internal/__IChatCancelFunctionsApplication.js.map +1 -0
- package/lib/context/internal/__IChatFunctionReference.js.map +1 -0
- package/lib/context/internal/__IChatInitialApplication.js.map +1 -0
- package/lib/context/internal/__IChatSelectFunctionsApplication.js.map +1 -0
- package/lib/events/AgenticaCallEvent.d.ts +18 -0
- package/lib/events/AgenticaCallEvent.js +22 -0
- package/lib/events/AgenticaCallEvent.js.map +1 -0
- package/lib/events/AgenticaCancelEvent.d.ts +14 -0
- package/lib/events/AgenticaCancelEvent.js +18 -0
- package/lib/events/AgenticaCancelEvent.js.map +1 -0
- package/lib/events/AgenticaDescribeEvent.d.ts +27 -0
- package/lib/events/AgenticaDescribeEvent.js +40 -0
- package/lib/events/AgenticaDescribeEvent.js.map +1 -0
- package/lib/events/AgenticaEvent.d.ts +25 -0
- package/lib/{structures/IAgenticaEvent.js → events/AgenticaEvent.js} +1 -1
- package/lib/events/AgenticaEvent.js.map +1 -0
- package/lib/events/AgenticaEventBase.d.ts +4 -0
- package/lib/events/AgenticaEventBase.js +10 -0
- package/lib/events/AgenticaEventBase.js.map +1 -0
- package/lib/events/AgenticaEventSource.d.ts +1 -0
- package/lib/events/AgenticaEventSource.js +3 -0
- package/lib/events/AgenticaEventSource.js.map +1 -0
- package/lib/events/AgenticaExecuteEvent.d.ts +22 -0
- package/lib/events/AgenticaExecuteEvent.js +33 -0
- package/lib/events/AgenticaExecuteEvent.js.map +1 -0
- package/lib/events/AgenticaInitializeEvent.d.ts +6 -0
- package/lib/events/AgenticaInitializeEvent.js +16 -0
- package/lib/events/AgenticaInitializeEvent.js.map +1 -0
- package/lib/events/AgenticaRequestEvent.d.ts +27 -0
- package/lib/events/AgenticaRequestEvent.js +22 -0
- package/lib/events/AgenticaRequestEvent.js.map +1 -0
- package/lib/events/AgenticaResponseEvent.d.ts +35 -0
- package/lib/events/AgenticaResponseEvent.js +16 -0
- package/lib/events/AgenticaResponseEvent.js.map +1 -0
- package/lib/events/AgenticaSelectEvent.d.ts +16 -0
- package/lib/events/AgenticaSelectEvent.js +26 -0
- package/lib/events/AgenticaSelectEvent.js.map +1 -0
- package/lib/events/AgenticaTextEvent.d.ts +25 -0
- package/lib/events/AgenticaTextEvent.js +40 -0
- package/lib/events/AgenticaTextEvent.js.map +1 -0
- package/lib/index.d.ts +26 -8
- package/lib/index.js +29 -9
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +1391 -304
- package/lib/index.mjs.map +1 -1
- package/lib/internal/AgenticaOperationComposer.d.ts +2 -2
- package/lib/internal/AgenticaOperationComposer.js +12 -0
- package/lib/internal/AgenticaOperationComposer.js.map +1 -1
- package/lib/internal/ByteArrayUtil.d.ts +3 -0
- package/lib/internal/ByteArrayUtil.js +10 -0
- package/lib/internal/ByteArrayUtil.js.map +1 -0
- package/lib/internal/MPSCUtil.d.ts +21 -0
- package/lib/internal/MPSCUtil.js +84 -0
- package/lib/internal/MPSCUtil.js.map +1 -0
- package/lib/internal/StreamUtil.d.ts +6 -0
- package/lib/internal/StreamUtil.js +65 -0
- package/lib/internal/StreamUtil.js.map +1 -0
- package/lib/json/IAgenticaEventJson.d.ts +149 -0
- package/lib/{structures/IAgenticaOperation.js → json/IAgenticaEventJson.js} +1 -1
- package/lib/json/IAgenticaEventJson.js.map +1 -0
- package/lib/json/IAgenticaOperationJson.d.ts +33 -0
- package/lib/json/IAgenticaOperationJson.js +3 -0
- package/lib/json/IAgenticaOperationJson.js.map +1 -0
- package/lib/json/IAgenticaOperationSelectionJson.d.ts +18 -0
- package/lib/{structures/IAgenticaOperationCollection.js → json/IAgenticaOperationSelectionJson.js} +1 -1
- package/lib/json/IAgenticaOperationSelectionJson.js.map +1 -0
- package/lib/json/IAgenticaPromptJson.d.ts +111 -0
- package/lib/json/IAgenticaPromptJson.js +3 -0
- package/lib/json/IAgenticaPromptJson.js.map +1 -0
- package/lib/{structures/IAgenticaTokenUsage.d.ts → json/IAgenticaTokenUsageJson.d.ts} +10 -10
- package/lib/json/IAgenticaTokenUsageJson.js +3 -0
- package/lib/json/IAgenticaTokenUsageJson.js.map +1 -0
- package/lib/prompts/AgenticaCancelPrompt.d.ts +16 -0
- package/lib/prompts/AgenticaCancelPrompt.js +20 -0
- package/lib/prompts/AgenticaCancelPrompt.js.map +1 -0
- package/lib/prompts/AgenticaDescribePrompt.d.ts +24 -0
- package/lib/prompts/AgenticaDescribePrompt.js +20 -0
- package/lib/prompts/AgenticaDescribePrompt.js.map +1 -0
- package/lib/prompts/AgenticaExecutePrompt.d.ts +20 -0
- package/lib/prompts/AgenticaExecutePrompt.js +24 -0
- package/lib/prompts/AgenticaExecutePrompt.js.map +1 -0
- package/lib/prompts/AgenticaPrompt.d.ts +7 -0
- package/lib/{typings/AgenticaSource.js → prompts/AgenticaPrompt.js} +1 -1
- package/lib/prompts/AgenticaPrompt.js.map +1 -0
- package/lib/prompts/AgenticaPromptBase.d.ts +22 -0
- package/lib/prompts/AgenticaPromptBase.js +22 -0
- package/lib/prompts/AgenticaPromptBase.js.map +1 -0
- package/lib/prompts/AgenticaSelectPrompt.d.ts +16 -0
- package/lib/prompts/AgenticaSelectPrompt.js +20 -0
- package/lib/prompts/AgenticaSelectPrompt.js.map +1 -0
- package/lib/prompts/AgenticaTextPrompt.d.ts +14 -0
- package/lib/prompts/AgenticaTextPrompt.js +20 -0
- package/lib/prompts/AgenticaTextPrompt.js.map +1 -0
- package/lib/structures/IAgenticaConfig.d.ts +3 -3
- package/lib/structures/IAgenticaExecutor.d.ts +12 -11
- package/lib/structures/IAgenticaProps.d.ts +2 -3
- package/lib/structures/IAgenticaSystemPrompt.d.ts +12 -11
- package/lib/transformers/AgenticaEventTransformer.d.ts +45 -0
- package/lib/transformers/AgenticaEventTransformer.js +127 -0
- package/lib/transformers/AgenticaEventTransformer.js.map +1 -0
- package/lib/transformers/AgenticaPromptTransformer.d.ts +34 -0
- package/lib/transformers/AgenticaPromptTransformer.js +90 -0
- package/lib/transformers/AgenticaPromptTransformer.js.map +1 -0
- package/package.json +1 -1
- package/src/Agentica.ts +94 -61
- package/src/chatgpt/ChatGptAgent.ts +8 -7
- package/src/chatgpt/ChatGptCallFunctionAgent.ts +90 -88
- package/src/chatgpt/ChatGptCancelFunctionAgent.ts +51 -58
- package/src/chatgpt/ChatGptCompletionMessageUtil.ts +166 -0
- package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +81 -12
- package/src/chatgpt/ChatGptHistoryDecoder.ts +14 -14
- package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +21 -13
- package/src/chatgpt/ChatGptSelectFunctionAgent.ts +66 -74
- package/src/chatgpt/ChatGptUsageAggregator.ts +62 -0
- package/src/context/AgenticaCancelPrompt.ts +32 -0
- package/src/context/AgenticaClassOperation.ts +23 -0
- package/src/{structures/IAgenticaContext.ts → context/AgenticaContext.ts} +17 -16
- package/src/context/AgenticaHttpOperation.ts +27 -0
- package/src/{structures/IAgenticaOperation.ts → context/AgenticaOperation.ts} +25 -22
- package/src/context/AgenticaOperationBase.ts +57 -0
- package/src/{structures/IAgenticaOperationCollection.ts → context/AgenticaOperationCollection.ts} +6 -6
- package/src/context/AgenticaOperationSelection.ts +27 -0
- package/src/context/AgenticaTokenUsage.ts +170 -0
- package/src/context/internal/AgenticaTokenUsageAggregator.ts +66 -0
- package/src/events/AgenticaCallEvent.ts +36 -0
- package/src/events/AgenticaCancelEvent.ts +28 -0
- package/src/events/AgenticaDescribeEvent.ts +61 -0
- package/src/events/AgenticaEvent.ts +36 -0
- package/src/events/AgenticaEventBase.ts +7 -0
- package/src/{typings/AgenticaSource.ts → events/AgenticaEventSource.ts} +1 -1
- package/src/events/AgenticaExecuteEvent.ts +50 -0
- package/src/events/AgenticaInitializeEvent.ts +14 -0
- package/src/events/AgenticaRequestEvent.ts +45 -0
- package/src/events/AgenticaResponseEvent.ts +48 -0
- package/src/events/AgenticaSelectEvent.ts +37 -0
- package/src/events/AgenticaTextEvent.ts +57 -0
- package/src/index.ts +33 -9
- package/src/internal/AgenticaOperationComposer.ts +24 -15
- package/src/internal/ByteArrayUtil.ts +5 -0
- package/src/internal/MPSCUtil.ts +75 -0
- package/src/internal/StreamUtil.ts +64 -0
- package/src/json/IAgenticaEventJson.ts +178 -0
- package/src/json/IAgenticaOperationJson.ts +36 -0
- package/src/json/IAgenticaOperationSelectionJson.ts +19 -0
- package/src/json/IAgenticaPromptJson.ts +130 -0
- package/src/{structures/IAgenticaTokenUsage.ts → json/IAgenticaTokenUsageJson.ts} +10 -10
- package/src/prompts/AgenticaCancelPrompt.ts +32 -0
- package/src/prompts/AgenticaDescribePrompt.ts +41 -0
- package/src/prompts/AgenticaExecutePrompt.ts +52 -0
- package/src/prompts/AgenticaPrompt.ts +14 -0
- package/src/prompts/AgenticaPromptBase.ts +27 -0
- package/src/prompts/AgenticaSelectPrompt.ts +32 -0
- package/src/prompts/AgenticaTextPrompt.ts +31 -0
- package/src/structures/IAgenticaConfig.ts +3 -3
- package/src/structures/IAgenticaExecutor.ts +14 -13
- package/src/structures/IAgenticaProps.ts +2 -3
- package/src/structures/IAgenticaSystemPrompt.ts +12 -11
- package/src/transformers/AgenticaEventTransformer.ts +165 -0
- package/src/transformers/AgenticaPromptTransformer.ts +134 -0
- package/lib/internal/AgenticaPromptFactory.d.ts +0 -7
- package/lib/internal/AgenticaPromptFactory.js +0 -9
- package/lib/internal/AgenticaPromptFactory.js.map +0 -1
- package/lib/internal/AgenticaPromptTransformer.d.ts +0 -10
- package/lib/internal/AgenticaPromptTransformer.js +0 -58
- package/lib/internal/AgenticaPromptTransformer.js.map +0 -1
- package/lib/internal/AgenticaTokenUsageAggregator.d.ts +0 -11
- package/lib/internal/AgenticaTokenUsageAggregator.js +0 -92
- package/lib/internal/AgenticaTokenUsageAggregator.js.map +0 -1
- package/lib/structures/IAgenticaContext.js.map +0 -1
- package/lib/structures/IAgenticaEvent.d.ts +0 -192
- package/lib/structures/IAgenticaEvent.js.map +0 -1
- package/lib/structures/IAgenticaOperation.d.ts +0 -47
- package/lib/structures/IAgenticaOperation.js.map +0 -1
- package/lib/structures/IAgenticaOperationCollection.js.map +0 -1
- package/lib/structures/IAgenticaOperationSelection.d.ts +0 -50
- package/lib/structures/IAgenticaOperationSelection.js.map +0 -1
- package/lib/structures/IAgenticaPrompt.d.ts +0 -138
- package/lib/structures/IAgenticaPrompt.js.map +0 -1
- package/lib/structures/IAgenticaTokenUsage.js +0 -3
- package/lib/structures/IAgenticaTokenUsage.js.map +0 -1
- package/lib/structures/internal/__IChatCancelFunctionsApplication.js.map +0 -1
- package/lib/structures/internal/__IChatFunctionReference.js.map +0 -1
- package/lib/structures/internal/__IChatInitialApplication.js.map +0 -1
- package/lib/structures/internal/__IChatSelectFunctionsApplication.js.map +0 -1
- package/lib/typings/AgenticaSource.d.ts +0 -1
- package/lib/typings/AgenticaSource.js.map +0 -1
- package/src/internal/AgenticaPromptFactory.ts +0 -32
- package/src/internal/AgenticaPromptTransformer.ts +0 -86
- package/src/internal/AgenticaTokenUsageAggregator.ts +0 -115
- package/src/structures/IAgenticaEvent.ts +0 -229
- package/src/structures/IAgenticaOperationSelection.ts +0 -68
- package/src/structures/IAgenticaPrompt.ts +0 -182
- /package/lib/{structures → context}/internal/__IChatCancelFunctionsApplication.d.ts +0 -0
- /package/lib/{structures → context}/internal/__IChatCancelFunctionsApplication.js +0 -0
- /package/lib/{structures → context}/internal/__IChatFunctionReference.d.ts +0 -0
- /package/lib/{structures → context}/internal/__IChatFunctionReference.js +0 -0
- /package/lib/{structures → context}/internal/__IChatInitialApplication.d.ts +0 -0
- /package/lib/{structures → context}/internal/__IChatInitialApplication.js +0 -0
- /package/lib/{structures → context}/internal/__IChatSelectFunctionsApplication.d.ts +0 -0
- /package/lib/{structures → context}/internal/__IChatSelectFunctionsApplication.js +0 -0
- /package/src/{structures → context}/internal/__IChatCancelFunctionsApplication.ts +0 -0
- /package/src/{structures → context}/internal/__IChatFunctionReference.ts +0 -0
- /package/src/{structures → context}/internal/__IChatInitialApplication.ts +0 -0
- /package/src/{structures → context}/internal/__IChatSelectFunctionsApplication.ts +0 -0
package/src/Agentica.ts
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
import OpenAI from "openai";
|
|
3
2
|
|
|
4
3
|
import { ChatGptAgent } from "./chatgpt/ChatGptAgent";
|
|
4
|
+
import { ChatGptCompletionMessageUtil } from "./chatgpt/ChatGptCompletionMessageUtil";
|
|
5
|
+
import { AgenticaContext } from "./context/AgenticaContext";
|
|
6
|
+
import { AgenticaOperation } from "./context/AgenticaOperation";
|
|
7
|
+
import { AgenticaOperationCollection } from "./context/AgenticaOperationCollection";
|
|
8
|
+
import { AgenticaOperationSelection } from "./context/AgenticaOperationSelection";
|
|
9
|
+
import { AgenticaTokenUsage } from "./context/AgenticaTokenUsage";
|
|
10
|
+
import { AgenticaTokenUsageAggregator } from "./context/internal/AgenticaTokenUsageAggregator";
|
|
11
|
+
import { AgenticaEvent } from "./events/AgenticaEvent";
|
|
12
|
+
import { AgenticaInitializeEvent } from "./events/AgenticaInitializeEvent";
|
|
13
|
+
import { AgenticaRequestEvent } from "./events/AgenticaRequestEvent";
|
|
14
|
+
import { AgenticaTextEvent } from "./events/AgenticaTextEvent";
|
|
5
15
|
import { AgenticaOperationComposer } from "./internal/AgenticaOperationComposer";
|
|
6
|
-
import {
|
|
7
|
-
import { AgenticaTokenUsageAggregator } from "./internal/AgenticaTokenUsageAggregator";
|
|
16
|
+
import { StreamUtil } from "./internal/StreamUtil";
|
|
8
17
|
import { __map_take } from "./internal/__map_take";
|
|
18
|
+
import { AgenticaPrompt } from "./prompts/AgenticaPrompt";
|
|
19
|
+
import { AgenticaTextPrompt } from "./prompts/AgenticaTextPrompt";
|
|
9
20
|
import { IAgenticaConfig } from "./structures/IAgenticaConfig";
|
|
10
|
-
import { IAgenticaContext } from "./structures/IAgenticaContext";
|
|
11
21
|
import { IAgenticaController } from "./structures/IAgenticaController";
|
|
12
|
-
import { IAgenticaEvent } from "./structures/IAgenticaEvent";
|
|
13
|
-
import { IAgenticaOperation } from "./structures/IAgenticaOperation";
|
|
14
|
-
import { IAgenticaOperationCollection } from "./structures/IAgenticaOperationCollection";
|
|
15
|
-
import { IAgenticaOperationSelection } from "./structures/IAgenticaOperationSelection";
|
|
16
|
-
import { IAgenticaPrompt } from "./structures/IAgenticaPrompt";
|
|
17
22
|
import { IAgenticaProps } from "./structures/IAgenticaProps";
|
|
18
|
-
import { IAgenticaTokenUsage } from "./structures/IAgenticaTokenUsage";
|
|
19
23
|
import { IAgenticaVendor } from "./structures/IAgenticaVendor";
|
|
24
|
+
import { AgenticaPromptTransformer } from "./transformers/AgenticaPromptTransformer";
|
|
20
25
|
|
|
21
26
|
/**
|
|
22
27
|
* Nestia A.I. chatbot agent.
|
|
@@ -39,27 +44,27 @@ import { IAgenticaVendor } from "./structures/IAgenticaVendor";
|
|
|
39
44
|
* - {@link IAgenticaSystemPrompt}
|
|
40
45
|
* - Accessors
|
|
41
46
|
* - {@link IAgenticaOperation}
|
|
42
|
-
* - {@link
|
|
43
|
-
* - {@link
|
|
44
|
-
* - {@link
|
|
47
|
+
* - {@link IAgenticaPromptJson}
|
|
48
|
+
* - {@link IAgenticaEventJson}
|
|
49
|
+
* - {@link IAgenticaTokenUsageJson}
|
|
45
50
|
*
|
|
46
51
|
* @author Samchon
|
|
47
52
|
*/
|
|
48
53
|
export class Agentica<Model extends ILlmSchema.Model> {
|
|
49
54
|
// THE OPERATIONS
|
|
50
|
-
private readonly operations_:
|
|
55
|
+
private readonly operations_: AgenticaOperationCollection<Model>;
|
|
51
56
|
|
|
52
57
|
// STACK
|
|
53
|
-
private readonly stack_:
|
|
54
|
-
private readonly prompt_histories_:
|
|
58
|
+
private readonly stack_: AgenticaOperationSelection<Model>[];
|
|
59
|
+
private readonly prompt_histories_: AgenticaPrompt<Model>[];
|
|
55
60
|
private readonly listeners_: Map<string, Set<Function>>;
|
|
56
61
|
|
|
57
62
|
// STATUS
|
|
58
|
-
private readonly token_usage_:
|
|
63
|
+
private readonly token_usage_: AgenticaTokenUsage;
|
|
59
64
|
private ready_: boolean;
|
|
60
65
|
private readonly executor_: (
|
|
61
|
-
ctx:
|
|
62
|
-
) => Promise<
|
|
66
|
+
ctx: AgenticaContext<Model>,
|
|
67
|
+
) => Promise<AgenticaPrompt<Model>[]>;
|
|
63
68
|
|
|
64
69
|
/* -----------------------------------------------------------
|
|
65
70
|
CONSTRUCTOR
|
|
@@ -82,12 +87,12 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
82
87
|
this.prompt_histories_ = (props.histories ?? []).map((input) =>
|
|
83
88
|
AgenticaPromptTransformer.transform({
|
|
84
89
|
operations: this.operations_.group,
|
|
85
|
-
input,
|
|
90
|
+
prompt: input,
|
|
86
91
|
}),
|
|
87
92
|
);
|
|
88
93
|
|
|
89
94
|
// STATUS
|
|
90
|
-
this.token_usage_ =
|
|
95
|
+
this.token_usage_ = AgenticaTokenUsage.zero();
|
|
91
96
|
this.ready_ = false;
|
|
92
97
|
this.executor_ =
|
|
93
98
|
typeof props.config?.executor === "function"
|
|
@@ -115,20 +120,27 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
115
120
|
*
|
|
116
121
|
* When the user's conversation implies the A.I. chatbot to execute a
|
|
117
122
|
* function calling, the returned chat prompts will contain the
|
|
118
|
-
* function calling information like {@link
|
|
123
|
+
* function calling information like {@link IAgenticaPromptJson.IExecute}.
|
|
119
124
|
*
|
|
120
125
|
* @param content The content to talk
|
|
121
126
|
* @returns List of newly created chat prompts
|
|
122
127
|
*/
|
|
123
|
-
public async conversate(content: string): Promise<
|
|
124
|
-
const prompt:
|
|
125
|
-
type: "text",
|
|
128
|
+
public async conversate(content: string): Promise<AgenticaPrompt<Model>[]> {
|
|
129
|
+
const prompt: AgenticaTextPrompt<"user"> = new AgenticaTextPrompt({
|
|
126
130
|
role: "user",
|
|
127
131
|
text: content,
|
|
128
|
-
};
|
|
129
|
-
await this.dispatch(
|
|
132
|
+
});
|
|
133
|
+
await this.dispatch(
|
|
134
|
+
new AgenticaTextEvent({
|
|
135
|
+
role: "user",
|
|
136
|
+
stream: StreamUtil.to(content),
|
|
137
|
+
done: () => true,
|
|
138
|
+
get: () => content,
|
|
139
|
+
join: () => Promise.resolve(content),
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
130
142
|
|
|
131
|
-
const newbie:
|
|
143
|
+
const newbie: AgenticaPrompt<Model>[] = await this.executor_(
|
|
132
144
|
this.getContext({
|
|
133
145
|
prompt,
|
|
134
146
|
usage: this.token_usage_,
|
|
@@ -170,7 +182,7 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
170
182
|
*
|
|
171
183
|
* @returns
|
|
172
184
|
*/
|
|
173
|
-
public getOperations(): ReadonlyArray<
|
|
185
|
+
public getOperations(): ReadonlyArray<AgenticaOperation<Model>> {
|
|
174
186
|
return this.operations_.array;
|
|
175
187
|
}
|
|
176
188
|
|
|
@@ -181,7 +193,7 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
181
193
|
*
|
|
182
194
|
* @returns List of chat prompts
|
|
183
195
|
*/
|
|
184
|
-
public getPromptHistories():
|
|
196
|
+
public getPromptHistories(): AgenticaPrompt<Model>[] {
|
|
185
197
|
return this.prompt_histories_;
|
|
186
198
|
}
|
|
187
199
|
|
|
@@ -193,7 +205,7 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
193
205
|
*
|
|
194
206
|
* @returns Cost of the A.I. chatbot
|
|
195
207
|
*/
|
|
196
|
-
public getTokenUsage():
|
|
208
|
+
public getTokenUsage(): AgenticaTokenUsage {
|
|
197
209
|
return this.token_usage_;
|
|
198
210
|
}
|
|
199
211
|
|
|
@@ -201,10 +213,10 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
201
213
|
* @internal
|
|
202
214
|
*/
|
|
203
215
|
public getContext(props: {
|
|
204
|
-
prompt:
|
|
205
|
-
usage:
|
|
206
|
-
}):
|
|
207
|
-
const dispatch = (event:
|
|
216
|
+
prompt: AgenticaTextPrompt<"user">;
|
|
217
|
+
usage: AgenticaTokenUsage;
|
|
218
|
+
}): AgenticaContext<Model> {
|
|
219
|
+
const dispatch = (event: AgenticaEvent<Model>) => this.dispatch(event);
|
|
208
220
|
return {
|
|
209
221
|
// APPLICATION
|
|
210
222
|
operations: this.operations_,
|
|
@@ -218,44 +230,65 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
218
230
|
|
|
219
231
|
// HANDLERS
|
|
220
232
|
dispatch: (event) => this.dispatch(event),
|
|
221
|
-
request: async (
|
|
233
|
+
request: async (source, body) => {
|
|
222
234
|
// request information
|
|
223
|
-
const event:
|
|
224
|
-
|
|
225
|
-
source: kind,
|
|
235
|
+
const event: AgenticaRequestEvent = new AgenticaRequestEvent({
|
|
236
|
+
source,
|
|
226
237
|
body: {
|
|
227
238
|
...body,
|
|
228
239
|
model: this.props.vendor.model,
|
|
240
|
+
stream: true,
|
|
229
241
|
},
|
|
230
242
|
options: this.props.vendor.options,
|
|
231
|
-
};
|
|
243
|
+
});
|
|
232
244
|
await dispatch(event);
|
|
233
245
|
|
|
234
246
|
// completion
|
|
235
|
-
const completion
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
247
|
+
const completion = await this.props.vendor.api.chat.completions.create(
|
|
248
|
+
event.body,
|
|
249
|
+
event.options,
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const [streamForEvent, temporaryStream] = StreamUtil.transform(
|
|
253
|
+
completion.toReadableStream() as ReadableStream<Uint8Array>,
|
|
254
|
+
(value) =>
|
|
255
|
+
ChatGptCompletionMessageUtil.transformCompletionChunk(value),
|
|
256
|
+
).tee();
|
|
257
|
+
|
|
258
|
+
const [streamForAggregate, streamForReturn] = temporaryStream.tee();
|
|
259
|
+
|
|
260
|
+
void (async () => {
|
|
261
|
+
const reader = streamForAggregate.getReader();
|
|
262
|
+
while (true) {
|
|
263
|
+
const chunk = await reader.read();
|
|
264
|
+
if (chunk.done) break;
|
|
265
|
+
if (chunk.value.usage) {
|
|
266
|
+
AgenticaTokenUsageAggregator.aggregate({
|
|
267
|
+
kind: source,
|
|
268
|
+
completionUsage: chunk.value.usage,
|
|
269
|
+
usage: props.usage,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
})();
|
|
274
|
+
|
|
245
275
|
await dispatch({
|
|
246
276
|
type: "response",
|
|
247
|
-
source:
|
|
277
|
+
source: source,
|
|
278
|
+
stream: streamForEvent,
|
|
248
279
|
body: event.body,
|
|
249
280
|
options: event.options,
|
|
250
|
-
|
|
281
|
+
join: async () => {
|
|
282
|
+
const chunks = await StreamUtil.readAll(streamForEvent);
|
|
283
|
+
return ChatGptCompletionMessageUtil.merge(chunks);
|
|
284
|
+
},
|
|
251
285
|
});
|
|
252
|
-
|
|
286
|
+
|
|
287
|
+
return streamForReturn;
|
|
253
288
|
},
|
|
254
289
|
initialize: async () => {
|
|
255
290
|
this.ready_ = true;
|
|
256
|
-
await dispatch(
|
|
257
|
-
type: "initialize",
|
|
258
|
-
});
|
|
291
|
+
await dispatch(new AgenticaInitializeEvent());
|
|
259
292
|
},
|
|
260
293
|
};
|
|
261
294
|
}
|
|
@@ -271,10 +304,10 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
271
304
|
* @param type Type of event
|
|
272
305
|
* @param listener Callback function to be called whenever the event is emitted
|
|
273
306
|
*/
|
|
274
|
-
public on<Type extends
|
|
307
|
+
public on<Type extends AgenticaEvent.Type>(
|
|
275
308
|
type: Type,
|
|
276
309
|
listener: (
|
|
277
|
-
event:
|
|
310
|
+
event: AgenticaEvent.Mapper<Model>[Type],
|
|
278
311
|
) => void | Promise<void>,
|
|
279
312
|
): this {
|
|
280
313
|
__map_take(this.listeners_, type, () => new Set()).add(listener);
|
|
@@ -289,10 +322,10 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
289
322
|
* @param type Type of event
|
|
290
323
|
* @param listener Callback function to erase
|
|
291
324
|
*/
|
|
292
|
-
public off<Type extends
|
|
325
|
+
public off<Type extends AgenticaEvent.Type>(
|
|
293
326
|
type: Type,
|
|
294
327
|
listener: (
|
|
295
|
-
event:
|
|
328
|
+
event: AgenticaEvent.Mapper<Model>[Type],
|
|
296
329
|
) => void | Promise<void>,
|
|
297
330
|
): this {
|
|
298
331
|
const set = this.listeners_.get(type);
|
|
@@ -303,7 +336,7 @@ export class Agentica<Model extends ILlmSchema.Model> {
|
|
|
303
336
|
return this;
|
|
304
337
|
}
|
|
305
338
|
|
|
306
|
-
private async dispatch<Event extends
|
|
339
|
+
private async dispatch<Event extends AgenticaEvent<Model>>(
|
|
307
340
|
event: Event,
|
|
308
341
|
): Promise<void> {
|
|
309
342
|
const set = this.listeners_.get(event.type);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { ILlmSchema } from "@samchon/openapi";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { AgenticaContext } from "../context/AgenticaContext";
|
|
4
|
+
import { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
|
|
5
|
+
import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
|
|
4
6
|
import { IAgenticaExecutor } from "../structures/IAgenticaExecutor";
|
|
5
|
-
import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
|
|
6
7
|
import { ChatGptCallFunctionAgent } from "./ChatGptCallFunctionAgent";
|
|
7
8
|
import { ChatGptCancelFunctionAgent } from "./ChatGptCancelFunctionAgent";
|
|
8
9
|
import { ChatGptDescribeFunctionAgent } from "./ChatGptDescribeFunctionAgent";
|
|
@@ -14,8 +15,8 @@ export namespace ChatGptAgent {
|
|
|
14
15
|
<Model extends ILlmSchema.Model>(
|
|
15
16
|
executor: Partial<IAgenticaExecutor<Model>> | null,
|
|
16
17
|
) =>
|
|
17
|
-
async (ctx:
|
|
18
|
-
const histories:
|
|
18
|
+
async (ctx: AgenticaContext<Model>): Promise<AgenticaPrompt<Model>[]> => {
|
|
19
|
+
const histories: AgenticaPrompt<Model>[] = [];
|
|
19
20
|
|
|
20
21
|
// FUNCTIONS ARE NOT LISTED YET
|
|
21
22
|
if (ctx.ready() === false) {
|
|
@@ -49,19 +50,19 @@ export namespace ChatGptAgent {
|
|
|
49
50
|
// FUNCTION CALLING LOOP
|
|
50
51
|
while (true) {
|
|
51
52
|
// EXECUTE FUNCTIONS
|
|
52
|
-
const prompts:
|
|
53
|
+
const prompts: AgenticaPrompt<Model>[] = await (
|
|
53
54
|
executor?.call ?? ChatGptCallFunctionAgent.execute
|
|
54
55
|
)(ctx);
|
|
55
56
|
histories.push(...prompts);
|
|
56
57
|
|
|
57
58
|
// EXPLAIN RETURN VALUES
|
|
58
|
-
const executes:
|
|
59
|
+
const executes: AgenticaExecutePrompt<Model>[] = prompts.filter(
|
|
59
60
|
(prompt) => prompt.type === "execute",
|
|
60
61
|
);
|
|
61
62
|
for (const e of executes)
|
|
62
63
|
await ChatGptCancelFunctionAgent.cancelFunction(ctx, {
|
|
63
64
|
reason: "completed",
|
|
64
|
-
name: e.
|
|
65
|
+
name: e.operation.name,
|
|
65
66
|
});
|
|
66
67
|
histories.push(
|
|
67
68
|
...(await (
|
|
@@ -9,25 +9,33 @@ import {
|
|
|
9
9
|
import OpenAI from "openai";
|
|
10
10
|
import { IValidation } from "typia";
|
|
11
11
|
|
|
12
|
+
import { AgenticaCancelPrompt } from "../context/AgenticaCancelPrompt";
|
|
13
|
+
import { AgenticaContext } from "../context/AgenticaContext";
|
|
14
|
+
import { AgenticaOperation } from "../context/AgenticaOperation";
|
|
15
|
+
import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
|
|
16
|
+
import { AgenticaCallEvent } from "../events/AgenticaCallEvent";
|
|
17
|
+
import { AgenticaCancelEvent } from "../events/AgenticaCancelEvent";
|
|
18
|
+
import { AgenticaExecuteEvent } from "../events/AgenticaExecuteEvent";
|
|
19
|
+
import { AgenticaTextEvent } from "../events/AgenticaTextEvent";
|
|
12
20
|
import { AgenticaConstant } from "../internal/AgenticaConstant";
|
|
13
21
|
import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
|
|
14
|
-
import { AgenticaPromptFactory } from "../internal/AgenticaPromptFactory";
|
|
15
22
|
import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
23
|
+
import { StreamUtil } from "../internal/StreamUtil";
|
|
24
|
+
import { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
|
|
25
|
+
import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
|
|
26
|
+
import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
|
|
20
27
|
import { ChatGptCancelFunctionAgent } from "./ChatGptCancelFunctionAgent";
|
|
28
|
+
import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
|
|
21
29
|
import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
|
|
22
30
|
|
|
23
31
|
export namespace ChatGptCallFunctionAgent {
|
|
24
32
|
export const execute = async <Model extends ILlmSchema.Model>(
|
|
25
|
-
ctx:
|
|
26
|
-
): Promise<
|
|
33
|
+
ctx: AgenticaContext<Model>,
|
|
34
|
+
): Promise<AgenticaPrompt<Model>[]> => {
|
|
27
35
|
//----
|
|
28
36
|
// EXECUTE CHATGPT API
|
|
29
37
|
//----
|
|
30
|
-
const
|
|
38
|
+
const completionStream = await ctx.request("call", {
|
|
31
39
|
messages: [
|
|
32
40
|
// COMMON SYSTEM PROMPT
|
|
33
41
|
{
|
|
@@ -51,14 +59,14 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
51
59
|
],
|
|
52
60
|
// STACKED FUNCTIONS
|
|
53
61
|
tools: ctx.stack.map(
|
|
54
|
-
(
|
|
62
|
+
(s) =>
|
|
55
63
|
({
|
|
56
64
|
type: "function",
|
|
57
65
|
function: {
|
|
58
|
-
name:
|
|
59
|
-
description:
|
|
60
|
-
parameters: (
|
|
61
|
-
? (
|
|
66
|
+
name: s.operation.name,
|
|
67
|
+
description: s.operation.function.description,
|
|
68
|
+
parameters: (s.operation.function.separated
|
|
69
|
+
? (s.operation.function.separated.llm ??
|
|
62
70
|
({
|
|
63
71
|
type: "object",
|
|
64
72
|
properties: {},
|
|
@@ -66,7 +74,7 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
66
74
|
additionalProperties: false,
|
|
67
75
|
$defs: {},
|
|
68
76
|
} satisfies IChatGptSchema.IParameters))
|
|
69
|
-
:
|
|
77
|
+
: s.operation.function.parameters) as Record<string, any>,
|
|
70
78
|
},
|
|
71
79
|
}) as OpenAI.ChatCompletionTool,
|
|
72
80
|
),
|
|
@@ -80,28 +88,31 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
80
88
|
const closures: Array<
|
|
81
89
|
() => Promise<
|
|
82
90
|
Array<
|
|
83
|
-
|
|
|
84
|
-
|
|
|
85
|
-
|
|
|
91
|
+
| AgenticaExecutePrompt<Model>
|
|
92
|
+
| AgenticaCancelPrompt<Model>
|
|
93
|
+
| AgenticaTextPrompt
|
|
86
94
|
>
|
|
87
95
|
>
|
|
88
96
|
> = [];
|
|
97
|
+
|
|
98
|
+
const chunks = await StreamUtil.readAll(completionStream);
|
|
99
|
+
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
100
|
+
|
|
89
101
|
for (const choice of completion.choices) {
|
|
90
102
|
for (const tc of choice.message.tool_calls ?? []) {
|
|
91
103
|
if (tc.type === "function") {
|
|
92
|
-
const operation:
|
|
104
|
+
const operation: AgenticaOperation<Model> | undefined =
|
|
93
105
|
ctx.operations.flat.get(tc.function.name);
|
|
94
106
|
if (operation === undefined) continue;
|
|
95
107
|
closures.push(
|
|
96
108
|
async (): Promise<
|
|
97
|
-
[
|
|
109
|
+
[AgenticaExecutePrompt<Model>, AgenticaCancelPrompt<Model>]
|
|
98
110
|
> => {
|
|
99
|
-
const call:
|
|
100
|
-
type: "call",
|
|
111
|
+
const call: AgenticaCallEvent<Model> = new AgenticaCallEvent({
|
|
101
112
|
id: tc.id,
|
|
102
113
|
operation,
|
|
103
114
|
arguments: JSON.parse(tc.function.arguments),
|
|
104
|
-
};
|
|
115
|
+
});
|
|
105
116
|
if (call.operation.protocol === "http")
|
|
106
117
|
fillHttpArguments({
|
|
107
118
|
operation: call.operation,
|
|
@@ -109,40 +120,43 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
109
120
|
});
|
|
110
121
|
await ctx.dispatch(call);
|
|
111
122
|
|
|
112
|
-
const execute:
|
|
123
|
+
const execute: AgenticaExecutePrompt<Model> = await propagate(
|
|
113
124
|
ctx,
|
|
114
125
|
call,
|
|
115
126
|
0,
|
|
116
127
|
);
|
|
117
|
-
await ctx.dispatch(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
128
|
+
await ctx.dispatch(
|
|
129
|
+
new AgenticaExecuteEvent({
|
|
130
|
+
id: call.id,
|
|
131
|
+
operation: call.operation,
|
|
132
|
+
arguments: execute.arguments,
|
|
133
|
+
value: execute.value,
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
124
136
|
|
|
125
137
|
await ChatGptCancelFunctionAgent.cancelFunction(ctx, {
|
|
126
138
|
name: call.operation.name,
|
|
127
139
|
reason: "completed",
|
|
128
140
|
});
|
|
129
|
-
await ctx.dispatch(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
141
|
+
await ctx.dispatch(
|
|
142
|
+
new AgenticaCancelEvent({
|
|
143
|
+
selection: new AgenticaOperationSelection({
|
|
144
|
+
operation: call.operation,
|
|
145
|
+
reason: "complete",
|
|
146
|
+
}),
|
|
147
|
+
}),
|
|
148
|
+
);
|
|
134
149
|
return [
|
|
135
150
|
execute,
|
|
136
|
-
{
|
|
137
|
-
type: "cancel",
|
|
151
|
+
new AgenticaCancelPrompt({
|
|
138
152
|
id: call.id,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
153
|
+
selections: [
|
|
154
|
+
new AgenticaOperationSelection({
|
|
155
|
+
operation: call.operation,
|
|
142
156
|
reason: "complete",
|
|
143
157
|
}),
|
|
144
158
|
],
|
|
145
|
-
}
|
|
159
|
+
}),
|
|
146
160
|
] as const;
|
|
147
161
|
},
|
|
148
162
|
);
|
|
@@ -153,12 +167,19 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
153
167
|
!!choice.message.content?.length
|
|
154
168
|
)
|
|
155
169
|
closures.push(async () => {
|
|
156
|
-
const value:
|
|
157
|
-
type: "text",
|
|
170
|
+
const value: AgenticaTextPrompt = new AgenticaTextPrompt({
|
|
158
171
|
role: "assistant",
|
|
159
172
|
text: choice.message.content!,
|
|
160
|
-
};
|
|
161
|
-
await ctx.dispatch(
|
|
173
|
+
});
|
|
174
|
+
await ctx.dispatch(
|
|
175
|
+
new AgenticaTextEvent({
|
|
176
|
+
role: "assistant",
|
|
177
|
+
get: () => value.text,
|
|
178
|
+
done: () => true,
|
|
179
|
+
stream: StreamUtil.to(value.text),
|
|
180
|
+
join: () => Promise.resolve(value.text),
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
162
183
|
return [value];
|
|
163
184
|
});
|
|
164
185
|
}
|
|
@@ -166,10 +187,10 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
166
187
|
};
|
|
167
188
|
|
|
168
189
|
const propagate = async <Model extends ILlmSchema.Model>(
|
|
169
|
-
ctx:
|
|
170
|
-
call:
|
|
190
|
+
ctx: AgenticaContext<Model>,
|
|
191
|
+
call: AgenticaCallEvent<Model>,
|
|
171
192
|
retry: number,
|
|
172
|
-
): Promise<
|
|
193
|
+
): Promise<AgenticaExecutePrompt<Model>> => {
|
|
173
194
|
if (call.operation.protocol === "http") {
|
|
174
195
|
//----
|
|
175
196
|
// HTTP PROTOCOL
|
|
@@ -182,7 +203,7 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
182
203
|
check.success === false &&
|
|
183
204
|
retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)
|
|
184
205
|
) {
|
|
185
|
-
const trial:
|
|
206
|
+
const trial: AgenticaExecutePrompt<Model> | null = await correct(
|
|
186
207
|
ctx,
|
|
187
208
|
call,
|
|
188
209
|
retry,
|
|
@@ -217,26 +238,18 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
217
238
|
(success === false
|
|
218
239
|
? await correct(ctx, call, retry, response.body)
|
|
219
240
|
: null) ??
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
protocol: "http",
|
|
223
|
-
controller: call.operation.controller,
|
|
224
|
-
function: call.operation.function,
|
|
241
|
+
new AgenticaExecutePrompt({
|
|
242
|
+
operation: call.operation,
|
|
225
243
|
id: call.id,
|
|
226
|
-
name: call.operation.name,
|
|
227
244
|
arguments: call.arguments,
|
|
228
245
|
value: response,
|
|
229
|
-
})
|
|
246
|
+
})
|
|
230
247
|
);
|
|
231
248
|
} catch (error) {
|
|
232
249
|
// DISPATCH ERROR
|
|
233
|
-
return
|
|
234
|
-
|
|
235
|
-
protocol: "http",
|
|
236
|
-
controller: call.operation.controller,
|
|
237
|
-
function: call.operation.function,
|
|
250
|
+
return new AgenticaExecutePrompt({
|
|
251
|
+
operation: call.operation,
|
|
238
252
|
id: call.id,
|
|
239
|
-
name: call.operation.name,
|
|
240
253
|
arguments: call.arguments,
|
|
241
254
|
value: {
|
|
242
255
|
status: 500,
|
|
@@ -265,13 +278,9 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
265
278
|
(retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)
|
|
266
279
|
? await correct(ctx, call, retry, check.errors)
|
|
267
280
|
: null) ??
|
|
268
|
-
|
|
269
|
-
type: "execute",
|
|
270
|
-
protocol: "class",
|
|
271
|
-
controller: call.operation.controller,
|
|
272
|
-
function: call.operation.function,
|
|
281
|
+
new AgenticaExecutePrompt({
|
|
273
282
|
id: call.id,
|
|
274
|
-
|
|
283
|
+
operation: call.operation,
|
|
275
284
|
arguments: call.arguments,
|
|
276
285
|
value: {
|
|
277
286
|
name: "TypeGuardError",
|
|
@@ -292,24 +301,16 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
292
301
|
: await (call.operation.controller.execute as any)[
|
|
293
302
|
call.operation.function.name
|
|
294
303
|
](call.arguments);
|
|
295
|
-
return
|
|
296
|
-
type: "execute",
|
|
297
|
-
protocol: "class",
|
|
298
|
-
controller: call.operation.controller,
|
|
299
|
-
function: call.operation.function,
|
|
304
|
+
return new AgenticaExecutePrompt({
|
|
300
305
|
id: call.id,
|
|
301
|
-
|
|
306
|
+
operation: call.operation,
|
|
302
307
|
arguments: call.arguments,
|
|
303
308
|
value,
|
|
304
309
|
});
|
|
305
310
|
} catch (error) {
|
|
306
|
-
return
|
|
307
|
-
type: "execute",
|
|
308
|
-
protocol: "class",
|
|
309
|
-
controller: call.operation.controller,
|
|
310
|
-
function: call.operation.function,
|
|
311
|
+
return new AgenticaExecutePrompt({
|
|
311
312
|
id: call.id,
|
|
312
|
-
|
|
313
|
+
operation: call.operation,
|
|
313
314
|
arguments: call.arguments,
|
|
314
315
|
value:
|
|
315
316
|
error instanceof Error
|
|
@@ -325,15 +326,15 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
325
326
|
};
|
|
326
327
|
|
|
327
328
|
const correct = async <Model extends ILlmSchema.Model>(
|
|
328
|
-
ctx:
|
|
329
|
-
call:
|
|
329
|
+
ctx: AgenticaContext<Model>,
|
|
330
|
+
call: AgenticaCallEvent<Model>,
|
|
330
331
|
retry: number,
|
|
331
332
|
error: unknown,
|
|
332
|
-
): Promise<
|
|
333
|
+
): Promise<AgenticaExecutePrompt<Model> | null> => {
|
|
333
334
|
//----
|
|
334
335
|
// EXECUTE CHATGPT API
|
|
335
336
|
//----
|
|
336
|
-
const
|
|
337
|
+
const completionStream = await ctx.request("call", {
|
|
337
338
|
messages: [
|
|
338
339
|
// COMMON SYSTEM PROMPT
|
|
339
340
|
{
|
|
@@ -405,6 +406,8 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
405
406
|
parallel_tool_calls: false,
|
|
406
407
|
});
|
|
407
408
|
|
|
409
|
+
const chunks = await StreamUtil.readAll(completionStream);
|
|
410
|
+
const completion = ChatGptCompletionMessageUtil.merge(chunks);
|
|
408
411
|
//----
|
|
409
412
|
// PROCESS COMPLETION
|
|
410
413
|
//----
|
|
@@ -417,18 +420,17 @@ export namespace ChatGptCallFunctionAgent {
|
|
|
417
420
|
if (toolCall === undefined) return null;
|
|
418
421
|
return propagate(
|
|
419
422
|
ctx,
|
|
420
|
-
{
|
|
423
|
+
new AgenticaCallEvent({
|
|
421
424
|
id: toolCall.id,
|
|
422
|
-
type: "call",
|
|
423
425
|
operation: call.operation,
|
|
424
426
|
arguments: JSON.parse(toolCall.function.arguments),
|
|
425
|
-
},
|
|
427
|
+
}),
|
|
426
428
|
retry,
|
|
427
429
|
);
|
|
428
430
|
};
|
|
429
431
|
|
|
430
432
|
const fillHttpArguments = <Model extends ILlmSchema.Model>(props: {
|
|
431
|
-
operation:
|
|
433
|
+
operation: AgenticaOperation<Model>;
|
|
432
434
|
arguments: object;
|
|
433
435
|
}): void => {
|
|
434
436
|
if (props.operation.protocol !== "http") return;
|