@agentica/core 0.34.2 → 0.35.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/lib/Agentica.d.ts +9 -10
- package/lib/Agentica.js.map +1 -1
- package/lib/MicroAgentica.d.ts +9 -10
- package/lib/MicroAgentica.js.map +1 -1
- package/lib/constants/AgenticaDefaultPrompt.d.ts +1 -2
- package/lib/constants/AgenticaDefaultPrompt.js.map +1 -1
- package/lib/context/AgenticaContext.d.ts +6 -7
- package/lib/context/AgenticaOperation.d.ts +5 -5
- package/lib/context/AgenticaOperationCollection.d.ts +5 -6
- package/lib/context/AgenticaOperationSelection.d.ts +2 -3
- package/lib/context/MicroAgenticaContext.d.ts +5 -6
- package/lib/context/internal/AgenticaOperationComposer.js.map +1 -1
- package/lib/context/internal/AgenticaOperationComposer.spec.js +41 -9
- package/lib/context/internal/AgenticaOperationComposer.spec.js.map +1 -1
- package/lib/context/internal/__IChatInitialApplication.d.ts +1 -1
- package/lib/context/internal/isAgenticaContext.js.map +1 -1
- package/lib/events/AgenticaCallEvent.d.ts +2 -3
- package/lib/events/AgenticaCancelEvent.d.ts +2 -3
- package/lib/events/AgenticaDescribeEvent.d.ts +3 -4
- package/lib/events/AgenticaEvent.d.ts +10 -11
- package/lib/events/AgenticaExecuteEvent.d.ts +5 -5
- package/lib/events/AgenticaJsonParseErrorEvent.d.ts +2 -3
- package/lib/events/AgenticaSelectEvent.d.ts +3 -4
- package/lib/events/AgenticaValidateEvent.d.ts +2 -3
- package/lib/events/MicroAgenticaEvent.d.ts +8 -9
- package/lib/factory/events.d.ts +22 -22
- package/lib/factory/events.js.map +1 -1
- package/lib/factory/histories.js.map +1 -1
- package/lib/factory/operations.d.ts +3 -4
- package/lib/factory/operations.js.map +1 -1
- package/lib/functional/assertHttpController.d.ts +6 -10
- package/lib/functional/assertHttpController.js +775 -91
- package/lib/functional/assertHttpController.js.map +1 -1
- package/lib/functional/assertHttpLlmApplication.d.ts +4 -8
- package/lib/functional/assertHttpLlmApplication.js +775 -91
- package/lib/functional/assertHttpLlmApplication.js.map +1 -1
- package/lib/functional/assertMcpController.d.ts +5 -6
- package/lib/functional/assertMcpController.js +201 -32
- package/lib/functional/assertMcpController.js.map +1 -1
- package/lib/functional/validateHttpController.d.ts +6 -10
- package/lib/functional/validateHttpController.js +636 -89
- package/lib/functional/validateHttpController.js.map +1 -1
- package/lib/functional/validateHttpLlmApplication.d.ts +4 -8
- package/lib/functional/validateHttpLlmApplication.js +636 -89
- package/lib/functional/validateHttpLlmApplication.js.map +1 -1
- package/lib/functional/validateMcpController.d.ts +5 -6
- package/lib/functional/validateMcpController.js +366 -61
- package/lib/functional/validateMcpController.js.map +1 -1
- package/lib/histories/AgenticaCancelHistory.d.ts +2 -3
- package/lib/histories/AgenticaDescribeHistory.d.ts +2 -3
- package/lib/histories/AgenticaExecuteHistory.d.ts +5 -5
- package/lib/histories/AgenticaHistory.d.ts +7 -8
- package/lib/histories/AgenticaSelectHistory.d.ts +2 -3
- package/lib/histories/MicroAgenticaHistory.d.ts +5 -6
- package/lib/index.mjs +3964 -849
- package/lib/index.mjs.map +1 -1
- package/lib/orchestrate/call.d.ts +1 -2
- package/lib/orchestrate/call.js.map +1 -1
- package/lib/orchestrate/cancel.d.ts +1 -2
- package/lib/orchestrate/cancel.js +4 -4
- package/lib/orchestrate/cancel.js.map +1 -1
- package/lib/orchestrate/describe.d.ts +1 -2
- package/lib/orchestrate/describe.js.map +1 -1
- package/lib/orchestrate/execute.d.ts +1 -2
- package/lib/orchestrate/execute.js.map +1 -1
- package/lib/orchestrate/initialize.d.ts +1 -2
- package/lib/orchestrate/initialize.js +262 -83
- package/lib/orchestrate/initialize.js.map +1 -1
- package/lib/orchestrate/internal/cancelFunctionFromContext.js.map +1 -1
- package/lib/orchestrate/internal/selectFunctionFromContext.js.map +1 -1
- package/lib/orchestrate/select.d.ts +1 -2
- package/lib/orchestrate/select.js +4 -4
- package/lib/orchestrate/select.js.map +1 -1
- package/lib/structures/IAgenticaConfig.d.ts +3 -4
- package/lib/structures/IAgenticaController.d.ts +11 -14
- package/lib/structures/IAgenticaExecutor.d.ts +6 -7
- package/lib/structures/IAgenticaProps.d.ts +3 -8
- package/lib/structures/IAgenticaSystemPrompt.d.ts +9 -10
- package/lib/structures/IMicroAgenticaConfig.d.ts +3 -4
- package/lib/structures/IMicroAgenticaExecutor.d.ts +3 -4
- package/lib/structures/IMicroAgenticaProps.d.ts +3 -8
- package/lib/structures/IMicroAgenticaSystemPrompt.d.ts +6 -7
- package/lib/transformers/transformHistory.js.map +1 -1
- package/lib/utils/ChatGptCompletionStreamingUtil.spec.js +2 -2
- package/lib/utils/ChatGptCompletionStreamingUtil.spec.js.map +1 -1
- package/lib/utils/request.d.ts +2 -3
- package/lib/utils/request.js.map +1 -1
- package/package.json +8 -8
- package/src/Agentica.ts +24 -26
- package/src/MicroAgentica.ts +27 -29
- package/src/constants/AgenticaDefaultPrompt.ts +2 -4
- package/src/context/AgenticaContext.ts +6 -7
- package/src/context/AgenticaOperation.ts +14 -14
- package/src/context/AgenticaOperationCollection.ts +5 -8
- package/src/context/AgenticaOperationSelection.ts +2 -4
- package/src/context/MicroAgenticaContext.ts +5 -6
- package/src/context/internal/AgenticaOperationComposer.spec.ts +50 -18
- package/src/context/internal/AgenticaOperationComposer.ts +21 -23
- package/src/context/internal/__IChatInitialApplication.ts +1 -1
- package/src/context/internal/isAgenticaContext.ts +4 -6
- package/src/events/AgenticaCallEvent.ts +2 -5
- package/src/events/AgenticaCancelEvent.ts +2 -6
- package/src/events/AgenticaDescribeEvent.ts +3 -7
- package/src/events/AgenticaEvent.ts +17 -19
- package/src/events/AgenticaEvent.type.ts +1 -1
- package/src/events/AgenticaExecuteEvent.ts +12 -12
- package/src/events/AgenticaJsonParseErrorEvent.ts +2 -4
- package/src/events/AgenticaSelectEvent.ts +3 -7
- package/src/events/AgenticaValidateEvent.ts +2 -5
- package/src/events/MicroAgenticaEvent.ts +13 -15
- package/src/factory/events.ts +24 -24
- package/src/factory/histories.ts +15 -17
- package/src/factory/operations.ts +3 -5
- package/src/functional/assertHttpController.ts +7 -15
- package/src/functional/assertHttpLlmApplication.ts +4 -13
- package/src/functional/assertMcpController.ts +6 -8
- package/src/functional/validateHttpController.ts +7 -15
- package/src/functional/validateHttpLlmApplication.ts +4 -13
- package/src/functional/validateMcpController.ts +6 -11
- package/src/histories/AgenticaCancelHistory.ts +2 -6
- package/src/histories/AgenticaDescribeHistory.ts +2 -6
- package/src/histories/AgenticaExecuteHistory.ts +7 -9
- package/src/histories/AgenticaHistory.ts +11 -13
- package/src/histories/AgenticaSelectHistory.ts +2 -6
- package/src/histories/MicroAgenticaHistory.ts +7 -9
- package/src/orchestrate/call.ts +55 -56
- package/src/orchestrate/cancel.ts +11 -12
- package/src/orchestrate/describe.ts +4 -5
- package/src/orchestrate/execute.ts +3 -5
- package/src/orchestrate/initialize.ts +4 -5
- package/src/orchestrate/internal/cancelFunctionFromContext.ts +4 -6
- package/src/orchestrate/internal/selectFunctionFromContext.ts +5 -9
- package/src/orchestrate/select.ts +11 -12
- package/src/structures/IAgenticaConfig.ts +4 -6
- package/src/structures/IAgenticaController.ts +11 -14
- package/src/structures/IAgenticaExecutor.ts +9 -11
- package/src/structures/IAgenticaProps.ts +3 -10
- package/src/structures/IAgenticaSystemPrompt.ts +9 -11
- package/src/structures/IMicroAgenticaConfig.ts +3 -5
- package/src/structures/IMicroAgenticaExecutor.ts +4 -6
- package/src/structures/IMicroAgenticaProps.ts +3 -10
- package/src/structures/IMicroAgenticaSystemPrompt.ts +6 -8
- package/src/transformers/transformHistory.ts +19 -21
- package/src/utils/ChatGptCompletionStreamingUtil.spec.ts +4 -5
- package/src/utils/request.ts +2 -3
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
|
|
3
1
|
import type { AgenticaAssistantMessageHistory } from "./AgenticaAssistantMessageHistory";
|
|
4
2
|
import type { AgenticaCancelHistory } from "./AgenticaCancelHistory";
|
|
5
3
|
import type { AgenticaDescribeHistory } from "./AgenticaDescribeHistory";
|
|
@@ -8,21 +6,21 @@ import type { AgenticaSelectHistory } from "./AgenticaSelectHistory";
|
|
|
8
6
|
import type { AgenticaSystemMessageHistory } from "./AgenticaSystemMessageHistory";
|
|
9
7
|
import type { AgenticaUserMessageHistory } from "./AgenticaUserMessageHistory";
|
|
10
8
|
|
|
11
|
-
export type AgenticaHistory
|
|
12
|
-
| AgenticaCancelHistory
|
|
13
|
-
| AgenticaDescribeHistory
|
|
14
|
-
| AgenticaExecuteHistory
|
|
15
|
-
| AgenticaSelectHistory
|
|
9
|
+
export type AgenticaHistory =
|
|
10
|
+
| AgenticaCancelHistory
|
|
11
|
+
| AgenticaDescribeHistory
|
|
12
|
+
| AgenticaExecuteHistory
|
|
13
|
+
| AgenticaSelectHistory
|
|
16
14
|
| AgenticaAssistantMessageHistory
|
|
17
15
|
| AgenticaUserMessageHistory
|
|
18
16
|
| AgenticaSystemMessageHistory;
|
|
19
17
|
export namespace AgenticaHistory {
|
|
20
|
-
export type Type = AgenticaHistory
|
|
21
|
-
export interface Mapper
|
|
22
|
-
select: AgenticaSelectHistory
|
|
23
|
-
cancel: AgenticaCancelHistory
|
|
24
|
-
execute: AgenticaExecuteHistory
|
|
25
|
-
describe: AgenticaDescribeHistory
|
|
18
|
+
export type Type = AgenticaHistory["type"];
|
|
19
|
+
export interface Mapper {
|
|
20
|
+
select: AgenticaSelectHistory;
|
|
21
|
+
cancel: AgenticaCancelHistory;
|
|
22
|
+
execute: AgenticaExecuteHistory;
|
|
23
|
+
describe: AgenticaDescribeHistory;
|
|
26
24
|
assistantMessage: AgenticaAssistantMessageHistory;
|
|
27
25
|
systemMessage: AgenticaSystemMessageHistory;
|
|
28
26
|
userMessage: AgenticaUserMessageHistory;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
|
|
3
1
|
import type { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
|
|
4
2
|
import type { IAgenticaHistoryJson } from "../json/IAgenticaHistoryJson";
|
|
5
3
|
|
|
6
4
|
import type { AgenticaHistoryBase } from "./AgenticaHistoryBase";
|
|
7
5
|
|
|
8
|
-
export interface AgenticaSelectHistory<
|
|
9
|
-
|
|
10
|
-
> extends AgenticaHistoryBase<"select", IAgenticaHistoryJson.ISelect> {
|
|
11
|
-
selection: AgenticaOperationSelection<Model>;
|
|
6
|
+
export interface AgenticaSelectHistory extends AgenticaHistoryBase<"select", IAgenticaHistoryJson.ISelect> {
|
|
7
|
+
selection: AgenticaOperationSelection;
|
|
12
8
|
}
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
|
|
3
1
|
import type { AgenticaAssistantMessageHistory } from "./AgenticaAssistantMessageHistory";
|
|
4
2
|
import type { AgenticaDescribeHistory } from "./AgenticaDescribeHistory";
|
|
5
3
|
import type { AgenticaExecuteHistory } from "./AgenticaExecuteHistory";
|
|
6
4
|
import type { AgenticaUserMessageHistory } from "./AgenticaUserMessageHistory";
|
|
7
5
|
|
|
8
|
-
export type MicroAgenticaHistory
|
|
9
|
-
| AgenticaDescribeHistory
|
|
10
|
-
| AgenticaExecuteHistory
|
|
6
|
+
export type MicroAgenticaHistory =
|
|
7
|
+
| AgenticaDescribeHistory
|
|
8
|
+
| AgenticaExecuteHistory
|
|
11
9
|
| AgenticaAssistantMessageHistory
|
|
12
10
|
| AgenticaUserMessageHistory;
|
|
13
11
|
export namespace MicroAgenticaHistory {
|
|
14
|
-
export type Type = MicroAgenticaHistory
|
|
15
|
-
export interface Mapper
|
|
16
|
-
describe: AgenticaDescribeHistory
|
|
17
|
-
execute: AgenticaExecuteHistory
|
|
12
|
+
export type Type = MicroAgenticaHistory["type"];
|
|
13
|
+
export interface Mapper {
|
|
14
|
+
describe: AgenticaDescribeHistory;
|
|
15
|
+
execute: AgenticaExecuteHistory;
|
|
18
16
|
userMessage: AgenticaUserMessageHistory;
|
|
19
17
|
assistantMessage: AgenticaAssistantMessageHistory;
|
|
20
18
|
}
|
package/src/orchestrate/call.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
IChatGptSchema,
|
|
3
2
|
IHttpResponse,
|
|
4
3
|
ILlmSchema,
|
|
5
4
|
IValidation,
|
|
@@ -32,10 +31,10 @@ import { StreamUtil, toAsyncGenerator } from "../utils/StreamUtil";
|
|
|
32
31
|
|
|
33
32
|
import { cancelFunctionFromContext } from "./internal/cancelFunctionFromContext";
|
|
34
33
|
|
|
35
|
-
export async function call
|
|
36
|
-
ctx: AgenticaContext
|
|
37
|
-
operations: AgenticaOperation
|
|
38
|
-
): Promise<AgenticaExecuteEvent
|
|
34
|
+
export async function call(
|
|
35
|
+
ctx: AgenticaContext | MicroAgenticaContext,
|
|
36
|
+
operations: AgenticaOperation[],
|
|
37
|
+
): Promise<AgenticaExecuteEvent[]> {
|
|
39
38
|
const _retryFn = __get_retry(1);
|
|
40
39
|
const retryFn = async (fn: (prevError?: unknown) => Promise<OpenAI.ChatCompletion>) => {
|
|
41
40
|
return _retryFn(fn).catch((e) => {
|
|
@@ -74,7 +73,7 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
74
73
|
? []
|
|
75
74
|
: [{
|
|
76
75
|
role: "system",
|
|
77
|
-
content: ctx.config?.systemPrompt?.execute?.(ctx.histories as MicroAgenticaHistory
|
|
76
|
+
content: ctx.config?.systemPrompt?.execute?.(ctx.histories as MicroAgenticaHistory[])
|
|
78
77
|
?? AgenticaSystemPrompt.EXECUTE,
|
|
79
78
|
} satisfies OpenAI.ChatCompletionSystemMessageParam]),
|
|
80
79
|
],
|
|
@@ -96,7 +95,7 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
96
95
|
required: [],
|
|
97
96
|
additionalProperties: false,
|
|
98
97
|
$defs: {},
|
|
99
|
-
} satisfies
|
|
98
|
+
} satisfies ILlmSchema.IParameters))
|
|
100
99
|
: s.function.parameters) as Record<string, any>,
|
|
101
100
|
},
|
|
102
101
|
}) as OpenAI.ChatCompletionTool,
|
|
@@ -134,19 +133,19 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
134
133
|
return [];
|
|
135
134
|
}
|
|
136
135
|
|
|
137
|
-
const executes: AgenticaExecuteEvent
|
|
136
|
+
const executes: AgenticaExecuteEvent[] = [];
|
|
138
137
|
|
|
139
138
|
const retry: number = ctx.config?.retry ?? AgenticaConstant.RETRY;
|
|
140
139
|
for (const choice of completion.choices) {
|
|
141
140
|
for (const tc of choice.message.tool_calls ?? []) {
|
|
142
141
|
if (tc.type === "function") {
|
|
143
|
-
const operation: AgenticaOperation
|
|
142
|
+
const operation: AgenticaOperation | undefined = operations.find(
|
|
144
143
|
s => s.name === tc.function.name,
|
|
145
144
|
);
|
|
146
145
|
if (operation === undefined) {
|
|
147
146
|
continue; // Ignore unknown tool calls
|
|
148
147
|
}
|
|
149
|
-
const event: AgenticaExecuteEvent
|
|
148
|
+
const event: AgenticaExecuteEvent = await predicate(
|
|
150
149
|
ctx,
|
|
151
150
|
operation,
|
|
152
151
|
tc,
|
|
@@ -156,7 +155,7 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
156
155
|
await ctx.dispatch(event);
|
|
157
156
|
executes.push(event);
|
|
158
157
|
if (isAgenticaContext(ctx)) {
|
|
159
|
-
cancelFunctionFromContext(ctx, {
|
|
158
|
+
cancelFunctionFromContext(ctx as unknown as AgenticaContext, {
|
|
160
159
|
name: event.operation.name,
|
|
161
160
|
reason: "completed",
|
|
162
161
|
});
|
|
@@ -167,15 +166,15 @@ export async function call<Model extends ILlmSchema.Model>(
|
|
|
167
166
|
return executes;
|
|
168
167
|
}
|
|
169
168
|
|
|
170
|
-
async function predicate
|
|
171
|
-
ctx: AgenticaContext
|
|
172
|
-
operation: AgenticaOperation
|
|
169
|
+
async function predicate(
|
|
170
|
+
ctx: AgenticaContext | MicroAgenticaContext,
|
|
171
|
+
operation: AgenticaOperation,
|
|
173
172
|
toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
|
|
174
|
-
previousValidationErrors: AgenticaValidateEvent
|
|
173
|
+
previousValidationErrors: AgenticaValidateEvent[],
|
|
175
174
|
life: number,
|
|
176
|
-
): Promise<AgenticaExecuteEvent
|
|
175
|
+
): Promise<AgenticaExecuteEvent> {
|
|
177
176
|
// CHECK INPUT ARGUMENT
|
|
178
|
-
const call: AgenticaCallEvent
|
|
177
|
+
const call: AgenticaCallEvent | AgenticaJsonParseErrorEvent
|
|
179
178
|
= parseArguments(
|
|
180
179
|
operation,
|
|
181
180
|
toolCall,
|
|
@@ -189,7 +188,7 @@ async function predicate<Model extends ILlmSchema.Model>(
|
|
|
189
188
|
// CHECK TYPE VALIDATION
|
|
190
189
|
const check: IValidation<unknown> = operation.function.validate(call.arguments);
|
|
191
190
|
if (check.success === false) {
|
|
192
|
-
const event: AgenticaValidateEvent
|
|
191
|
+
const event: AgenticaValidateEvent = createValidateEvent({
|
|
193
192
|
call_id: toolCall.id,
|
|
194
193
|
operation,
|
|
195
194
|
result: check,
|
|
@@ -212,14 +211,14 @@ async function predicate<Model extends ILlmSchema.Model>(
|
|
|
212
211
|
/* -----------------------------------------------------------
|
|
213
212
|
ERROR CORRECTORS
|
|
214
213
|
----------------------------------------------------------- */
|
|
215
|
-
async function correctTypeError
|
|
216
|
-
ctx: AgenticaContext
|
|
217
|
-
callEvent: AgenticaCallEvent
|
|
218
|
-
validateEvent: AgenticaValidateEvent
|
|
219
|
-
previousValidationErrors: AgenticaValidateEvent
|
|
214
|
+
async function correctTypeError(
|
|
215
|
+
ctx: AgenticaContext | MicroAgenticaContext,
|
|
216
|
+
callEvent: AgenticaCallEvent,
|
|
217
|
+
validateEvent: AgenticaValidateEvent,
|
|
218
|
+
previousValidationErrors: AgenticaValidateEvent[],
|
|
220
219
|
life: number,
|
|
221
|
-
): Promise<AgenticaExecuteEvent
|
|
222
|
-
return correctError
|
|
220
|
+
): Promise<AgenticaExecuteEvent> {
|
|
221
|
+
return correctError(ctx, {
|
|
223
222
|
giveUp: () => createExecuteEvent({
|
|
224
223
|
call_id: callEvent.id,
|
|
225
224
|
operation: callEvent.operation,
|
|
@@ -255,14 +254,14 @@ async function correctTypeError<Model extends ILlmSchema.Model>(
|
|
|
255
254
|
});
|
|
256
255
|
}
|
|
257
256
|
|
|
258
|
-
async function correctJsonError
|
|
259
|
-
ctx: AgenticaContext
|
|
257
|
+
async function correctJsonError(
|
|
258
|
+
ctx: AgenticaContext | MicroAgenticaContext,
|
|
260
259
|
toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
|
|
261
|
-
parseErrorEvent: AgenticaJsonParseErrorEvent
|
|
262
|
-
previousValidationErrors: AgenticaValidateEvent
|
|
260
|
+
parseErrorEvent: AgenticaJsonParseErrorEvent,
|
|
261
|
+
previousValidationErrors: AgenticaValidateEvent[],
|
|
263
262
|
life: number,
|
|
264
|
-
): Promise<AgenticaExecuteEvent
|
|
265
|
-
return correctError
|
|
263
|
+
): Promise<AgenticaExecuteEvent> {
|
|
264
|
+
return correctError(ctx, {
|
|
266
265
|
giveUp: () => createExecuteEvent({
|
|
267
266
|
call_id: toolCall.id,
|
|
268
267
|
operation: parseErrorEvent.operation,
|
|
@@ -291,11 +290,11 @@ async function correctJsonError<Model extends ILlmSchema.Model>(
|
|
|
291
290
|
});
|
|
292
291
|
}
|
|
293
292
|
|
|
294
|
-
function parseArguments
|
|
295
|
-
operation: AgenticaOperation
|
|
293
|
+
function parseArguments(
|
|
294
|
+
operation: AgenticaOperation,
|
|
296
295
|
toolCall: OpenAI.ChatCompletionMessageFunctionToolCall,
|
|
297
296
|
life: number,
|
|
298
|
-
): AgenticaCallEvent
|
|
297
|
+
): AgenticaCallEvent | AgenticaJsonParseErrorEvent {
|
|
299
298
|
try {
|
|
300
299
|
const data: Record<string, unknown> = JsonUtil.parse(toolCall.function.arguments);
|
|
301
300
|
return createCallEvent({
|
|
@@ -315,11 +314,11 @@ function parseArguments<Model extends ILlmSchema.Model>(
|
|
|
315
314
|
}
|
|
316
315
|
}
|
|
317
316
|
|
|
318
|
-
async function correctError
|
|
319
|
-
ctx: AgenticaContext
|
|
317
|
+
async function correctError(
|
|
318
|
+
ctx: AgenticaContext | MicroAgenticaContext,
|
|
320
319
|
props: {
|
|
321
|
-
giveUp: () => AgenticaExecuteEvent
|
|
322
|
-
operation: AgenticaOperation
|
|
320
|
+
giveUp: () => AgenticaExecuteEvent;
|
|
321
|
+
operation: AgenticaOperation;
|
|
323
322
|
toolCall: {
|
|
324
323
|
id: string;
|
|
325
324
|
arguments: string;
|
|
@@ -327,9 +326,9 @@ async function correctError<Model extends ILlmSchema.Model>(
|
|
|
327
326
|
};
|
|
328
327
|
systemPrompt: string;
|
|
329
328
|
life: number;
|
|
330
|
-
previousValidationErrors: AgenticaValidateEvent
|
|
329
|
+
previousValidationErrors: AgenticaValidateEvent[];
|
|
331
330
|
},
|
|
332
|
-
): Promise<AgenticaExecuteEvent
|
|
331
|
+
): Promise<AgenticaExecuteEvent> {
|
|
333
332
|
if (props.life <= 0) {
|
|
334
333
|
return props.giveUp();
|
|
335
334
|
}
|
|
@@ -352,7 +351,7 @@ async function correctError<Model extends ILlmSchema.Model>(
|
|
|
352
351
|
{
|
|
353
352
|
role: "system",
|
|
354
353
|
content:
|
|
355
|
-
ctx.config?.systemPrompt?.execute?.(ctx.histories as MicroAgenticaHistory
|
|
354
|
+
ctx.config?.systemPrompt?.execute?.(ctx.histories as MicroAgenticaHistory[])
|
|
356
355
|
?? AgenticaSystemPrompt.EXECUTE,
|
|
357
356
|
},
|
|
358
357
|
{
|
|
@@ -399,7 +398,7 @@ async function correctError<Model extends ILlmSchema.Model>(
|
|
|
399
398
|
properties: {},
|
|
400
399
|
additionalProperties: false,
|
|
401
400
|
required: [],
|
|
402
|
-
} satisfies
|
|
401
|
+
} satisfies ILlmSchema.IParameters))
|
|
403
402
|
|
|
404
403
|
: props.operation.function.parameters) as unknown as Record<string, unknown>,
|
|
405
404
|
},
|
|
@@ -417,7 +416,7 @@ async function correctError<Model extends ILlmSchema.Model>(
|
|
|
417
416
|
);
|
|
418
417
|
return toolCall === undefined
|
|
419
418
|
? props.giveUp()
|
|
420
|
-
: predicate
|
|
419
|
+
: predicate(
|
|
421
420
|
ctx,
|
|
422
421
|
props.operation,
|
|
423
422
|
toolCall,
|
|
@@ -429,10 +428,10 @@ async function correctError<Model extends ILlmSchema.Model>(
|
|
|
429
428
|
/* -----------------------------------------------------------
|
|
430
429
|
FUNCTION EXECUTORS
|
|
431
430
|
----------------------------------------------------------- */
|
|
432
|
-
async function executeFunction
|
|
433
|
-
call: AgenticaCallEvent
|
|
434
|
-
operation: AgenticaOperation
|
|
435
|
-
): Promise<AgenticaExecuteEvent
|
|
431
|
+
async function executeFunction(
|
|
432
|
+
call: AgenticaCallEvent,
|
|
433
|
+
operation: AgenticaOperation,
|
|
434
|
+
): Promise<AgenticaExecuteEvent> {
|
|
436
435
|
try {
|
|
437
436
|
const value: unknown = await (async () => {
|
|
438
437
|
switch (operation.protocol) {
|
|
@@ -473,9 +472,9 @@ async function executeFunction<Model extends ILlmSchema.Model>(
|
|
|
473
472
|
}
|
|
474
473
|
}
|
|
475
474
|
|
|
476
|
-
async function executeClassFunction
|
|
477
|
-
call: AgenticaCallEvent
|
|
478
|
-
operation: AgenticaOperation.Class
|
|
475
|
+
async function executeClassFunction(
|
|
476
|
+
call: AgenticaCallEvent,
|
|
477
|
+
operation: AgenticaOperation.Class,
|
|
479
478
|
): Promise<unknown> {
|
|
480
479
|
const execute = operation.controller.execute;
|
|
481
480
|
const value: unknown = typeof execute === "function"
|
|
@@ -490,9 +489,9 @@ async function executeClassFunction<Model extends ILlmSchema.Model>(
|
|
|
490
489
|
return value;
|
|
491
490
|
}
|
|
492
491
|
|
|
493
|
-
async function executeHttpOperation
|
|
494
|
-
call: AgenticaCallEvent
|
|
495
|
-
operation: AgenticaOperation.Http
|
|
492
|
+
async function executeHttpOperation(
|
|
493
|
+
call: AgenticaCallEvent,
|
|
494
|
+
operation: AgenticaOperation.Http,
|
|
496
495
|
): Promise<unknown> {
|
|
497
496
|
const execute = operation.controller.execute;
|
|
498
497
|
const value: IHttpResponse = typeof execute === "function"
|
|
@@ -511,9 +510,9 @@ async function executeHttpOperation<Model extends ILlmSchema.Model>(
|
|
|
511
510
|
return value;
|
|
512
511
|
}
|
|
513
512
|
|
|
514
|
-
async function executeMcpOperation
|
|
515
|
-
call: AgenticaCallEvent
|
|
516
|
-
operation: AgenticaOperation.Mcp
|
|
513
|
+
async function executeMcpOperation(
|
|
514
|
+
call: AgenticaCallEvent,
|
|
515
|
+
operation: AgenticaOperation.Mcp,
|
|
517
516
|
): Promise<unknown> {
|
|
518
517
|
return operation.controller.client.callTool({
|
|
519
518
|
method: operation.function.name,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ILlmApplication
|
|
1
|
+
import type { ILlmApplication } from "@samchon/openapi";
|
|
2
2
|
import type OpenAI from "openai";
|
|
3
3
|
import type { IValidation } from "typia";
|
|
4
4
|
|
|
@@ -22,9 +22,8 @@ import { StreamUtil } from "../utils/StreamUtil";
|
|
|
22
22
|
|
|
23
23
|
import { cancelFunctionFromContext } from "./internal/cancelFunctionFromContext";
|
|
24
24
|
|
|
25
|
-
const CONTAINER: ILlmApplication
|
|
26
|
-
__IChatCancelFunctionsApplication
|
|
27
|
-
"chatgpt"
|
|
25
|
+
const CONTAINER: ILlmApplication = typia.llm.application<
|
|
26
|
+
__IChatCancelFunctionsApplication
|
|
28
27
|
>();
|
|
29
28
|
|
|
30
29
|
interface IFailure {
|
|
@@ -33,16 +32,16 @@ interface IFailure {
|
|
|
33
32
|
validation: IValidation.IFailure;
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
export async function cancel
|
|
37
|
-
ctx: AgenticaContext
|
|
35
|
+
export async function cancel(
|
|
36
|
+
ctx: AgenticaContext,
|
|
38
37
|
): Promise<void> {
|
|
39
38
|
if (ctx.operations.divided === undefined) {
|
|
40
39
|
return step(ctx, ctx.operations.array, 0);
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
const stacks: AgenticaOperationSelection
|
|
42
|
+
const stacks: AgenticaOperationSelection[][]
|
|
44
43
|
= ctx.operations.divided.map(() => []);
|
|
45
|
-
const events: AgenticaEvent
|
|
44
|
+
const events: AgenticaEvent[] = [];
|
|
46
45
|
await Promise.all(
|
|
47
46
|
ctx.operations.divided.map(async (operations, i) =>
|
|
48
47
|
step(
|
|
@@ -78,7 +77,7 @@ export async function cancel<Model extends ILlmSchema.Model>(
|
|
|
78
77
|
);
|
|
79
78
|
}
|
|
80
79
|
else {
|
|
81
|
-
const cancelled: AgenticaCancelEvent
|
|
80
|
+
const cancelled: AgenticaCancelEvent[]
|
|
82
81
|
= events.filter(e => e.type === "cancel");
|
|
83
82
|
(cancelled.length !== 0 ? cancelled : events)
|
|
84
83
|
.forEach((e) => {
|
|
@@ -87,9 +86,9 @@ export async function cancel<Model extends ILlmSchema.Model>(
|
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
88
|
|
|
90
|
-
async function step
|
|
91
|
-
ctx: AgenticaContext
|
|
92
|
-
operations: AgenticaOperation
|
|
89
|
+
async function step(
|
|
90
|
+
ctx: AgenticaContext,
|
|
91
|
+
operations: AgenticaOperation[],
|
|
93
92
|
retry: number,
|
|
94
93
|
failures?: IFailure[],
|
|
95
94
|
): Promise<void> {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
1
|
import type OpenAI from "openai";
|
|
3
2
|
|
|
4
3
|
import type { AgenticaContext } from "../context/AgenticaContext";
|
|
@@ -12,9 +11,9 @@ import { createDescribeEvent } from "../factory/events";
|
|
|
12
11
|
import { decodeHistory } from "../factory/histories";
|
|
13
12
|
import { reduceStreamingWithDispatch } from "../utils/ChatGptCompletionStreamingUtil";
|
|
14
13
|
|
|
15
|
-
export async function describe
|
|
16
|
-
ctx: AgenticaContext
|
|
17
|
-
histories: AgenticaExecuteHistory
|
|
14
|
+
export async function describe(
|
|
15
|
+
ctx: AgenticaContext | MicroAgenticaContext,
|
|
16
|
+
histories: AgenticaExecuteHistory[],
|
|
18
17
|
): Promise<void> {
|
|
19
18
|
if (histories.length === 0) {
|
|
20
19
|
return;
|
|
@@ -40,7 +39,7 @@ export async function describe<Model extends ILlmSchema.Model>(
|
|
|
40
39
|
});
|
|
41
40
|
|
|
42
41
|
await reduceStreamingWithDispatch(completionStream, (props) => {
|
|
43
|
-
const event: AgenticaDescribeEvent
|
|
42
|
+
const event: AgenticaDescribeEvent = createDescribeEvent({
|
|
44
43
|
executes: histories,
|
|
45
44
|
...props,
|
|
46
45
|
});
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
|
|
3
1
|
import type { AgenticaContext } from "../context/AgenticaContext";
|
|
4
2
|
import type { AgenticaExecuteEvent } from "../events";
|
|
5
3
|
import type { IAgenticaExecutor } from "../structures/IAgenticaExecutor";
|
|
@@ -10,8 +8,8 @@ import { describe } from "./describe";
|
|
|
10
8
|
import { initialize } from "./initialize";
|
|
11
9
|
import { select } from "./select";
|
|
12
10
|
|
|
13
|
-
export function execute
|
|
14
|
-
return async (ctx: AgenticaContext
|
|
11
|
+
export function execute(executor: Partial<IAgenticaExecutor> | null) {
|
|
12
|
+
return async (ctx: AgenticaContext): Promise<void> => {
|
|
15
13
|
// FUNCTIONS ARE NOT LISTED YET
|
|
16
14
|
if (ctx.ready() === false) {
|
|
17
15
|
if (executor?.initialize !== true && typeof executor?.initialize !== "function") {
|
|
@@ -43,7 +41,7 @@ export function execute<Model extends ILlmSchema.Model>(executor: Partial<IAgent
|
|
|
43
41
|
// FUNCTION CALLING LOOP
|
|
44
42
|
while (true) {
|
|
45
43
|
// EXECUTE FUNCTIONS
|
|
46
|
-
const executes: AgenticaExecuteEvent
|
|
44
|
+
const executes: AgenticaExecuteEvent[] = await (
|
|
47
45
|
executor?.call ?? call
|
|
48
46
|
)(ctx, ctx.stack.map(s => s.operation));
|
|
49
47
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ILlmFunction
|
|
1
|
+
import type { ILlmFunction } from "@samchon/openapi";
|
|
2
2
|
import type OpenAI from "openai";
|
|
3
3
|
|
|
4
4
|
import typia from "typia";
|
|
@@ -13,12 +13,11 @@ import { createAssistantMessageEvent } from "../factory/events";
|
|
|
13
13
|
import { decodeHistory, decodeUserMessageContent } from "../factory/histories";
|
|
14
14
|
import { reduceStreamingWithDispatch } from "../utils/ChatGptCompletionStreamingUtil";
|
|
15
15
|
|
|
16
|
-
const FUNCTION: ILlmFunction
|
|
17
|
-
__IChatInitialApplication
|
|
18
|
-
"chatgpt"
|
|
16
|
+
const FUNCTION: ILlmFunction = typia.llm.application<
|
|
17
|
+
__IChatInitialApplication
|
|
19
18
|
>().functions[0]!;
|
|
20
19
|
|
|
21
|
-
export async function initialize
|
|
20
|
+
export async function initialize(ctx: AgenticaContext): Promise<void> {
|
|
22
21
|
// ----
|
|
23
22
|
// EXECUTE CHATGPT API
|
|
24
23
|
// ----
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
|
|
3
1
|
import type { AgenticaContext } from "../../context/AgenticaContext";
|
|
4
2
|
import type { AgenticaOperationSelection } from "../../context/AgenticaOperationSelection";
|
|
5
3
|
import type { __IChatFunctionReference } from "../../context/internal/__IChatFunctionReference";
|
|
@@ -11,8 +9,8 @@ import { createOperationSelection } from "../../factory/operations";
|
|
|
11
9
|
/**
|
|
12
10
|
* @internal
|
|
13
11
|
*/
|
|
14
|
-
export function cancelFunctionFromContext
|
|
15
|
-
ctx: AgenticaContext
|
|
12
|
+
export function cancelFunctionFromContext(
|
|
13
|
+
ctx: AgenticaContext,
|
|
16
14
|
reference: __IChatFunctionReference,
|
|
17
15
|
): void {
|
|
18
16
|
const index: number = ctx.stack.findIndex(
|
|
@@ -22,10 +20,10 @@ export function cancelFunctionFromContext<Model extends ILlmSchema.Model>(
|
|
|
22
20
|
return;
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
const item: AgenticaOperationSelection
|
|
23
|
+
const item: AgenticaOperationSelection = ctx.stack[index]!;
|
|
26
24
|
ctx.stack.splice(index, 1);
|
|
27
25
|
|
|
28
|
-
const event: AgenticaCancelEvent
|
|
26
|
+
const event: AgenticaCancelEvent = createCancelEvent({
|
|
29
27
|
selection: createOperationSelection({
|
|
30
28
|
operation: item.operation,
|
|
31
29
|
reason: reference.reason,
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
|
|
3
1
|
import type { AgenticaContext } from "../../context/AgenticaContext";
|
|
4
2
|
import type { AgenticaOperation } from "../../context/AgenticaOperation";
|
|
5
3
|
import type { AgenticaOperationSelection } from "../../context/AgenticaOperationSelection";
|
|
@@ -12,26 +10,24 @@ import { createOperationSelection } from "../../factory/operations";
|
|
|
12
10
|
/**
|
|
13
11
|
* @internal
|
|
14
12
|
*/
|
|
15
|
-
export function selectFunctionFromContext
|
|
16
|
-
|
|
17
|
-
>(
|
|
18
|
-
ctx: AgenticaContext<Model>,
|
|
13
|
+
export function selectFunctionFromContext(
|
|
14
|
+
ctx: AgenticaContext,
|
|
19
15
|
reference: __IChatFunctionReference,
|
|
20
16
|
): void {
|
|
21
|
-
const operation: AgenticaOperation
|
|
17
|
+
const operation: AgenticaOperation | undefined
|
|
22
18
|
= ctx.operations.flat.get(reference.name);
|
|
23
19
|
if (operation === undefined) {
|
|
24
20
|
return;
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
const selection: AgenticaOperationSelection
|
|
23
|
+
const selection: AgenticaOperationSelection
|
|
28
24
|
= createOperationSelection({
|
|
29
25
|
operation,
|
|
30
26
|
reason: reference.reason,
|
|
31
27
|
});
|
|
32
28
|
ctx.stack.push(selection);
|
|
33
29
|
|
|
34
|
-
const event: AgenticaSelectEvent
|
|
30
|
+
const event: AgenticaSelectEvent = createSelectEvent({
|
|
35
31
|
selection,
|
|
36
32
|
});
|
|
37
33
|
void ctx.dispatch(event).catch(() => {});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ILlmApplication
|
|
1
|
+
import type { ILlmApplication } from "@samchon/openapi";
|
|
2
2
|
import type OpenAI from "openai";
|
|
3
3
|
import type { IValidation } from "typia";
|
|
4
4
|
|
|
@@ -25,9 +25,8 @@ import { toAsyncGenerator } from "../utils/StreamUtil";
|
|
|
25
25
|
|
|
26
26
|
import { selectFunctionFromContext } from "./internal/selectFunctionFromContext";
|
|
27
27
|
|
|
28
|
-
const CONTAINER: ILlmApplication
|
|
29
|
-
__IChatSelectFunctionsApplication
|
|
30
|
-
"chatgpt"
|
|
28
|
+
const CONTAINER: ILlmApplication = typia.llm.application<
|
|
29
|
+
__IChatSelectFunctionsApplication
|
|
31
30
|
>();
|
|
32
31
|
|
|
33
32
|
interface IFailure {
|
|
@@ -36,16 +35,16 @@ interface IFailure {
|
|
|
36
35
|
validation: IValidation.IFailure;
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
export async function select
|
|
40
|
-
ctx: AgenticaContext
|
|
38
|
+
export async function select(
|
|
39
|
+
ctx: AgenticaContext,
|
|
41
40
|
): Promise<void> {
|
|
42
41
|
if (ctx.operations.divided === undefined) {
|
|
43
42
|
return step(ctx, ctx.operations.array, 0);
|
|
44
43
|
}
|
|
45
44
|
|
|
46
|
-
const stacks: AgenticaOperationSelection
|
|
45
|
+
const stacks: AgenticaOperationSelection[][]
|
|
47
46
|
= ctx.operations.divided.map(() => []);
|
|
48
|
-
const events: AgenticaEvent
|
|
47
|
+
const events: AgenticaEvent[] = [];
|
|
49
48
|
await Promise.all(
|
|
50
49
|
ctx.operations.divided.map(async (operations, i) =>
|
|
51
50
|
step(
|
|
@@ -81,7 +80,7 @@ export async function select<Model extends ILlmSchema.Model>(
|
|
|
81
80
|
);
|
|
82
81
|
}
|
|
83
82
|
else {
|
|
84
|
-
const selected: AgenticaSelectEvent
|
|
83
|
+
const selected: AgenticaSelectEvent[]
|
|
85
84
|
= events.filter(e => e.type === "select");
|
|
86
85
|
(selected.length !== 0 ? selected : events)
|
|
87
86
|
.forEach((e) => {
|
|
@@ -90,9 +89,9 @@ export async function select<Model extends ILlmSchema.Model>(
|
|
|
90
89
|
}
|
|
91
90
|
}
|
|
92
91
|
|
|
93
|
-
async function step
|
|
94
|
-
ctx: AgenticaContext
|
|
95
|
-
operations: AgenticaOperation
|
|
92
|
+
async function step(
|
|
93
|
+
ctx: AgenticaContext,
|
|
94
|
+
operations: AgenticaOperation[],
|
|
96
95
|
retry: number,
|
|
97
96
|
failures?: IFailure[],
|
|
98
97
|
): Promise<void> {
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { ILlmSchema } from "@samchon/openapi";
|
|
2
|
-
|
|
3
1
|
import type { AgenticaContext } from "../context/AgenticaContext";
|
|
4
2
|
|
|
5
3
|
import type { IAgenticaExecutor } from "./IAgenticaExecutor";
|
|
@@ -22,7 +20,7 @@ import type { IAgenticaSystemPrompt } from "./IAgenticaSystemPrompt";
|
|
|
22
20
|
*
|
|
23
21
|
* @author Samchon
|
|
24
22
|
*/
|
|
25
|
-
export interface IAgenticaConfig
|
|
23
|
+
export interface IAgenticaConfig {
|
|
26
24
|
/**
|
|
27
25
|
* Agent executor.
|
|
28
26
|
*
|
|
@@ -38,8 +36,8 @@ export interface IAgenticaConfig<Model extends ILlmSchema.Model> {
|
|
|
38
36
|
* @default ChatGptAgent.execute
|
|
39
37
|
*/
|
|
40
38
|
executor?:
|
|
41
|
-
| Partial<IAgenticaExecutor
|
|
42
|
-
| ((ctx: AgenticaContext
|
|
39
|
+
| Partial<IAgenticaExecutor>
|
|
40
|
+
| ((ctx: AgenticaContext) => Promise<void>);
|
|
43
41
|
|
|
44
42
|
/**
|
|
45
43
|
* System prompt messages.
|
|
@@ -47,7 +45,7 @@ export interface IAgenticaConfig<Model extends ILlmSchema.Model> {
|
|
|
47
45
|
* System prompt messages if you want to customize the system prompt
|
|
48
46
|
* messages for each situation.
|
|
49
47
|
*/
|
|
50
|
-
systemPrompt?: IAgenticaSystemPrompt
|
|
48
|
+
systemPrompt?: IAgenticaSystemPrompt;
|
|
51
49
|
|
|
52
50
|
/**
|
|
53
51
|
* Locale of the A.I. chatbot.
|