@agentica/core 0.12.1 → 0.12.2-dev.20250314

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.
Files changed (77) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +461 -461
  3. package/package.json +1 -1
  4. package/prompts/cancel.md +4 -4
  5. package/prompts/common.md +2 -2
  6. package/prompts/describe.md +6 -6
  7. package/prompts/execute.md +6 -6
  8. package/prompts/initialize.md +2 -2
  9. package/prompts/select.md +6 -6
  10. package/src/Agentica.ts +359 -359
  11. package/src/chatgpt/ChatGptAgent.ts +76 -76
  12. package/src/chatgpt/ChatGptCallFunctionAgent.ts +466 -466
  13. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +280 -280
  14. package/src/chatgpt/ChatGptCompletionMessageUtil.ts +166 -166
  15. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +122 -122
  16. package/src/chatgpt/ChatGptHistoryDecoder.ts +88 -88
  17. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +96 -96
  18. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +311 -311
  19. package/src/chatgpt/ChatGptUsageAggregator.ts +62 -62
  20. package/src/context/AgenticaCancelPrompt.ts +32 -32
  21. package/src/context/AgenticaClassOperation.ts +23 -23
  22. package/src/context/AgenticaContext.ts +130 -130
  23. package/src/context/AgenticaHttpOperation.ts +27 -27
  24. package/src/context/AgenticaOperation.ts +66 -66
  25. package/src/context/AgenticaOperationBase.ts +57 -57
  26. package/src/context/AgenticaOperationCollection.ts +52 -52
  27. package/src/context/AgenticaOperationSelection.ts +27 -27
  28. package/src/context/AgenticaTokenUsage.ts +170 -170
  29. package/src/context/internal/AgenticaTokenUsageAggregator.ts +66 -66
  30. package/src/context/internal/__IChatCancelFunctionsApplication.ts +23 -23
  31. package/src/context/internal/__IChatFunctionReference.ts +21 -21
  32. package/src/context/internal/__IChatInitialApplication.ts +15 -15
  33. package/src/context/internal/__IChatSelectFunctionsApplication.ts +24 -24
  34. package/src/events/AgenticaCallEvent.ts +36 -36
  35. package/src/events/AgenticaCancelEvent.ts +28 -28
  36. package/src/events/AgenticaDescribeEvent.ts +66 -66
  37. package/src/events/AgenticaEvent.ts +36 -36
  38. package/src/events/AgenticaEventBase.ts +7 -7
  39. package/src/events/AgenticaEventSource.ts +6 -6
  40. package/src/events/AgenticaExecuteEvent.ts +50 -50
  41. package/src/events/AgenticaInitializeEvent.ts +14 -14
  42. package/src/events/AgenticaRequestEvent.ts +45 -45
  43. package/src/events/AgenticaResponseEvent.ts +48 -48
  44. package/src/events/AgenticaSelectEvent.ts +37 -37
  45. package/src/events/AgenticaTextEvent.ts +62 -62
  46. package/src/functional/assertHttpLlmApplication.ts +55 -55
  47. package/src/functional/validateHttpLlmApplication.ts +66 -66
  48. package/src/index.ts +44 -44
  49. package/src/internal/AgenticaConstant.ts +4 -4
  50. package/src/internal/AgenticaDefaultPrompt.ts +43 -43
  51. package/src/internal/AgenticaOperationComposer.ts +96 -96
  52. package/src/internal/ByteArrayUtil.ts +5 -5
  53. package/src/internal/MPSCUtil.ts +111 -111
  54. package/src/internal/MathUtil.ts +3 -3
  55. package/src/internal/Singleton.ts +22 -22
  56. package/src/internal/StreamUtil.ts +64 -64
  57. package/src/internal/__map_take.ts +15 -15
  58. package/src/json/IAgenticaEventJson.ts +178 -178
  59. package/src/json/IAgenticaOperationJson.ts +36 -36
  60. package/src/json/IAgenticaOperationSelectionJson.ts +19 -19
  61. package/src/json/IAgenticaPromptJson.ts +130 -130
  62. package/src/json/IAgenticaTokenUsageJson.ts +107 -107
  63. package/src/prompts/AgenticaCancelPrompt.ts +32 -32
  64. package/src/prompts/AgenticaDescribePrompt.ts +41 -41
  65. package/src/prompts/AgenticaExecutePrompt.ts +52 -52
  66. package/src/prompts/AgenticaPrompt.ts +14 -14
  67. package/src/prompts/AgenticaPromptBase.ts +27 -27
  68. package/src/prompts/AgenticaSelectPrompt.ts +32 -32
  69. package/src/prompts/AgenticaTextPrompt.ts +31 -31
  70. package/src/structures/IAgenticaConfig.ts +123 -123
  71. package/src/structures/IAgenticaController.ts +133 -133
  72. package/src/structures/IAgenticaExecutor.ts +157 -157
  73. package/src/structures/IAgenticaProps.ts +69 -69
  74. package/src/structures/IAgenticaSystemPrompt.ts +125 -125
  75. package/src/structures/IAgenticaVendor.ts +39 -39
  76. package/src/transformers/AgenticaEventTransformer.ts +165 -165
  77. package/src/transformers/AgenticaPromptTransformer.ts +134 -134
