@agentica/core 0.8.1 → 0.8.3-dev.20250227

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 (55) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +404 -404
  3. package/lib/Agentica.d.ts +2 -2
  4. package/lib/Agentica.js +9 -3
  5. package/lib/Agentica.js.map +1 -1
  6. package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -1
  7. package/lib/index.mjs +17 -15
  8. package/lib/index.mjs.map +1 -1
  9. package/lib/internal/AgenticaTokenUsageAggregator.js +7 -9
  10. package/lib/internal/AgenticaTokenUsageAggregator.js.map +1 -1
  11. package/lib/structures/IAgenticaTokenUsage.d.ts +7 -11
  12. package/package.json +1 -1
  13. package/prompts/cancel.md +4 -4
  14. package/prompts/common.md +2 -2
  15. package/prompts/describe.md +6 -6
  16. package/prompts/execute.md +6 -6
  17. package/prompts/initialize.md +2 -2
  18. package/prompts/select.md +6 -6
  19. package/src/Agentica.ts +318 -312
  20. package/src/chatgpt/ChatGptAgent.ts +71 -71
  21. package/src/chatgpt/ChatGptCallFunctionAgent.ts +445 -445
  22. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +283 -283
  23. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +51 -51
  24. package/src/chatgpt/ChatGptHistoryDecoder.ts +87 -86
  25. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -88
  26. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +318 -318
  27. package/src/functional/createHttpLlmApplication.ts +63 -63
  28. package/src/index.ts +19 -19
  29. package/src/internal/AgenticaConstant.ts +4 -4
  30. package/src/internal/AgenticaDefaultPrompt.ts +39 -39
  31. package/src/internal/AgenticaOperationComposer.ts +82 -82
  32. package/src/internal/AgenticaPromptFactory.ts +30 -30
  33. package/src/internal/AgenticaPromptTransformer.ts +83 -83
  34. package/src/internal/AgenticaTokenUsageAggregator.ts +115 -123
  35. package/src/internal/MathUtil.ts +3 -3
  36. package/src/internal/Singleton.ts +22 -22
  37. package/src/internal/__map_take.ts +15 -15
  38. package/src/structures/IAgenticaConfig.ts +121 -121
  39. package/src/structures/IAgenticaContext.ts +128 -128
  40. package/src/structures/IAgenticaController.ts +130 -130
  41. package/src/structures/IAgenticaEvent.ts +224 -224
  42. package/src/structures/IAgenticaExecutor.ts +152 -152
  43. package/src/structures/IAgenticaOperation.ts +64 -64
  44. package/src/structures/IAgenticaOperationCollection.ts +50 -50
  45. package/src/structures/IAgenticaOperationSelection.ts +69 -69
  46. package/src/structures/IAgenticaPrompt.ts +173 -173
  47. package/src/structures/IAgenticaProps.ts +64 -64
  48. package/src/structures/IAgenticaProvider.ts +45 -45
  49. package/src/structures/IAgenticaSystemPrompt.ts +122 -122
  50. package/src/structures/IAgenticaTokenUsage.ts +107 -112
  51. package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -23
  52. package/src/structures/internal/__IChatFunctionReference.ts +21 -21
  53. package/src/structures/internal/__IChatInitialApplication.ts +15 -15
  54. package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -24
  55. package/src/typings/AgenticaSource.ts +6 -6
