@agentica/core 0.27.3 → 0.28.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.
Files changed (90) hide show
  1. package/README.md +17 -2
  2. package/lib/Agentica.d.ts +2 -1
  3. package/lib/Agentica.js +92 -60
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/MicroAgentica.d.ts +1 -0
  6. package/lib/MicroAgentica.js +88 -60
  7. package/lib/MicroAgentica.js.map +1 -1
  8. package/lib/constants/AgenticaDefaultPrompt.js +2 -18
  9. package/lib/constants/AgenticaDefaultPrompt.js.map +1 -1
  10. package/lib/context/AgenticaContext.d.ts +1 -1
  11. package/lib/context/MicroAgenticaContext.d.ts +2 -2
  12. package/lib/context/internal/AgenticaOperationComposer.spec.js +0 -2
  13. package/lib/context/internal/AgenticaOperationComposer.spec.js.map +1 -1
  14. package/lib/events/AgenticaEventBase.d.ts +9 -0
  15. package/lib/factory/events.d.ts +1 -2
  16. package/lib/factory/events.js +71 -7
  17. package/lib/factory/events.js.map +1 -1
  18. package/lib/factory/histories.js +27 -9
  19. package/lib/factory/histories.js.map +1 -1
  20. package/lib/histories/AgenticaCancelHistory.d.ts +1 -2
  21. package/lib/histories/AgenticaExecuteHistory.d.ts +0 -4
  22. package/lib/histories/AgenticaHistoryBase.d.ts +9 -0
  23. package/lib/histories/AgenticaSelectHistory.d.ts +1 -2
  24. package/lib/index.mjs +413 -380
  25. package/lib/index.mjs.map +1 -1
  26. package/lib/json/IAgenticaEventJson.d.ts +9 -1
  27. package/lib/json/IAgenticaHistoryJson.d.ts +11 -14
  28. package/lib/orchestrate/call.d.ts +2 -2
  29. package/lib/orchestrate/call.js +41 -70
  30. package/lib/orchestrate/call.js.map +1 -1
  31. package/lib/orchestrate/cancel.d.ts +1 -2
  32. package/lib/orchestrate/cancel.js +13 -38
  33. package/lib/orchestrate/cancel.js.map +1 -1
  34. package/lib/orchestrate/describe.d.ts +1 -2
  35. package/lib/orchestrate/describe.js +5 -17
  36. package/lib/orchestrate/describe.js.map +1 -1
  37. package/lib/orchestrate/execute.d.ts +1 -2
  38. package/lib/orchestrate/execute.js +9 -13
  39. package/lib/orchestrate/execute.js.map +1 -1
  40. package/lib/orchestrate/initialize.d.ts +1 -2
  41. package/lib/orchestrate/initialize.js +3 -11
  42. package/lib/orchestrate/initialize.js.map +1 -1
  43. package/lib/orchestrate/internal/{cancelFunction.js → cancelFunctionFromContext.js} +7 -7
  44. package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -0
  45. package/lib/orchestrate/internal/selectFunctionFromContext.js +24 -0
  46. package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -0
  47. package/lib/orchestrate/select.d.ts +1 -2
  48. package/lib/orchestrate/select.js +20 -51
  49. package/lib/orchestrate/select.js.map +1 -1
  50. package/lib/structures/IAgenticaConfig.d.ts +1 -3
  51. package/lib/structures/IAgenticaExecutor.d.ts +6 -7
  52. package/lib/structures/IAgenticaVendor.d.ts +14 -0
  53. package/lib/structures/IMicroAgenticaExecutor.d.ts +2 -3
  54. package/lib/transformers/transformHistory.js +13 -8
  55. package/lib/transformers/transformHistory.js.map +1 -1
  56. package/lib/utils/ChatGptCompletionMessageUtil.js +3 -3
  57. package/package.json +9 -7
  58. package/src/Agentica.ts +127 -87
  59. package/src/MicroAgentica.ts +118 -81
  60. package/src/constants/AgenticaDefaultPrompt.ts +3 -20
  61. package/src/context/AgenticaContext.ts +1 -1
  62. package/src/context/MicroAgenticaContext.ts +2 -2
  63. package/src/context/internal/AgenticaOperationComposer.spec.ts +1 -2
  64. package/src/events/AgenticaEventBase.ts +12 -0
  65. package/src/factory/events.ts +78 -8
  66. package/src/factory/histories.ts +41 -11
  67. package/src/histories/AgenticaCancelHistory.ts +1 -2
  68. package/src/histories/AgenticaExecuteHistory.ts +0 -5
  69. package/src/histories/AgenticaHistoryBase.ts +12 -0
  70. package/src/histories/AgenticaSelectHistory.ts +1 -2
  71. package/src/json/IAgenticaEventJson.ts +11 -1
  72. package/src/json/IAgenticaHistoryJson.ts +14 -17
  73. package/src/orchestrate/call.ts +57 -107
  74. package/src/orchestrate/cancel.ts +76 -99
  75. package/src/orchestrate/describe.ts +16 -36
  76. package/src/orchestrate/execute.ts +17 -37
  77. package/src/orchestrate/initialize.ts +36 -49
  78. package/src/orchestrate/internal/{cancelFunction.ts → cancelFunctionFromContext.ts} +11 -11
  79. package/src/orchestrate/internal/{selectFunction.ts → selectFunctionFromContext.ts} +18 -13
  80. package/src/orchestrate/select.ts +112 -151
  81. package/src/structures/IAgenticaConfig.ts +1 -3
  82. package/src/structures/IAgenticaExecutor.ts +10 -8
  83. package/src/structures/IAgenticaVendor.ts +15 -0
  84. package/src/structures/IMicroAgenticaExecutor.ts +2 -3
  85. package/src/transformers/transformHistory.ts +19 -20
  86. package/lib/orchestrate/internal/cancelFunction.js.map +0 -1
  87. package/lib/orchestrate/internal/selectFunction.js +0 -35
  88. package/lib/orchestrate/internal/selectFunction.js.map +0 -1
  89. /package/lib/orchestrate/internal/{cancelFunction.d.ts → cancelFunctionFromContext.d.ts} +0 -0
  90. /package/lib/orchestrate/internal/{selectFunction.d.ts → selectFunctionFromContext.d.ts} +0 -0