@@ -1,64 +1,64 @@
1
- export namespace StreamUtil {
2
- export const readAll = async <T>(stream: ReadableStream<T>): Promise<T[]> => {
3
- const reader = stream.getReader();
4
- const result: T[] = [];
5
- while (true) {
6
- const { done, value } = await reader.read();
7
- if (done) break;
8
- result.push(value);
9
- }
10
- return result;
11
- };
12
-
13
- export const reduce = async <T, R = T>(
14
- stream: ReadableStream<T>,
15
- reducer: (acc: T | R, cur: T) => R,
16
- initial?: R,
17
- ): Promise<R | null> => {
18
- const reader = stream.getReader();
19
-
20
- let acc = (initial ?? null) as R | null | T;
21
-
22
- while (true) {
23
- const { done, value } = await reader.read();
24
- if (done) break;
25
- if (acc === null) {
26
- acc = value;
27
- continue;
28
- }
29
-
30
- acc = reducer(acc, value);
31
- }
32
-
33
- return acc as R;
34
- };
35
-
36
- export const to = <T>(value: T): ReadableStream<T> => {
37
- const stream = new ReadableStream<T>({
38
- start: (controller) => {
39
- controller.enqueue(value);
40
- controller.close();
41
- },
42
- });
43
-
44
- return stream;
45
- };
46
-
47
- export const transform = <T, R>(
48
- stream: ReadableStream<T>,
49
- transformer: (value: T) => R,
50
- ): ReadableStream<R> => {
51
- const reader = stream.getReader();
52
-
53
- return new ReadableStream<R>({
54
- pull: async (controller) => {
55
- const { done, value } = await reader.read();
56
- if (!done) {
57
- controller.enqueue(transformer(value));
58
- } else {
59
- controller.close();
60
- }
61
- },
62
- });
63
- };
64
- }
1
+ export namespace StreamUtil {
2
+ export const readAll = async <T>(stream: ReadableStream<T>): Promise<T[]> => {
3
+ const reader = stream.getReader();
4
+ const result: T[] = [];
5
+ while (true) {
6
+ const { done, value } = await reader.read();
7
+ if (done) break;
8
+ result.push(value);
9
+ }
10
+ return result;
11
+ };
12
+
13
+ export const reduce = async <T, R = T>(
14
+ stream: ReadableStream<T>,
15
+ reducer: (acc: T | R, cur: T) => R,
16
+ initial?: R,
17
+ ): Promise<R | null> => {
18
+ const reader = stream.getReader();
19
+
20
+ let acc = (initial ?? null) as R | null | T;
21
+
22
+ while (true) {
23
+ const { done, value } = await reader.read();
24
+ if (done) break;
25
+ if (acc === null) {
26
+ acc = value;
27
+ continue;
28
+ }
29
+
30
+ acc = reducer(acc, value);
31
+ }
32
+
33
+ return acc as R;
34
+ };
35
+
36
+ export const to = <T>(value: T): ReadableStream<T> => {
37
+ const stream = new ReadableStream<T>({
38
+ start: (controller) => {
39
+ controller.enqueue(value);
40
+ controller.close();
41
+ },
42
+ });
43
+
44
+ return stream;
45
+ };
46
+
47
+ export const transform = <T, R>(
48
+ stream: ReadableStream<T>,
49
+ transformer: (value: T) => R,
50
+ ): ReadableStream<R> => {
51
+ const reader = stream.getReader();
52
+
53
+ return new ReadableStream<R>({
54
+ pull: async (controller) => {
55
+ const { done, value } = await reader.read();
56
+ if (!done) {
57
+ controller.enqueue(transformer(value));
58
+ } else {
59
+ controller.close();
60
+ }
61
+ },
62
+ });
63
+ };
64
+ }
@@ -1,15 +1,15 @@
1
- /**
2
- * @internal
3
- */
4
- export const __map_take = <Key, T>(
5
- dict: Map<Key, T>,
6
- key: Key,
7
- generator: () => T,
8
- ): T => {
9
- const oldbie: T | undefined = dict.get(key);
10
- if (oldbie) return oldbie;
11
-
12
- const value: T = generator();
13
- dict.set(key, value);
14
- return value;
15
- };
1
+ /**
2
+ * @internal
3
+ */
4
+ export const __map_take = <Key, T>(
5
+ dict: Map<Key, T>,
6
+ key: Key,
7
+ generator: () => T,
8
+ ): T => {
9
+ const oldbie: T | undefined = dict.get(key);
10
+ if (oldbie) return oldbie;
11
+
12
+ const value: T = generator();
13
+ dict.set(key, value);
14
+ return value;
15
+ };
@@ -1,178 +1,178 @@
1
- import OpenAI from "openai";
2
-
3
- import { AgenticaEventSource } from "../events/AgenticaEventSource";
4
- import { IAgenticaOperationJson } from "./IAgenticaOperationJson";
5
- import { IAgenticaOperationSelectionJson } from "./IAgenticaOperationSelectionJson";
6
- import { IAgenticaPromptJson } from "./IAgenticaPromptJson";
7
-
8
- /**
9
- * Nestia A.I. chatbot event.
10
- *
11
- * `IAgenticaEventJson` is an union type of all possible events that can
12
- * be emitted by the A.I. chatbot of the {@link Agentica} class. You
13
- * can discriminate the subtype by checking the {@link type} property.
14
- *
15
- * @author Samchon
16
- */
17
- export type IAgenticaEventJson =
18
- | IAgenticaEventJson.ICall
19
- | IAgenticaEventJson.ICancel
20
- | IAgenticaEventJson.IDescribe
21
- | IAgenticaEventJson.IExecute
22
- | IAgenticaEventJson.IInitialize
23
- | IAgenticaEventJson.IRequest
24
- | IAgenticaEventJson.ISelect
25
- | IAgenticaEventJson.IText;
26
- export namespace IAgenticaEventJson {
27
- export type Type = IAgenticaEventJson["type"];
28
- export type Mapper = {
29
- initialize: IInitialize;
30
- select: ISelect;
31
- cancel: ICancel;
32
- call: ICall;
33
- execute: IExecute;
34
- describe: IDescribe;
35
- text: IText;
36
- request: IRequest;
37
- };
38
-
39
- /**
40
- * Event of initializing the chatbot.
41
- */
42
- // eslint-disable-next-line @typescript-eslint/no-empty-object-type
43
- export interface IInitialize extends IBase<"initialize"> {}
44
-
45
- /**
46
- * Event of selecting a function to call.
47
- */
48
- export interface ISelect extends IBase<"select"> {
49
- selection: IAgenticaOperationSelectionJson;
50
- }
51
-
52
- /**
53
- * Event of canceling a function calling.
54
- */
55
- export interface ICancel extends IBase<"cancel"> {
56
- selection: IAgenticaOperationSelectionJson;
57
- }
58
-
59
- /**
60
- * Event of calling a function.
61
- */
62
- export interface ICall extends IBase<"call"> {
63
- /**
64
- * ID of the tool calling.
65
- */
66
- id: string;
67
-
68
- /**
69
- * Target operation to call.
70
- */
71
- operation: IAgenticaOperationJson;
72
-
73
- /**
74
- * Arguments of the function calling.
75
- *
76
- * If you modify this {@link arguments} property, it actually modifies
77
- * the backend server's request. Therefore, be careful when you're
78
- * trying to modify this property.
79
- */
80
- arguments: Record<string, any>;
81
- }
82
-
83
- /**
84
- * Event of function calling execution.
85
- */
86
- export interface IExecute extends IBase<"execute"> {
87
- /**
88
- * ID of the tool calling.
89
- */
90
- id: string;
91
-
92
- /**
93
- * Target operation had called.
94
- */
95
- operation: IAgenticaOperationJson;
96
-
97
- /**
98
- * Arguments of the function calling.
99
- */
100
- arguments: object;
101
-
102
- /**
103
- * Return value.
104
- */
105
- value: any;
106
- }
107
-
108
- /**
109
- * Event of description.
110
- *
111
- * Event describing return values of LLM function callings.
112
- */
113
- export interface IDescribe extends IBase<"describe"> {
114
- /**
115
- * Executions of the LLM function calling.
116
- *
117
- * This prompt describes the return value of them.
118
- */
119
- executes: IAgenticaPromptJson.IExecute[];
120
-
121
- /**
122
- * Description text.
123
- */
124
- text: string;
125
-
126
- /**
127
- * Whether the streaming is completed or not.
128
- */
129
- done: boolean;
130
- }
131
-
132
- /**
133
- * Event of text message.
134
- */
135
- export interface IText extends IBase<"text"> {
136
- /**
137
- * Role of the orator.
138
- */
139
- role: "assistant" | "user";
140
-
141
- /**
142
- * Conversation text.
143
- */
144
- text: string;
145
-
146
- /**
147
- * Whether the streaming is completed or not.
148
- */
149
- done: boolean;
150
- }
151
-
152
- /**
153
- * Request event of LLM vendor API.
154
- */
155
- export interface IRequest extends IBase<"request"> {
156
- /**
157
- * The source agent of the request.
158
- */
159
- source: AgenticaEventSource;
160
-
161
- /**
162
- * Request body.
163
- */
164
- body: OpenAI.ChatCompletionCreateParamsStreaming;
165
-
166
- /**
167
- * Options for the request.
168
- */
169
- options?: OpenAI.RequestOptions | undefined;
170
- }
171
-
172
- interface IBase<Type extends string> {
173
- /**
174
- * Discriminator type.
175
- */
176
- type: Type;
177
- }
178
- }
1
+ import OpenAI from "openai";
2
+
3
+ import { AgenticaEventSource } from "../events/AgenticaEventSource";
4
+ import { IAgenticaOperationJson } from "./IAgenticaOperationJson";
5
+ import { IAgenticaOperationSelectionJson } from "./IAgenticaOperationSelectionJson";
6
+ import { IAgenticaPromptJson } from "./IAgenticaPromptJson";
7
+
8
+ /**
9
+ * Nestia A.I. chatbot event.
10
+ *
11
+ * `IAgenticaEventJson` is an union type of all possible events that can
12
+ * be emitted by the A.I. chatbot of the {@link Agentica} class. You
13
+ * can discriminate the subtype by checking the {@link type} property.
14
+ *
15
+ * @author Samchon
16
+ */
17
+ export type IAgenticaEventJson =
18
+ | IAgenticaEventJson.ICall
19
+ | IAgenticaEventJson.ICancel
20
+ | IAgenticaEventJson.IDescribe
21
+ | IAgenticaEventJson.IExecute
22
+ | IAgenticaEventJson.IInitialize
23
+ | IAgenticaEventJson.IRequest
24
+ | IAgenticaEventJson.ISelect
25
+ | IAgenticaEventJson.IText;
26
+ export namespace IAgenticaEventJson {
27
+ export type Type = IAgenticaEventJson["type"];
28
+ export type Mapper = {
29
+ initialize: IInitialize;
30
+ select: ISelect;
31
+ cancel: ICancel;
32
+ call: ICall;
33
+ execute: IExecute;
34
+ describe: IDescribe;
35
+ text: IText;
36
+ request: IRequest;
37
+ };
38
+
39
+ /**
40
+ * Event of initializing the chatbot.
41
+ */
42
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
43
+ export interface IInitialize extends IBase<"initialize"> {}
44
+
45
+ /**
46
+ * Event of selecting a function to call.
47
+ */
48
+ export interface ISelect extends IBase<"select"> {
49
+ selection: IAgenticaOperationSelectionJson;
50
+ }
51
+
52
+ /**
53
+ * Event of canceling a function calling.
54
+ */
55
+ export interface ICancel extends IBase<"cancel"> {
56
+ selection: IAgenticaOperationSelectionJson;
57
+ }
58
+
59
+ /**
60
+ * Event of calling a function.
61
+ */
62
+ export interface ICall extends IBase<"call"> {
63
+ /**
64
+ * ID of the tool calling.
65
+ */
66
+ id: string;
67
+
68
+ /**
69
+ * Target operation to call.
70
+ */
71
+ operation: IAgenticaOperationJson;
72
+
73
+ /**
74
+ * Arguments of the function calling.
75
+ *
76
+ * If you modify this {@link arguments} property, it actually modifies
77
+ * the backend server's request. Therefore, be careful when you're
78
+ * trying to modify this property.
79
+ */
80
+ arguments: Record<string, any>;
81
+ }
82
+
83
+ /**
84
+ * Event of function calling execution.
85
+ */
86
+ export interface IExecute extends IBase<"execute"> {
87
+ /**
88
+ * ID of the tool calling.
89
+ */
90
+ id: string;
91
+
92
+ /**
93
+ * Target operation had called.
94
+ */
95
+ operation: IAgenticaOperationJson;
96
+
97
+ /**
98
+ * Arguments of the function calling.
99
+ */
100
+ arguments: object;
101
+
102
+ /**
103
+ * Return value.
104
+ */
105
+ value: any;
106
+ }
107
+
108
+ /**
109
+ * Event of description.
110
+ *
111
+ * Event describing return values of LLM function callings.
112
+ */
113
+ export interface IDescribe extends IBase<"describe"> {
114
+ /**
115
+ * Executions of the LLM function calling.
116
+ *
117
+ * This prompt describes the return value of them.
118
+ */
119
+ executes: IAgenticaPromptJson.IExecute[];
120
+
121
+ /**
122
+ * Description text.
123
+ */
124
+ text: string;
125
+
126
+ /**
127
+ * Whether the streaming is completed or not.
128
+ */
129
+ done: boolean;
130
+ }
131
+
132
+ /**
133
+ * Event of text message.
134
+ */
135
+ export interface IText extends IBase<"text"> {
136
+ /**
137
+ * Role of the orator.
138
+ */
139
+ role: "assistant" | "user";
140
+
141
+ /**
142
+ * Conversation text.
143
+ */
144
+ text: string;
145
+
146
+ /**
147
+ * Whether the streaming is completed or not.
148
+ */
149
+ done: boolean;
150
+ }
151
+
152
+ /**
153
+ * Request event of LLM vendor API.
154
+ */
155
+ export interface IRequest extends IBase<"request"> {
156
+ /**
157
+ * The source agent of the request.
158
+ */
159
+ source: AgenticaEventSource;
160
+
161
+ /**
162
+ * Request body.
163
+ */
164
+ body: OpenAI.ChatCompletionCreateParamsStreaming;
165
+
166
+ /**
167
+ * Options for the request.
168
+ */
169
+ options?: OpenAI.RequestOptions | undefined;
170
+ }
171
+
172
+ interface IBase<Type extends string> {
173
+ /**
174
+ * Discriminator type.
175
+ */
176
+ type: Type;
177
+ }
178
+ }
@@ -1,36 +1,36 @@
1
- /**
2
- * Operation information in the Agentica Agent.
3
- *
4
- * `IAgenticaOperation` is a type represents an operation that would
5
- * be selected by the A.I. chatbot of {@link Agentica} class to
6
- * perform the LLM (Large Language Model) function calling.
7
- *
8
- * Also, it is an union type that is discriminated by the {@link protocol}
9
- * property. If the protocol value is `http`, it means that the HTTP API
10
- * operation would be called by the A.I. chatbot. Otherwise, if the protocol
11
- * value is `class`, it means that the operation has come from a
12
- * TypeScript class.
13
- *
14
- * @author Samchon
15
- */
16
- export interface IAgenticaOperationJson {
17
- /**
18
- * Protocol discriminator.
19
- */
20
- protocol: "class" | "http";
21
-
22
- /**
23
- * Belonged controller of the target function.
24
- */
25
- controller: string;
26
-
27
- /**
28
- * Target function to call.
29
- */
30
- function: string;
31
-
32
- /**
33
- * Identifier name.
34
- */
35
- name: string;
36
- }
1
+ /**
2
+ * Operation information in the Agentica Agent.
3
+ *
4
+ * `IAgenticaOperation` is a type represents an operation that would
5
+ * be selected by the A.I. chatbot of {@link Agentica} class to
6
+ * perform the LLM (Large Language Model) function calling.
7
+ *
8
+ * Also, it is an union type that is discriminated by the {@link protocol}
9
+ * property. If the protocol value is `http`, it means that the HTTP API
10
+ * operation would be called by the A.I. chatbot. Otherwise, if the protocol
11
+ * value is `class`, it means that the operation has come from a
12
+ * TypeScript class.
13
+ *
14
+ * @author Samchon
15
+ */
16
+ export interface IAgenticaOperationJson {
17
+ /**
18
+ * Protocol discriminator.
19
+ */
20
+ protocol: "class" | "http";
21
+
22
+ /**
23
+ * Belonged controller of the target function.
24
+ */
25
+ controller: string;
26
+
27
+ /**
28
+ * Target function to call.
29
+ */
30
+ function: string;
31
+
32
+ /**
33
+ * Identifier name.
34
+ */
35
+ name: string;
36
+ }
@@ -1,19 +1,19 @@
1
- import { IAgenticaOperationJson } from "./IAgenticaOperationJson";
2
-
3
- /**
4
- * Nestia agent operation selection.
5
- *
6
- * `IAgenticaOperationSelection` is a type represents an operation
7
- * which has been selected by the A.I. chatbot of {@link Agentica}
8
- * class for the LLM (Large Language Model) function calling with
9
- * detailed {@link reason} of the selection (or cancellation).
10
- *
11
- * Also, `IAgenticaOperationSelection` is an union type that can
12
- * specify a subtype by checking the {@link protocol} property.
13
- *
14
- * @author Samchon
15
- */
16
- export interface IAgenticaOperationSelectionJson {
17
- reason: string;
18
- operation: IAgenticaOperationJson;
19
- }
1
+ import { IAgenticaOperationJson } from "./IAgenticaOperationJson";
2
+
3
+ /**
4
+ * Nestia agent operation selection.
5
+ *
6
+ * `IAgenticaOperationSelection` is a type represents an operation
7
+ * which has been selected by the A.I. chatbot of {@link Agentica}
8
+ * class for the LLM (Large Language Model) function calling with
9
+ * detailed {@link reason} of the selection (or cancellation).
10
+ *
11
+ * Also, `IAgenticaOperationSelection` is an union type that can
12
+ * specify a subtype by checking the {@link protocol} property.
13
+ *
14
+ * @author Samchon
15
+ */
16
+ export interface IAgenticaOperationSelectionJson {
17
+ reason: string;
18
+ operation: IAgenticaOperationJson;
19
+ }