@@ -1,86 +1,87 @@
1
- import OpenAI from "openai";
2
-
3
- import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
4
-
5
- export namespace ChatGptHistoryDecoder {
6
- export const decode = (
7
- history: IAgenticaPrompt,
8
- ): OpenAI.ChatCompletionMessageParam[] => {
9
- // NO NEED TO DECODE DESCRIBE
10
- if (history.type === "describe") return [];
11
- else if (history.type === "text")
12
- return [
13
- {
14
- role: history.role,
15
- content: history.text,
16
- },
17
- ];
18
- else if (history.type === "select" || history.type === "cancel")
19
- return [
20
- {
21
- role: "assistant",
22
- tool_calls: [
23
- {
24
- type: "function",
25
- id: history.id,
26
- function: {
27
- name: `${history.type}Functions`,
28
- arguments: JSON.stringify({
29
- functions: history.operations.map((t) => ({
30
- name: t.function.name,
31
- reason: t.reason,
32
- })),
33
- }),
34
- },
35
- },
36
- ],
37
- },
38
- {
39
- role: "tool",
40
- tool_call_id: history.id,
41
- content: "",
42
- },
43
- ];
44
- return [
45
- {
46
- role: "assistant",
47
- tool_calls: [
48
- {
49
- type: "function",
50
- id: history.id,
51
- function: {
52
- name: history.function.name,
53
- arguments: JSON.stringify(history.arguments),
54
- },
55
- },
56
- ],
57
- },
58
- {
59
- role: "tool",
60
- tool_call_id: history.id,
61
- content: JSON.stringify({
62
- function: {
63
- protocol: history.protocol,
64
- description: history.function.description,
65
- parameters: history.function.parameters,
66
- output: history.function.output,
67
- ...(history.protocol === "http"
68
- ? {
69
- method: history.function.method,
70
- path: history.function.path,
71
- }
72
- : {}),
73
- },
74
- ...(history.protocol === "http"
75
- ? {
76
- status: history.value.status,
77
- data: history.value.body,
78
- }
79
- : {
80
- value: history.value,
81
- }),
82
- }),
83
- },
84
- ];
85
- };
86
- }
1
+ import OpenAI from "openai";
2
+
3
+ import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
4
+
5
+ export namespace ChatGptHistoryDecoder {
6
+ export const decode = (
7
+ history: IAgenticaPrompt,
8
+ ): OpenAI.ChatCompletionMessageParam[] => {
9
+ // NO NEED TO DECODE DESCRIBE
10
+ if (history.type === "describe") return [];
11
+ else if (history.type === "text")
12
+ return [
13
+ {
14
+ role: history.role,
15
+ content: history.text,
16
+ },
17
+ ];
18
+ else if (history.type === "select" || history.type === "cancel")
19
+ return [
20
+ {
21
+ role: "assistant",
22
+ tool_calls: [
23
+ {
24
+ type: "function",
25
+ id: history.id,
26
+ function: {
27
+ name: `${history.type}Functions`,
28
+ arguments: JSON.stringify({
29
+ functions: history.operations.map((t) => ({
30
+ name: t.function.name,
31
+ reason: t.reason,
32
+ })),
33
+ }),
34
+ },
35
+ },
36
+ ],
37
+ },
38
+ {
39
+ role: "tool",
40
+ tool_call_id: history.id,
41
+ content: "",
42
+ },
43
+ ];
44
+
45
+ return [
46
+ {
47
+ role: "assistant",
48
+ tool_calls: [
49
+ {
50
+ type: "function",
51
+ id: history.id,
52
+ function: {
53
+ name: history.function.name,
54
+ arguments: JSON.stringify(history.arguments),
55
+ },
56
+ },
57
+ ],
58
+ },
59
+ {
60
+ role: "tool",
61
+ tool_call_id: history.id,
62
+ content: JSON.stringify({
63
+ function: {
64
+ protocol: history.protocol,
65
+ description: history.function.description,
66
+ parameters: history.function.parameters,
67
+ output: history.function.output,
68
+ ...(history.protocol === "http"
69
+ ? {
70
+ method: history.function.method,
71
+ path: history.function.path,
72
+ }
73
+ : {}),
74
+ },
75
+ ...(history.protocol === "http"
76
+ ? {
77
+ status: history.value.status,
78
+ data: history.value.body,
79
+ }
80
+ : {
81
+ value: history.value,
82
+ }),
83
+ }),
84
+ },
85
+ ];
86
+ };
87
+ }
@@ -1,88 +1,88 @@
1
- import { ILlmFunction } from "@samchon/openapi";
2
- import OpenAI from "openai";
3
- import typia from "typia";
4
-
5
- import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
6
- import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
7
- import { IAgenticaContext } from "../structures/IAgenticaContext";
8
- import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
9
- import { __IChatInitialApplication } from "../structures/internal/__IChatInitialApplication";
10
- import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
11
-
12
- export namespace ChatGptInitializeFunctionAgent {
13
- export const execute = async (
14
- ctx: IAgenticaContext,
15
- ): Promise<IAgenticaPrompt[]> => {
16
- //----
17
- // EXECUTE CHATGPT API
18
- //----
19
- const completion: OpenAI.ChatCompletion = await ctx.request("initialize", {
20
- messages: [
21
- // COMMON SYSTEM PROMPT
22
- {
23
- role: "system",
24
- content: AgenticaDefaultPrompt.write(ctx.config),
25
- } satisfies OpenAI.ChatCompletionSystemMessageParam,
26
- // PREVIOUS HISTORIES
27
- ...ctx.histories.map(ChatGptHistoryDecoder.decode).flat(),
28
- // USER INPUT
29
- {
30
- role: "user",
31
- content: ctx.prompt.text,
32
- },
33
- {
34
- // SYSTEM PROMPT
35
- role: "system",
36
- content:
37
- ctx.config?.systemPrompt?.initialize?.(ctx.histories) ??
38
- AgenticaSystemPrompt.INITIALIZE,
39
- },
40
- ],
41
- // GETTER FUNCTION
42
- tools: [
43
- {
44
- type: "function",
45
- function: {
46
- name: FUNCTION.name,
47
- description: FUNCTION.description,
48
- parameters: FUNCTION.parameters as any,
49
- },
50
- },
51
- ],
52
- tool_choice: "auto",
53
- parallel_tool_calls: false,
54
- });
55
-
56
- //----
57
- // PROCESS COMPLETION
58
- //----
59
- const prompts: IAgenticaPrompt[] = [];
60
- for (const choice of completion.choices) {
61
- if (
62
- choice.message.role === "assistant" &&
63
- !!choice.message.content?.length
64
- )
65
- prompts.push({
66
- type: "text",
67
- role: "assistant",
68
- text: choice.message.content,
69
- });
70
- }
71
- if (
72
- completion.choices.some(
73
- (c) =>
74
- !!c.message.tool_calls?.some(
75
- (tc) =>
76
- tc.type === "function" && tc.function.name === FUNCTION.name,
77
- ),
78
- )
79
- )
80
- await ctx.initialize();
81
- return prompts;
82
- };
83
- }
84
-
85
- const FUNCTION: ILlmFunction<"chatgpt"> = typia.llm.application<
86
- __IChatInitialApplication,
87
- "chatgpt"
88
- >().functions[0]!;
1
+ import { ILlmFunction } from "@samchon/openapi";
2
+ import OpenAI from "openai";
3
+ import typia from "typia";
4
+
5
+ import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
6
+ import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
7
+ import { IAgenticaContext } from "../structures/IAgenticaContext";
8
+ import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
9
+ import { __IChatInitialApplication } from "../structures/internal/__IChatInitialApplication";
10
+ import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
11
+
12
+ export namespace ChatGptInitializeFunctionAgent {
13
+ export const execute = async (
14
+ ctx: IAgenticaContext,
15
+ ): Promise<IAgenticaPrompt[]> => {
16
+ //----
17
+ // EXECUTE CHATGPT API
18
+ //----
19
+ const completion: OpenAI.ChatCompletion = await ctx.request("initialize", {
20
+ messages: [
21
+ // COMMON SYSTEM PROMPT
22
+ {
23
+ role: "system",
24
+ content: AgenticaDefaultPrompt.write(ctx.config),
25
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
26
+ // PREVIOUS HISTORIES
27
+ ...ctx.histories.map(ChatGptHistoryDecoder.decode).flat(),
28
+ // USER INPUT
29
+ {
30
+ role: "user",
31
+ content: ctx.prompt.text,
32
+ },
33
+ {
34
+ // SYSTEM PROMPT
35
+ role: "system",
36
+ content:
37
+ ctx.config?.systemPrompt?.initialize?.(ctx.histories) ??
38
+ AgenticaSystemPrompt.INITIALIZE,
39
+ },
40
+ ],
41
+ // GETTER FUNCTION
42
+ tools: [
43
+ {
44
+ type: "function",
45
+ function: {
46
+ name: FUNCTION.name,
47
+ description: FUNCTION.description,
48
+ parameters: FUNCTION.parameters as any,
49
+ },
50
+ },
51
+ ],
52
+ tool_choice: "auto",
53
+ parallel_tool_calls: false,
54
+ });
55
+
56
+ //----
57
+ // PROCESS COMPLETION
58
+ //----
59
+ const prompts: IAgenticaPrompt[] = [];
60
+ for (const choice of completion.choices) {
61
+ if (
62
+ choice.message.role === "assistant" &&
63
+ !!choice.message.content?.length
64
+ )
65
+ prompts.push({
66
+ type: "text",
67
+ role: "assistant",
68
+ text: choice.message.content,
69
+ });
70
+ }
71
+ if (
72
+ completion.choices.some(
73
+ (c) =>
74
+ !!c.message.tool_calls?.some(
75
+ (tc) =>
76
+ tc.type === "function" && tc.function.name === FUNCTION.name,
77
+ ),
78
+ )
79
+ )
80
+ await ctx.initialize();
81
+ return prompts;
82
+ };
83
+ }
84
+
85
+ const FUNCTION: ILlmFunction<"chatgpt"> = typia.llm.application<
86
+ __IChatInitialApplication,
87
+ "chatgpt"
88
+ >().functions[0]!;