@agentica/core 0.27.2 → 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.
- package/README.md +17 -2
- package/lib/Agentica.d.ts +2 -1
- package/lib/Agentica.js +92 -60
- package/lib/Agentica.js.map +1 -1
- package/lib/MicroAgentica.d.ts +1 -0
- package/lib/MicroAgentica.js +88 -60
- package/lib/MicroAgentica.js.map +1 -1
- package/lib/constants/AgenticaDefaultPrompt.js +2 -18
- package/lib/constants/AgenticaDefaultPrompt.js.map +1 -1
- package/lib/context/AgenticaContext.d.ts +1 -1
- package/lib/context/MicroAgenticaContext.d.ts +2 -2
- package/lib/context/internal/AgenticaOperationComposer.spec.js +0 -2
- package/lib/context/internal/AgenticaOperationComposer.spec.js.map +1 -1
- package/lib/events/AgenticaEventBase.d.ts +9 -0
- package/lib/factory/events.d.ts +1 -2
- package/lib/factory/events.js +71 -7
- package/lib/factory/events.js.map +1 -1
- package/lib/factory/histories.js +27 -9
- package/lib/factory/histories.js.map +1 -1
- package/lib/functional/assertMcpController.d.ts +1 -1
- package/lib/functional/assertMcpController.js.map +1 -1
- package/lib/functional/validateMcpController.d.ts +1 -1
- package/lib/functional/validateMcpController.js.map +1 -1
- package/lib/histories/AgenticaCancelHistory.d.ts +1 -2
- package/lib/histories/AgenticaExecuteHistory.d.ts +0 -4
- package/lib/histories/AgenticaHistoryBase.d.ts +9 -0
- package/lib/histories/AgenticaSelectHistory.d.ts +1 -2
- package/lib/index.mjs +413 -380
- package/lib/index.mjs.map +1 -1
- package/lib/json/IAgenticaEventJson.d.ts +9 -1
- package/lib/json/IAgenticaHistoryJson.d.ts +11 -14
- package/lib/orchestrate/call.d.ts +2 -2
- package/lib/orchestrate/call.js +41 -70
- package/lib/orchestrate/call.js.map +1 -1
- package/lib/orchestrate/cancel.d.ts +1 -2
- package/lib/orchestrate/cancel.js +13 -38
- package/lib/orchestrate/cancel.js.map +1 -1
- package/lib/orchestrate/describe.d.ts +1 -2
- package/lib/orchestrate/describe.js +5 -17
- package/lib/orchestrate/describe.js.map +1 -1
- package/lib/orchestrate/execute.d.ts +1 -2
- package/lib/orchestrate/execute.js +9 -13
- package/lib/orchestrate/execute.js.map +1 -1
- package/lib/orchestrate/initialize.d.ts +1 -2
- package/lib/orchestrate/initialize.js +3 -11
- package/lib/orchestrate/initialize.js.map +1 -1
- package/lib/orchestrate/internal/{cancelFunction.js → cancelFunctionFromContext.js} +7 -7
- package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -0
- package/lib/orchestrate/internal/selectFunctionFromContext.js +24 -0
- package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -0
- package/lib/orchestrate/select.d.ts +1 -2
- package/lib/orchestrate/select.js +20 -51
- package/lib/orchestrate/select.js.map +1 -1
- package/lib/structures/IAgenticaConfig.d.ts +1 -3
- package/lib/structures/IAgenticaController.d.ts +123 -88
- package/lib/structures/IAgenticaExecutor.d.ts +6 -7
- package/lib/structures/IAgenticaVendor.d.ts +14 -0
- package/lib/structures/IMicroAgenticaExecutor.d.ts +2 -3
- package/lib/transformers/transformHistory.js +13 -8
- package/lib/transformers/transformHistory.js.map +1 -1
- package/lib/utils/ChatGptCompletionMessageUtil.js +3 -3
- package/package.json +13 -11
- package/src/Agentica.ts +127 -87
- package/src/MicroAgentica.ts +118 -81
- package/src/constants/AgenticaDefaultPrompt.ts +3 -20
- package/src/context/AgenticaContext.ts +1 -1
- package/src/context/MicroAgenticaContext.ts +2 -2
- package/src/context/internal/AgenticaOperationComposer.spec.ts +1 -2
- package/src/events/AgenticaEventBase.ts +12 -0
- package/src/factory/events.ts +78 -8
- package/src/factory/histories.ts +41 -11
- package/src/functional/assertMcpController.ts +1 -4
- package/src/functional/validateMcpController.ts +1 -4
- package/src/histories/AgenticaCancelHistory.ts +1 -2
- package/src/histories/AgenticaExecuteHistory.ts +0 -5
- package/src/histories/AgenticaHistoryBase.ts +12 -0
- package/src/histories/AgenticaSelectHistory.ts +1 -2
- package/src/json/IAgenticaEventJson.ts +11 -1
- package/src/json/IAgenticaHistoryJson.ts +14 -17
- package/src/orchestrate/call.ts +57 -107
- package/src/orchestrate/cancel.ts +76 -99
- package/src/orchestrate/describe.ts +16 -36
- package/src/orchestrate/execute.ts +17 -37
- package/src/orchestrate/initialize.ts +36 -49
- package/src/orchestrate/internal/{cancelFunction.ts → cancelFunctionFromContext.ts} +11 -11
- package/src/orchestrate/internal/{selectFunction.ts → selectFunctionFromContext.ts} +18 -13
- package/src/orchestrate/select.ts +112 -151
- package/src/structures/IAgenticaConfig.ts +1 -3
- package/src/structures/IAgenticaController.ts +4 -0
- package/src/structures/IAgenticaExecutor.ts +10 -8
- package/src/structures/IAgenticaVendor.ts +15 -0
- package/src/structures/IMicroAgenticaExecutor.ts +2 -3
- package/src/transformers/transformHistory.ts +19 -20
- package/lib/orchestrate/internal/cancelFunction.js.map +0 -1
- package/lib/orchestrate/internal/selectFunction.js +0 -35
- package/lib/orchestrate/internal/selectFunction.js.map +0 -1
- /package/lib/orchestrate/internal/{cancelFunction.d.ts → cancelFunctionFromContext.d.ts} +0 -0
- /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 {
|
|
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<
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
|
27
|
+
return;
|
|
33
28
|
}
|
|
34
29
|
}
|
|
35
30
|
}
|
|
36
31
|
|
|
37
32
|
// CANCEL CANDIDATE FUNCTIONS
|
|
38
33
|
if (ctx.stack.length !== 0) {
|
|
39
|
-
|
|
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
|
-
|
|
48
|
-
...(await (executor?.select ?? select)(
|
|
49
|
-
ctx,
|
|
50
|
-
)),
|
|
51
|
-
);
|
|
38
|
+
await (executor?.select ?? select)(ctx);
|
|
52
39
|
if (ctx.stack.length === 0) {
|
|
53
|
-
return
|
|
40
|
+
return;
|
|
54
41
|
}
|
|
55
42
|
|
|
56
43
|
// FUNCTION CALLING LOOP
|
|
57
44
|
while (true) {
|
|
58
45
|
// EXECUTE FUNCTIONS
|
|
59
|
-
const
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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 {
|
|
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 {
|
|
13
|
-
import {
|
|
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<
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
) {
|
|
161
|
-
|
|
162
|
-
|
|
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
|
|
14
|
+
export function cancelFunctionFromContext<Model extends ILlmSchema.Model>(
|
|
14
15
|
ctx: AgenticaContext<Model>,
|
|
15
16
|
reference: __IChatFunctionReference,
|
|
16
|
-
):
|
|
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
|
|
22
|
+
return;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
const item: AgenticaOperationSelection<Model> = ctx.stack[index]!;
|
|
25
26
|
ctx.stack.splice(index, 1);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}),
|
|
27
|
+
|
|
28
|
+
const event: AgenticaCancelEvent<Model> = createCancelEvent({
|
|
29
|
+
selection: createOperationSelection({
|
|
30
|
+
operation: item.operation,
|
|
31
|
+
reason: reference.reason,
|
|
32
32
|
}),
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
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
|
-
|
|
22
|
+
= ctx.operations.flat.get(reference.name);
|
|
17
23
|
if (operation === undefined) {
|
|
18
|
-
return
|
|
24
|
+
return;
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
const selection: AgenticaOperationSelection<Model>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
= createOperationSelection({
|
|
29
|
+
operation,
|
|
30
|
+
reason: reference.reason,
|
|
31
|
+
});
|
|
26
32
|
ctx.stack.push(selection);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return operation;
|
|
33
|
+
|
|
34
|
+
const event: AgenticaSelectEvent<Model> = createSelectEvent({
|
|
35
|
+
selection,
|
|
36
|
+
});
|
|
37
|
+
ctx.dispatch(event);
|
|
33
38
|
}
|