@@ -1,8 +1,7 @@
1
1
  import type { ILlmSchema } from "@samchon/openapi";
2
2
 
3
3
  import type { AgenticaContext } from "../context/AgenticaContext";
4
- import type { AgenticaExecuteHistory } from "../histories/AgenticaExecuteHistory";
5
- import type { AgenticaHistory } from "../histories/AgenticaHistory";
4
+ import type { AgenticaExecuteEvent } from "../events";
6
5
  import type { IAgenticaExecutor } from "../structures/IAgenticaExecutor";
7
6
 
8
7
  import { call } from "./call";
@@ -12,72 +11,53 @@ import { initialize } from "./initialize";
12
11
  import { select } from "./select";
13
12
 
14
13
  export function execute<Model extends ILlmSchema.Model>(executor: Partial<IAgenticaExecutor<Model>> | null) {
15
- return async (ctx: AgenticaContext<Model>): Promise<AgenticaHistory<Model>[]> => {
16
- const histories: AgenticaHistory<Model>[] = [];
17
-
14
+ return async (ctx: AgenticaContext<Model>): Promise<void> => {
18
15
  // FUNCTIONS ARE NOT LISTED YET
19
16
  if (ctx.ready() === false) {
20
17
  if (executor?.initialize !== true && typeof executor?.initialize !== "function") {
21
18
  await ctx.initialize();
22
19
  }
23
20
  else {
24
- histories.push(
25
- ...(await (
26
- typeof executor?.initialize === "function"
27
- ? executor.initialize
28
- : initialize
29
- )(ctx)),
30
- );
21
+ await (
22
+ typeof executor?.initialize === "function"
23
+ ? executor.initialize
24
+ : initialize
25
+ )(ctx);
31
26
  if (ctx.ready() === false) {
32
- return histories;
27
+ return;
33
28
  }
34
29
  }
35
30
  }
36
31
 
37
32
  // CANCEL CANDIDATE FUNCTIONS
38
33
  if (ctx.stack.length !== 0) {
39
- histories.push(
40
- ...(await (executor?.cancel ?? cancel)(
41
- ctx,
42
- )),
43
- );
34
+ await (executor?.cancel ?? cancel)(ctx);
44
35
  }
45
36
 
46
37
  // SELECT CANDIDATE FUNCTIONS
47
- histories.push(
48
- ...(await (executor?.select ?? select)(
49
- ctx,
50
- )),
51
- );
38
+ await (executor?.select ?? select)(ctx);
52
39
  if (ctx.stack.length === 0) {
53
- return histories;
40
+ return;
54
41
  }
55
42
 
56
43
  // FUNCTION CALLING LOOP
57
44
  while (true) {
58
45
  // EXECUTE FUNCTIONS
59
- const prompts: AgenticaHistory<Model>[] = await (
46
+ const executes: AgenticaExecuteEvent<Model>[] = await (
60
47
  executor?.call ?? call
61
48
  )(ctx, ctx.stack.map(s => s.operation));
62
- histories.push(...prompts);
63
49
 
64
50
  // EXPLAIN RETURN VALUES
65
- const executes: AgenticaExecuteHistory<Model>[] = prompts.filter(
66
- prompt => prompt.type === "execute",
67
- );
68
51
  if (executor?.describe !== null && executor?.describe !== false) {
69
- histories.push(
70
- ...(await (
71
- typeof executor?.describe === "function"
72
- ? executor.describe
73
- : describe
74
- )(ctx, executes)),
75
- );
52
+ await (
53
+ typeof executor?.describe === "function"
54
+ ? executor.describe
55
+ : describe
56
+ )(ctx, executes);
76
57
  }
77
58
  if (executes.length === 0 || ctx.stack.length === 0) {
78
59
  break;
79
60
  }
80
61
  }
81
- return histories;
82
62
  };
83
63
  }
@@ -5,12 +5,12 @@ import typia from "typia";
5
5
 
6
6
  import type { AgenticaContext } from "../context/AgenticaContext";
7
7
  import type { __IChatInitialApplication } from "../context/internal/__IChatInitialApplication";
8
- import type { AgenticaHistory } from "../histories/AgenticaHistory";
8
+ import type { AgenticaAssistantMessageEvent } from "../events/AgenticaAssistantMessageEvent";
9
9
 
10
10
  import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
11
11
  import { AgenticaSystemPrompt } from "../constants/AgenticaSystemPrompt";
12
- import { creatAssistantEvent } from "../factory/events";
13
- import { createAssistantMessageHistory, decodeHistory, decodeUserMessageContent } from "../factory/histories";
12
+ import { creatAssistantMessageEvent } from "../factory/events";
13
+ import { decodeHistory, decodeUserMessageContent } from "../factory/histories";
14
14
  import { ChatGptCompletionMessageUtil } from "../utils/ChatGptCompletionMessageUtil";
15
15
  import { MPSC } from "../utils/MPSC";
16
16
  import { streamDefaultReaderToAsyncGenerator, StreamUtil } from "../utils/StreamUtil";
@@ -20,31 +20,31 @@ const FUNCTION: ILlmFunction<"chatgpt"> = typia.llm.application<
20
20
  "chatgpt"
21
21
  >().functions[0]!;
22
22
 
23
- export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>): Promise<AgenticaHistory<Model>[]> {
23
+ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>): Promise<void> {
24
24
  // ----
25
25
  // EXECUTE CHATGPT API
26
26
  // ----
27
27
  const completionStream = await ctx.request("initialize", {
28
28
  messages: [
29
- // COMMON SYSTEM PROMPT
30
- {
31
- role: "system",
32
- content: AgenticaDefaultPrompt.write(ctx.config),
33
- } satisfies OpenAI.ChatCompletionSystemMessageParam,
34
- // PREVIOUS HISTORIES
35
- ...ctx.histories.map(decodeHistory).flat(),
36
- // USER INPUT
37
- {
38
- role: "user",
39
- content: ctx.prompt.contents.map(decodeUserMessageContent),
40
- },
41
- {
42
- // SYSTEM PROMPT
43
- role: "system",
44
- content:
45
- ctx.config?.systemPrompt?.initialize?.(ctx.histories)
46
- ?? AgenticaSystemPrompt.INITIALIZE,
47
- },
29
+ // COMMON SYSTEM PROMPT
30
+ {
31
+ role: "system",
32
+ content: AgenticaDefaultPrompt.write(ctx.config),
33
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
34
+ // PREVIOUS HISTORIES
35
+ ...ctx.histories.map(decodeHistory).flat(),
36
+ // USER INPUT
37
+ {
38
+ role: "user",
39
+ content: ctx.prompt.contents.map(decodeUserMessageContent),
40
+ },
41
+ {
42
+ // SYSTEM PROMPT
43
+ role: "system",
44
+ content:
45
+ ctx.config?.systemPrompt?.initialize?.(ctx.histories)
46
+ ?? AgenticaSystemPrompt.INITIALIZE,
47
+ },
48
48
  ],
49
49
  // GETTER FUNCTION
50
50
  tools: [
@@ -106,17 +106,16 @@ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaCo
106
106
  };
107
107
  mpsc.produce(choice.delta.content);
108
108
 
109
- ctx.dispatch(
110
- creatAssistantEvent({
111
- stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
112
- done: () => mpsc.done(),
113
- get: () => textContext[choice.index]!.content,
114
- join: async () => {
115
- await mpsc.waitClosed();
116
- return textContext[choice.index]!.content;
117
- },
118
- }),
119
- ).catch(() => {});
109
+ const event: AgenticaAssistantMessageEvent = creatAssistantMessageEvent({
110
+ stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
111
+ done: () => mpsc.done(),
112
+ get: () => textContext[choice.index]!.content,
113
+ join: async () => {
114
+ await mpsc.waitClosed();
115
+ return textContext[choice.index]!.content;
116
+ },
117
+ });
118
+ ctx.dispatch(event);
120
119
  }
121
120
  };
122
121
 
@@ -136,18 +135,6 @@ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaCo
136
135
  // ----
137
136
  // PROCESS COMPLETION
138
137
  // ----
139
- const prompts: AgenticaHistory<Model>[] = [];
140
- for (const choice of completion.choices) {
141
- if (
142
- choice.message.role === "assistant"
143
- && choice.message.content != null
144
- && choice.message.content.length !== 0
145
- ) {
146
- prompts.push(
147
- createAssistantMessageHistory({ text: choice.message.content }),
148
- );
149
- }
150
- }
151
138
  if (
152
139
  completion.choices.some(
153
140
  c =>
@@ -157,7 +144,7 @@ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaCo
157
144
  tc.type === "function" && tc.function.name === FUNCTION.name,
158
145
  ),
159
146
  )
160
- ) { await ctx.initialize(); }
161
-
162
- return prompts;
147
+ ) {
148
+ await ctx.initialize();
149
+ }
163
150
  }
