@agentica/core 0.7.0-dev.20250224-5 → 0.7.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.
Files changed (50) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +404 -464
  3. package/lib/chatgpt/ChatGptSelectFunctionAgent.js +5 -5
  4. package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -1
  5. package/lib/index.mjs +1 -1
  6. package/lib/index.mjs.map +1 -1
  7. package/package.json +2 -1
  8. package/prompts/cancel.md +4 -4
  9. package/prompts/common.md +2 -2
  10. package/prompts/describe.md +6 -6
  11. package/prompts/execute.md +6 -6
  12. package/prompts/initialize.md +2 -2
  13. package/prompts/select.md +6 -6
  14. package/src/Agentica.ts +322 -322
  15. package/src/chatgpt/ChatGptAgent.ts +71 -71
  16. package/src/chatgpt/ChatGptCallFunctionAgent.ts +445 -445
  17. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +283 -283
  18. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +51 -51
  19. package/src/chatgpt/ChatGptHistoryDecoder.ts +86 -86
  20. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -88
  21. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +318 -316
  22. package/src/functional/createHttpLlmApplication.ts +63 -63
  23. package/src/index.ts +19 -19
  24. package/src/internal/AgenticaConstant.ts +4 -4
  25. package/src/internal/AgenticaCostAggregator.ts +35 -35
  26. package/src/internal/AgenticaDefaultPrompt.ts +39 -39
  27. package/src/internal/AgenticaOperationComposer.ts +82 -82
  28. package/src/internal/AgenticaPromptFactory.ts +30 -30
  29. package/src/internal/AgenticaPromptTransformer.ts +83 -83
  30. package/src/internal/MathUtil.ts +3 -3
  31. package/src/internal/Singleton.ts +22 -22
  32. package/src/internal/__map_take.ts +15 -15
  33. package/src/structures/IAgenticaConfig.ts +121 -121
  34. package/src/structures/IAgenticaContext.ts +128 -128
  35. package/src/structures/IAgenticaController.ts +130 -130
  36. package/src/structures/IAgenticaEvent.ts +224 -224
  37. package/src/structures/IAgenticaExecutor.ts +152 -152
  38. package/src/structures/IAgenticaOperation.ts +64 -64
  39. package/src/structures/IAgenticaOperationCollection.ts +50 -50
  40. package/src/structures/IAgenticaOperationSelection.ts +69 -69
  41. package/src/structures/IAgenticaPrompt.ts +173 -173
  42. package/src/structures/IAgenticaProps.ts +64 -64
  43. package/src/structures/IAgenticaProvider.ts +45 -45
  44. package/src/structures/IAgenticaSystemPrompt.ts +122 -122
  45. package/src/structures/IAgenticaTokenUsage.ts +52 -52
  46. package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -23
  47. package/src/structures/internal/__IChatFunctionReference.ts +21 -21
  48. package/src/structures/internal/__IChatInitialApplication.ts +15 -15
  49. package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -24
  50. package/src/typings/AgenticaSource.ts +6 -6
@@ -1,63 +1,63 @@
1
- import {
2
- HttpLlm,
3
- IHttpLlmApplication,
4
- OpenApi,
5
- OpenApiV3,
6
- OpenApiV3_1,
7
- SwaggerV2,
8
- } from "@samchon/openapi";
9
- import typia, { IValidation } from "typia";
10
-
11
- /**
12
- * Create an HTTP LLM application instance.
13
- *
14
- * Create an {@link IHttpLlmApplication} instance which represents
15
- * the LLM (Large Language Model) function calling application schema
16
- * from the given Swagger/OpenAPI document and the target LLM model.
17
- *
18
- * By the way, even though this `createHttpLlmApplication` function
19
- * supports every version of Swagger/OpenAPI specification, there can
20
- * be a type error in the given document. In that case, the function
21
- * will return {@link IValidation.IFailure} instance with detailed
22
- * type error tracing information.
23
- *
24
- * @param props Properties to create the HTTP LLM application instance
25
- * @returns Validation result of the HTTP LLM application composition
26
- * @author Samchon
27
- */
28
- export const createHttpLlmApplication = (props: {
29
- /**
30
- * Target LLM model.
31
- */
32
- model: "chatgpt";
33
-
34
- /**
35
- * Swagger/OpenAPI document.
36
- */
37
- document:
38
- | SwaggerV2.IDocument
39
- | OpenApiV3.IDocument
40
- | OpenApiV3_1.IDocument
41
- | OpenApi.IDocument;
42
-
43
- /**
44
- * Options for the LLM function calling schema composition.
45
- */
46
- options?: Partial<IHttpLlmApplication.IOptions<"chatgpt">>;
47
- }): IValidation<IHttpLlmApplication<"chatgpt">> => {
48
- const inspect: IValidation<
49
- | SwaggerV2.IDocument
50
- | OpenApiV3.IDocument
51
- | OpenApiV3_1.IDocument
52
- | OpenApi.IDocument
53
- > = typia.validate(props.document);
54
- if (inspect.success === false) return inspect;
55
- return {
56
- success: true,
57
- data: HttpLlm.application({
58
- model: props.model,
59
- document: OpenApi.convert(props.document),
60
- options: props.options,
61
- }),
62
- };
63
- };
1
+ import {
2
+ HttpLlm,
3
+ IHttpLlmApplication,
4
+ OpenApi,
5
+ OpenApiV3,
6
+ OpenApiV3_1,
7
+ SwaggerV2,
8
+ } from "@samchon/openapi";
9
+ import typia, { IValidation } from "typia";
10
+
11
+ /**
12
+ * Create an HTTP LLM application instance.
13
+ *
14
+ * Create an {@link IHttpLlmApplication} instance which represents
15
+ * the LLM (Large Language Model) function calling application schema
16
+ * from the given Swagger/OpenAPI document and the target LLM model.
17
+ *
18
+ * By the way, even though this `createHttpLlmApplication` function
19
+ * supports every version of Swagger/OpenAPI specification, there can
20
+ * be a type error in the given document. In that case, the function
21
+ * will return {@link IValidation.IFailure} instance with detailed
22
+ * type error tracing information.
23
+ *
24
+ * @param props Properties to create the HTTP LLM application instance
25
+ * @returns Validation result of the HTTP LLM application composition
26
+ * @author Samchon
27
+ */
28
+ export const createHttpLlmApplication = (props: {
29
+ /**
30
+ * Target LLM model.
31
+ */
32
+ model: "chatgpt";
33
+
34
+ /**
35
+ * Swagger/OpenAPI document.
36
+ */
37
+ document:
38
+ | SwaggerV2.IDocument
39
+ | OpenApiV3.IDocument
40
+ | OpenApiV3_1.IDocument
41
+ | OpenApi.IDocument;
42
+
43
+ /**
44
+ * Options for the LLM function calling schema composition.
45
+ */
46
+ options?: Partial<IHttpLlmApplication.IOptions<"chatgpt">>;
47
+ }): IValidation<IHttpLlmApplication<"chatgpt">> => {
48
+ const inspect: IValidation<
49
+ | SwaggerV2.IDocument
50
+ | OpenApiV3.IDocument
51
+ | OpenApiV3_1.IDocument
52
+ | OpenApi.IDocument
53
+ > = typia.validate(props.document);
54
+ if (inspect.success === false) return inspect;
55
+ return {
56
+ success: true,
57
+ data: HttpLlm.application({
58
+ model: props.model,
59
+ document: OpenApi.convert(props.document),
60
+ options: props.options,
61
+ }),
62
+ };
63
+ };
package/src/index.ts CHANGED
@@ -1,19 +1,19 @@
1
- // TYPES
2
- export * from "./structures/IAgenticaConfig";
3
- export * from "./structures/IAgenticaContext";
4
- export * from "./structures/IAgenticaController";
5
- export * from "./structures/IAgenticaEvent";
6
- export * from "./structures/IAgenticaExecutor";
7
- export * from "./structures/IAgenticaOperation";
8
- export * from "./structures/IAgenticaOperationCollection";
9
- export * from "./structures/IAgenticaOperationSelection";
10
- export * from "./structures/IAgenticaPrompt";
11
- export * from "./structures/IAgenticaProps";
12
- export * from "./structures/IAgenticaProvider";
13
- export * from "./structures/IAgenticaSystemPrompt";
14
- export * from "./structures/IAgenticaTokenUsage";
15
- export * from "./typings/AgenticaSource";
16
-
17
- // FACADE CLASS
18
- export * from "./functional/createHttpLlmApplication";
19
- export * from "./Agentica";
1
+ // TYPES
2
+ export * from "./structures/IAgenticaConfig";
3
+ export * from "./structures/IAgenticaContext";
4
+ export * from "./structures/IAgenticaController";
5
+ export * from "./structures/IAgenticaEvent";
6
+ export * from "./structures/IAgenticaExecutor";
7
+ export * from "./structures/IAgenticaOperation";
8
+ export * from "./structures/IAgenticaOperationCollection";
9
+ export * from "./structures/IAgenticaOperationSelection";
10
+ export * from "./structures/IAgenticaPrompt";
11
+ export * from "./structures/IAgenticaProps";
12
+ export * from "./structures/IAgenticaProvider";
13
+ export * from "./structures/IAgenticaSystemPrompt";
14
+ export * from "./structures/IAgenticaTokenUsage";
15
+ export * from "./typings/AgenticaSource";
16
+
17
+ // FACADE CLASS
18
+ export * from "./functional/createHttpLlmApplication";
19
+ export * from "./Agentica";
@@ -1,4 +1,4 @@
1
- export namespace AgenticaConstant {
2
- export const RETRY = 3;
3
- export const ELITICISM = true;
4
- }
1
+ export namespace AgenticaConstant {
2
+ export const RETRY = 3;
3
+ export const ELITICISM = true;
4
+ }
@@ -1,35 +1,35 @@
1
- import OpenAI from "openai";
2
-
3
- import { IAgenticaTokenUsage } from "../structures/IAgenticaTokenUsage";
4
-
5
- export namespace AgenticaCostAggregator {
6
- export const aggregate = (
7
- cost: IAgenticaTokenUsage,
8
- completion: OpenAI.ChatCompletion,
9
- ): void => {
10
- if (!completion.usage) return;
11
-
12
- // TOTAL
13
- cost.total += completion.usage.total_tokens;
14
-
15
- // PROMPT
16
- cost.prompt.total += completion.usage.prompt_tokens;
17
- cost.prompt.audio +=
18
- completion.usage.prompt_tokens_details?.audio_tokens ?? 0;
19
- cost.prompt.cached +=
20
- completion.usage.prompt_tokens_details?.cached_tokens ?? 0;
21
-
22
- // COMPLETION
23
- cost.completion.total += completion.usage.total_tokens;
24
- cost.completion.accepted_prediction +=
25
- completion.usage.completion_tokens_details?.accepted_prediction_tokens ??
26
- 0;
27
- cost.completion.audio +=
28
- completion.usage.completion_tokens_details?.audio_tokens ?? 0;
29
- cost.completion.reasoning +=
30
- completion.usage.completion_tokens_details?.reasoning_tokens ?? 0;
31
- cost.completion.rejected_prediction +=
32
- completion.usage.completion_tokens_details?.rejected_prediction_tokens ??
33
- 0;
34
- };
35
- }
1
+ import OpenAI from "openai";
2
+
3
+ import { IAgenticaTokenUsage } from "../structures/IAgenticaTokenUsage";
4
+
5
+ export namespace AgenticaCostAggregator {
6
+ export const aggregate = (
7
+ cost: IAgenticaTokenUsage,
8
+ completion: OpenAI.ChatCompletion,
9
+ ): void => {
10
+ if (!completion.usage) return;
11
+
12
+ // TOTAL
13
+ cost.total += completion.usage.total_tokens;
14
+
15
+ // PROMPT
16
+ cost.prompt.total += completion.usage.prompt_tokens;
17
+ cost.prompt.audio +=
18
+ completion.usage.prompt_tokens_details?.audio_tokens ?? 0;
19
+ cost.prompt.cached +=
20
+ completion.usage.prompt_tokens_details?.cached_tokens ?? 0;
21
+
22
+ // COMPLETION
23
+ cost.completion.total += completion.usage.total_tokens;
24
+ cost.completion.accepted_prediction +=
25
+ completion.usage.completion_tokens_details?.accepted_prediction_tokens ??
26
+ 0;
27
+ cost.completion.audio +=
28
+ completion.usage.completion_tokens_details?.audio_tokens ?? 0;
29
+ cost.completion.reasoning +=
30
+ completion.usage.completion_tokens_details?.reasoning_tokens ?? 0;
31
+ cost.completion.rejected_prediction +=
32
+ completion.usage.completion_tokens_details?.rejected_prediction_tokens ??
33
+ 0;
34
+ };
35
+ }
@@ -1,39 +1,39 @@
1
- import { IAgenticaConfig } from "../structures/IAgenticaConfig";
2
- import { AgenticaSystemPrompt } from "./AgenticaSystemPrompt";
3
- import { Singleton } from "./Singleton";
4
-
5
- export namespace AgenticaDefaultPrompt {
6
- export const write = (config?: IAgenticaConfig): string => {
7
- if (config?.systemPrompt?.common)
8
- return config?.systemPrompt?.common(config);
9
-
10
- const locale: string = config?.locale ?? getLocale.get();
11
- const timezone: string = config?.timezone ?? getTimezone.get();
12
-
13
- return AgenticaSystemPrompt.COMMON.replace("${locale}", locale).replace(
14
- "${timezone}",
15
- timezone,
16
- );
17
- };
18
- }
19
-
20
- const getLocale = new Singleton(() =>
21
- isNode.get()
22
- ? (process.env.LANG?.split(".")[0] ?? "en-US")
23
- : navigator.language,
24
- );
25
-
26
- const getTimezone = new Singleton(
27
- () => Intl.DateTimeFormat().resolvedOptions().timeZone,
28
- );
29
-
30
- const isNode = new Singleton(() => {
31
- const isObject = (obj: any) => typeof obj === "object" && obj !== null;
32
- return (
33
- typeof global === "object" &&
34
- isObject(global) &&
35
- isObject(global.process) &&
36
- isObject(global.process.versions) &&
37
- typeof global.process.versions.node !== "undefined"
38
- );
39
- });
1
+ import { IAgenticaConfig } from "../structures/IAgenticaConfig";
2
+ import { AgenticaSystemPrompt } from "./AgenticaSystemPrompt";
3
+ import { Singleton } from "./Singleton";
4
+
5
+ export namespace AgenticaDefaultPrompt {
6
+ export const write = (config?: IAgenticaConfig): string => {
7
+ if (config?.systemPrompt?.common)
8
+ return config?.systemPrompt?.common(config);
9
+
10
+ const locale: string = config?.locale ?? getLocale.get();
11
+ const timezone: string = config?.timezone ?? getTimezone.get();
12
+
13
+ return AgenticaSystemPrompt.COMMON.replace("${locale}", locale).replace(
14
+ "${timezone}",
15
+ timezone,
16
+ );
17
+ };
18
+ }
19
+
20
+ const getLocale = new Singleton(() =>
21
+ isNode.get()
22
+ ? (process.env.LANG?.split(".")[0] ?? "en-US")
23
+ : navigator.language,
24
+ );
25
+
26
+ const getTimezone = new Singleton(
27
+ () => Intl.DateTimeFormat().resolvedOptions().timeZone,
28
+ );
29
+
30
+ const isNode = new Singleton(() => {
31
+ const isObject = (obj: any) => typeof obj === "object" && obj !== null;
32
+ return (
33
+ typeof global === "object" &&
34
+ isObject(global) &&
35
+ isObject(global.process) &&
36
+ isObject(global.process.versions) &&
37
+ typeof global.process.versions.node !== "undefined"
38
+ );
39
+ });
@@ -1,82 +1,82 @@
1
- import { IAgenticaConfig } from "../structures/IAgenticaConfig";
2
- import { IAgenticaController } from "../structures/IAgenticaController";
3
- import { IAgenticaOperation } from "../structures/IAgenticaOperation";
4
- import { IAgenticaOperationCollection } from "../structures/IAgenticaOperationCollection";
5
- import { __map_take } from "./__map_take";
6
-
7
- export namespace AgenticaOperationComposer {
8
- export const compose = (props: {
9
- controllers: IAgenticaController[];
10
- config?: IAgenticaConfig | undefined;
11
- }): IAgenticaOperationCollection => {
12
- const unique: boolean =
13
- props.controllers.length === 1 ||
14
- (() => {
15
- const names: string[] = props.controllers
16
- .map((controller) =>
17
- controller.application.functions.map((func) => func.name),
18
- )
19
- .flat();
20
- return new Set(names).size === names.length;
21
- })();
22
- const naming = (func: string, ci: number) =>
23
- unique ? func : `_${ci}_${func}`;
24
-
25
- const array: IAgenticaOperation[] = props.controllers
26
- .map((controller, ci) =>
27
- controller.protocol === "http"
28
- ? controller.application.functions.map(
29
- (func) =>
30
- ({
31
- protocol: "http",
32
- controller,
33
- function: func,
34
- name: naming(func.name, ci),
35
- }) satisfies IAgenticaOperation.IHttp,
36
- )
37
- : controller.application.functions.map(
38
- (func) =>
39
- ({
40
- protocol: "class",
41
- controller,
42
- function: func,
43
- name: naming(func.name, ci),
44
- }) satisfies IAgenticaOperation.IClass,
45
- ),
46
- )
47
- .flat();
48
- const divided: IAgenticaOperation[][] | undefined =
49
- !!props.config?.capacity && array.length > props.config.capacity
50
- ? divideOperations({
51
- array,
52
- capacity: props.config.capacity,
53
- })
54
- : undefined;
55
-
56
- const flat: Map<string, IAgenticaOperation> = new Map();
57
- const group: Map<string, Map<string, IAgenticaOperation>> = new Map();
58
- for (const item of array) {
59
- flat.set(item.name, item);
60
- __map_take(group, item.controller.name, () => new Map()).set(
61
- item.name,
62
- item,
63
- );
64
- }
65
- return {
66
- array,
67
- divided,
68
- flat,
69
- group,
70
- };
71
- };
72
-
73
- const divideOperations = (props: {
74
- array: IAgenticaOperation[];
75
- capacity: number;
76
- }): IAgenticaOperation[][] => {
77
- const size: number = Math.ceil(props.array.length / props.capacity);
78
- const capacity: number = Math.ceil(props.array.length / size);
79
- const replica: IAgenticaOperation[] = props.array.slice();
80
- return new Array(size).fill(0).map(() => replica.splice(0, capacity));
81
- };
82
- }
1
+ import { IAgenticaConfig } from "../structures/IAgenticaConfig";
2
+ import { IAgenticaController } from "../structures/IAgenticaController";
3
+ import { IAgenticaOperation } from "../structures/IAgenticaOperation";
4
+ import { IAgenticaOperationCollection } from "../structures/IAgenticaOperationCollection";
5
+ import { __map_take } from "./__map_take";
6
+
7
+ export namespace AgenticaOperationComposer {
8
+ export const compose = (props: {
9
+ controllers: IAgenticaController[];
10
+ config?: IAgenticaConfig | undefined;
11
+ }): IAgenticaOperationCollection => {
12
+ const unique: boolean =
13
+ props.controllers.length === 1 ||
14
+ (() => {
15
+ const names: string[] = props.controllers
16
+ .map((controller) =>
17
+ controller.application.functions.map((func) => func.name),
18
+ )
19
+ .flat();
20
+ return new Set(names).size === names.length;
21
+ })();
22
+ const naming = (func: string, ci: number) =>
23
+ unique ? func : `_${ci}_${func}`;
24
+
25
+ const array: IAgenticaOperation[] = props.controllers
26
+ .map((controller, ci) =>
27
+ controller.protocol === "http"
28
+ ? controller.application.functions.map(
29
+ (func) =>
30
+ ({
31
+ protocol: "http",
32
+ controller,
33
+ function: func,
34
+ name: naming(func.name, ci),
35
+ }) satisfies IAgenticaOperation.IHttp,
36
+ )
37
+ : controller.application.functions.map(
38
+ (func) =>
39
+ ({
40
+ protocol: "class",
41
+ controller,
42
+ function: func,
43
+ name: naming(func.name, ci),
44
+ }) satisfies IAgenticaOperation.IClass,
45
+ ),
46
+ )
47
+ .flat();
48
+ const divided: IAgenticaOperation[][] | undefined =
49
+ !!props.config?.capacity && array.length > props.config.capacity
50
+ ? divideOperations({
51
+ array,
52
+ capacity: props.config.capacity,
53
+ })
54
+ : undefined;
55
+
56
+ const flat: Map<string, IAgenticaOperation> = new Map();
57
+ const group: Map<string, Map<string, IAgenticaOperation>> = new Map();
58
+ for (const item of array) {
59
+ flat.set(item.name, item);
60
+ __map_take(group, item.controller.name, () => new Map()).set(
61
+ item.name,
62
+ item,
63
+ );
64
+ }
65
+ return {
66
+ array,
67
+ divided,
68
+ flat,
69
+ group,
70
+ };
71
+ };
72
+
73
+ const divideOperations = (props: {
74
+ array: IAgenticaOperation[];
75
+ capacity: number;
76
+ }): IAgenticaOperation[][] => {
77
+ const size: number = Math.ceil(props.array.length / props.capacity);
78
+ const capacity: number = Math.ceil(props.array.length / size);
79
+ const replica: IAgenticaOperation[] = props.array.slice();
80
+ return new Array(size).fill(0).map(() => replica.splice(0, capacity));
81
+ };
82
+ }
@@ -1,30 +1,30 @@
1
- import { IAgenticaOperationSelection } from "../structures/IAgenticaOperationSelection";
2
- import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
3
-
4
- export namespace AgenticaPromptFactory {
5
- export const execute = (
6
- props: Omit<IAgenticaPrompt.IExecute, "toJSON">,
7
- ): IAgenticaPrompt.IExecute =>
8
- ({
9
- ...props,
10
- toJSON: () =>
11
- ({
12
- ...props,
13
- controller: props.controller.name,
14
- function: props.function.name,
15
- }) as any,
16
- }) as IAgenticaPrompt.IExecute;
17
-
18
- export const selection = (
19
- props: Omit<IAgenticaOperationSelection, "toJSON">,
20
- ): IAgenticaOperationSelection =>
21
- ({
22
- ...props,
23
- toJSON: () =>
24
- ({
25
- ...props,
26
- controller: props.controller.name,
27
- function: props.function.name,
28
- }) as any,
29
- }) as IAgenticaOperationSelection;
30
- }
1
+ import { IAgenticaOperationSelection } from "../structures/IAgenticaOperationSelection";
2
+ import { IAgenticaPrompt } from "../structures/IAgenticaPrompt";
3
+
4
+ export namespace AgenticaPromptFactory {
5
+ export const execute = (
6
+ props: Omit<IAgenticaPrompt.IExecute, "toJSON">,
7
+ ): IAgenticaPrompt.IExecute =>
8
+ ({
9
+ ...props,
10
+ toJSON: () =>
11
+ ({
12
+ ...props,
13
+ controller: props.controller.name,
14
+ function: props.function.name,
15
+ }) as any,
16
+ }) as IAgenticaPrompt.IExecute;
17
+
18
+ export const selection = (
19
+ props: Omit<IAgenticaOperationSelection, "toJSON">,
20
+ ): IAgenticaOperationSelection =>
21
+ ({
22
+ ...props,
23
+ toJSON: () =>
24
+ ({
25
+ ...props,
26
+ controller: props.controller.name,
27
+ function: props.function.name,
28
+ }) as any,
29
+ }) as IAgenticaOperationSelection;
30
+ }