@@ -3,6 +3,7 @@ import type { ILlmSchema } from "@samchon/openapi";
3
3
  import type { AgenticaContext } from "../../context/AgenticaContext";
4
4
  import type { AgenticaOperationSelection } from "../../context/AgenticaOperationSelection";
5
5
  import type { __IChatFunctionReference } from "../../context/internal/__IChatFunctionReference";
6
+ import type { AgenticaCancelEvent } from "../../events";
6
7
 
7
8
  import { createCancelEvent } from "../../factory/events";
8
9
  import { createOperationSelection } from "../../factory/operations";
@@ -10,26 +11,25 @@ import { createOperationSelection } from "../../factory/operations";
10
11
  /**
11
12
  * @internal
12
13
  */
13
- export function cancelFunction<Model extends ILlmSchema.Model>(
14
+ export function cancelFunctionFromContext<Model extends ILlmSchema.Model>(
14
15
  ctx: AgenticaContext<Model>,
15
16
  reference: __IChatFunctionReference,
16
- ): AgenticaOperationSelection<Model> | null {
17
+ ): void {
17
18
  const index: number = ctx.stack.findIndex(
18
19
  item => item.operation.name === reference.name,
19
20
  );
20
21
  if (index === -1) {
21
- return null;
22
+ return;
22
23
  }
23
24
 
24
25
  const item: AgenticaOperationSelection<Model> = ctx.stack[index]!;
25
26
  ctx.stack.splice(index, 1);
26
- ctx.dispatch(
27
- createCancelEvent({
28
- selection: createOperationSelection({
29
- operation: item.operation,
30
- reason: reference.reason,
31
- }),
27
+
28
+ const event: AgenticaCancelEvent<Model> = createCancelEvent({
29
+ selection: createOperationSelection({
30
+ operation: item.operation,
31
+ reason: reference.reason,
32
32
  }),
33
- ).catch(() => {});
34
- return item;
33
+ });
34
+ ctx.dispatch(event);
35
35
  }
@@ -4,6 +4,7 @@ import type { AgenticaContext } from "../../context/AgenticaContext";
4
4
  import type { AgenticaOperation } from "../../context/AgenticaOperation";
5
5
  import type { AgenticaOperationSelection } from "../../context/AgenticaOperationSelection";
6
6
  import type { __IChatFunctionReference } from "../../context/internal/__IChatFunctionReference";
7
+ import type { AgenticaSelectEvent } from "../../events/AgenticaSelectEvent";
7
8
 
8
9
  import { createSelectEvent } from "../../factory/events";
9
10
  import { createOperationSelection } from "../../factory/operations";
@@ -11,23 +12,27 @@ import { createOperationSelection } from "../../factory/operations";
11
12
  /**
12
13
  * @internal
13
14
  */
14
- export async function selectFunction<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>, reference: __IChatFunctionReference): Promise<AgenticaOperation<Model> | null> {
15
+ export function selectFunctionFromContext<
16
+ Model extends ILlmSchema.Model,
17
+ >(
18
+ ctx: AgenticaContext<Model>,
19
+ reference: __IChatFunctionReference,
20
+ ): void {
15
21
  const operation: AgenticaOperation<Model> | undefined
16
- = ctx.operations.flat.get(reference.name);
22
+ = ctx.operations.flat.get(reference.name);
17
23
  if (operation === undefined) {
18
- return null;
24
+ return;
19
25
  }
20
26
 
21
27
  const selection: AgenticaOperationSelection<Model>
22
- = createOperationSelection({
23
- operation,
24
- reason: reference.reason,
25
- });
28
+ = createOperationSelection({
29
+ operation,
30
+ reason: reference.reason,
31
+ });
26
32
  ctx.stack.push(selection);
27
- ctx.dispatch(
28
- createSelectEvent({
29
- selection,
30
- }),
31
- ).catch(() => {});
32
- return operation;
33
+
34
+ const event: AgenticaSelectEvent<Model> = createSelectEvent({
35
+ selection,
36
+ });
37
+ ctx.dispatch(event);
33
38
  }