@agentica/core 0.20.0 → 0.22.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 (76) hide show
  1. package/README.md +115 -413
  2. package/lib/Agentica.d.ts +2 -1
  3. package/lib/Agentica.js +15 -13
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/MicroAgentica.d.ts +2 -1
  6. package/lib/MicroAgentica.js +16 -11
  7. package/lib/MicroAgentica.js.map +1 -1
  8. package/lib/context/AgenticaContext.d.ts +4 -4
  9. package/lib/context/MicroAgenticaContext.d.ts +2 -2
  10. package/lib/context/internal/AgenticaOperationComposer.js +1 -8
  11. package/lib/context/internal/AgenticaOperationComposer.js.map +1 -1
  12. package/lib/events/AgenticaEvent.d.ts +3 -1
  13. package/lib/events/AgenticaTextEvent.d.ts +2 -2
  14. package/lib/events/AgenticaUserInputEvent.d.ts +10 -0
  15. package/lib/events/AgenticaUserInputEvent.js +3 -0
  16. package/lib/events/AgenticaUserInputEvent.js.map +1 -0
  17. package/lib/events/MicroAgenticaEvent.d.ts +3 -1
  18. package/lib/factory/events.d.ts +7 -3
  19. package/lib/factory/events.js +29 -4
  20. package/lib/factory/events.js.map +1 -1
  21. package/lib/factory/histories.d.ts +6 -3
  22. package/lib/factory/histories.js +59 -32
  23. package/lib/factory/histories.js.map +1 -1
  24. package/lib/functional/assertMcpController.js +0 -2
  25. package/lib/functional/assertMcpController.js.map +1 -1
  26. package/lib/histories/AgenticaHistory.d.ts +3 -1
  27. package/lib/histories/AgenticaTextHistory.d.ts +2 -2
  28. package/lib/histories/AgenticaUserInputHistory.d.ts +80 -0
  29. package/lib/histories/AgenticaUserInputHistory.js +3 -0
  30. package/lib/histories/AgenticaUserInputHistory.js.map +1 -0
  31. package/lib/histories/MicroAgenticaHistory.d.ts +2 -1
  32. package/lib/index.mjs +138 -105
  33. package/lib/index.mjs.map +1 -1
  34. package/lib/json/IAgenticaEventJson.d.ts +8 -1
  35. package/lib/json/IAgenticaHistoryJson.d.ts +15 -3
  36. package/lib/orchestrate/call.js +3 -17
  37. package/lib/orchestrate/call.js.map +1 -1
  38. package/lib/orchestrate/cancel.js +1 -1
  39. package/lib/orchestrate/cancel.js.map +1 -1
  40. package/lib/orchestrate/execute.js +13 -7
  41. package/lib/orchestrate/execute.js.map +1 -1
  42. package/lib/orchestrate/initialize.js +2 -6
  43. package/lib/orchestrate/initialize.js.map +1 -1
  44. package/lib/orchestrate/select.js +2 -6
  45. package/lib/orchestrate/select.js.map +1 -1
  46. package/lib/structures/IAgenticaExecutor.d.ts +10 -6
  47. package/lib/structures/IMicroAgenticaExecutor.d.ts +4 -1
  48. package/lib/transformers/AgenticaEventTransformer.js +0 -1
  49. package/lib/transformers/AgenticaEventTransformer.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/Agentica.ts +21 -18
  52. package/src/MicroAgentica.ts +20 -16
  53. package/src/context/AgenticaContext.ts +4 -4
  54. package/src/context/MicroAgenticaContext.ts +2 -2
  55. package/src/context/internal/AgenticaOperationComposer.ts +5 -6
  56. package/src/events/AgenticaEvent.ts +4 -1
  57. package/src/events/AgenticaTextEvent.ts +2 -4
  58. package/src/events/AgenticaUserInputEvent.ts +12 -0
  59. package/src/events/MicroAgenticaEvent.ts +4 -1
  60. package/src/factory/events.ts +26 -8
  61. package/src/factory/histories.ts +76 -43
  62. package/src/functional/assertMcpController.ts +1 -2
  63. package/src/histories/AgenticaHistory.ts +4 -1
  64. package/src/histories/AgenticaTextHistory.ts +2 -4
  65. package/src/histories/AgenticaUserInputHistory.ts +88 -0
  66. package/src/histories/MicroAgenticaHistory.ts +3 -1
  67. package/src/json/IAgenticaEventJson.ts +9 -1
  68. package/src/json/IAgenticaHistoryJson.ts +16 -4
  69. package/src/orchestrate/call.ts +15 -17
  70. package/src/orchestrate/cancel.ts +1 -1
  71. package/src/orchestrate/execute.ts +13 -7
  72. package/src/orchestrate/initialize.ts +2 -6
  73. package/src/orchestrate/select.ts +2 -7
  74. package/src/structures/IAgenticaExecutor.ts +16 -8
  75. package/src/structures/IMicroAgenticaExecutor.ts +10 -4
  76. package/src/transformers/AgenticaEventTransformer.ts +0 -1
@@ -54,7 +54,7 @@ export async function call<Model extends ILlmSchema.Model>(
54
54
  // USER INPUT
55
55
  {
56
56
  role: "user",
57
- content: ctx.prompt.text,
57
+ content: ctx.prompt.contents,
58
58
  },
59
59
  // SYSTEM PROMPT
60
60
  ...(ctx.config?.systemPrompt?.execute === null
@@ -72,13 +72,13 @@ export async function call<Model extends ILlmSchema.Model>(
72
72
  type: "function",
73
73
  function: {
74
74
  name: s.name,
75
- // eslint-disable-next-line ts/no-unsafe-assignment, ts/no-unsafe-member-access
75
+
76
76
  description: s.function.description,
77
77
  parameters: (
78
78
  "separated" in s.function
79
- // eslint-disable-next-line ts/no-unsafe-member-access
79
+
80
80
  && s.function.separated !== undefined
81
- // eslint-disable-next-line ts/no-unsafe-member-access
81
+
82
82
  ? (s.function.separated.llm
83
83
  ?? ({
84
84
  type: "object",
@@ -87,7 +87,7 @@ export async function call<Model extends ILlmSchema.Model>(
87
87
  additionalProperties: false,
88
88
  $defs: {},
89
89
  } satisfies IChatGptSchema.IParameters))
90
- // eslint-disable-next-line ts/no-unsafe-member-access
90
+
91
91
  : s.function.parameters) as Record<string, any>,
92
92
  },
93
93
  }) as OpenAI.ChatCompletionTool,
@@ -181,13 +181,11 @@ export async function call<Model extends ILlmSchema.Model>(
181
181
  && choice.message.content.length !== 0
182
182
  ) {
183
183
  closures.push(async () => {
184
- const value: AgenticaTextHistory = createTextHistory({
185
- role: "assistant",
186
- text: choice.message.content!,
187
- });
184
+ const value: AgenticaTextHistory = createTextHistory(
185
+ { text: choice.message.content! },
186
+ );
188
187
  ctx.dispatch(
189
188
  createTextEvent({
190
- role: "assistant",
191
189
  get: () => value.text,
192
190
  done: () => true,
193
191
  stream: toAsyncGenerator(value.text),
@@ -444,9 +442,9 @@ async function executeMcpOperation<Model extends ILlmSchema.Model>(
444
442
  operationArguments: Record<string, unknown>,
445
443
  ): Promise<unknown> {
446
444
  return operation.controller.client.callTool({
447
- // eslint-disable-next-line ts/no-unsafe-member-access
445
+
448
446
  method: operation.function.name,
449
- // eslint-disable-next-line ts/no-unsafe-assignment, ts/no-unsafe-member-access
447
+
450
448
  name: operation.function.name,
451
449
  arguments: operationArguments,
452
450
  }).then(v => v.content);
@@ -473,7 +471,7 @@ async function correct<Model extends ILlmSchema.Model>(
473
471
  // USER INPUT
474
472
  {
475
473
  role: "user",
476
- content: ctx.prompt.text,
474
+ content: ctx.prompt.contents,
477
475
  },
478
476
  // TYPE CORRECTION
479
477
  ...(ctx.config?.systemPrompt?.execute === null
@@ -518,7 +516,7 @@ async function correct<Model extends ILlmSchema.Model>(
518
516
  type: "function",
519
517
  function: {
520
518
  name: call.operation.name,
521
- // eslint-disable-next-line ts/no-unsafe-assignment, ts/no-unsafe-member-access
519
+
522
520
  description: call.operation.function.description,
523
521
  /**
524
522
  * @TODO fix it
@@ -526,9 +524,9 @@ async function correct<Model extends ILlmSchema.Model>(
526
524
  */
527
525
  parameters: (
528
526
  "separated" in call.operation.function
529
- // eslint-disable-next-line ts/no-unsafe-member-access
527
+
530
528
  && call.operation.function.separated !== undefined
531
- // eslint-disable-next-line ts/no-unsafe-member-access
529
+
532
530
  ? (call.operation.function.separated?.llm
533
531
  ?? ({
534
532
  $defs: {},
@@ -537,7 +535,7 @@ async function correct<Model extends ILlmSchema.Model>(
537
535
  additionalProperties: false,
538
536
  required: [],
539
537
  } satisfies IChatGptSchema.IParameters))
540
- // eslint-disable-next-line ts/no-unsafe-member-access
538
+
541
539
  : call.operation.function.parameters) as unknown as Record<string, unknown>,
542
540
  },
543
541
  },
@@ -141,7 +141,7 @@ async function step<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>,
141
141
  // USER INPUT
142
142
  {
143
143
  role: "user",
144
- content: ctx.prompt.text,
144
+ content: ctx.prompt.contents,
145
145
  },
146
146
  // SYSTEM PROMPT
147
147
  {
@@ -17,13 +17,15 @@ export function execute<Model extends ILlmSchema.Model>(executor: Partial<IAgent
17
17
 
18
18
  // FUNCTIONS ARE NOT LISTED YET
19
19
  if (ctx.ready() === false) {
20
- if (executor?.initialize === null) {
20
+ if (executor?.initialize !== true && typeof executor?.initialize !== "function") {
21
21
  await ctx.initialize();
22
22
  }
23
23
  else {
24
24
  histories.push(
25
25
  ...(await (
26
- executor?.initialize ?? initialize
26
+ typeof executor?.initialize === "function"
27
+ ? executor.initialize
28
+ : initialize
27
29
  )(ctx)),
28
30
  );
29
31
  if (ctx.ready() === false) {
@@ -63,11 +65,15 @@ export function execute<Model extends ILlmSchema.Model>(executor: Partial<IAgent
63
65
  const executes: AgenticaExecuteHistory<Model>[] = prompts.filter(
64
66
  prompt => prompt.type === "execute",
65
67
  );
66
- histories.push(
67
- ...(await (
68
- executor?.describe ?? describe
69
- )(ctx, executes)),
70
- );
68
+ 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
+ );
76
+ }
71
77
  if (executes.length === 0 || ctx.stack.length === 0) {
72
78
  break;
73
79
  }
@@ -36,7 +36,7 @@ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaCo
36
36
  // USER INPUT
37
37
  {
38
38
  role: "user",
39
- content: ctx.prompt.text,
39
+ content: ctx.prompt.contents,
40
40
  },
41
41
  {
42
42
  // SYSTEM PROMPT
@@ -108,7 +108,6 @@ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaCo
108
108
 
109
109
  ctx.dispatch(
110
110
  createTextEvent({
111
- role: "assistant",
112
111
  stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
113
112
  done: () => mpsc.done(),
114
113
  get: () => textContext[choice.index]!.content,
@@ -145,10 +144,7 @@ export async function initialize<Model extends ILlmSchema.Model>(ctx: AgenticaCo
145
144
  && choice.message.content.length !== 0
146
145
  ) {
147
146
  prompts.push(
148
- createTextHistory({
149
- role: "assistant",
150
- text: choice.message.content,
151
- }),
147
+ createTextHistory({ text: choice.message.content }),
152
148
  );
153
149
  }
154
150
  }
@@ -13,7 +13,6 @@ import type { __IChatSelectFunctionsApplication } from "../context/internal/__IC
13
13
  import type { AgenticaEvent } from "../events/AgenticaEvent";
14
14
  import type { AgenticaHistory } from "../histories/AgenticaHistory";
15
15
  import type { AgenticaSelectHistory } from "../histories/AgenticaSelectHistory";
16
- import type { AgenticaTextHistory } from "../histories/AgenticaTextHistory";
17
16
 
18
17
  import { AgenticaConstant } from "../constants/AgenticaConstant";
19
18
  import { AgenticaDefaultPrompt } from "../constants/AgenticaDefaultPrompt";
@@ -145,7 +144,7 @@ async function step<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>,
145
144
  // USER INPUT
146
145
  {
147
146
  role: "user",
148
- content: ctx.prompt.text,
147
+ content: ctx.prompt.contents,
149
148
  },
150
149
  // SYSTEM PROMPT
151
150
  {
@@ -260,15 +259,11 @@ async function step<Model extends ILlmSchema.Model>(ctx: AgenticaContext<Model>,
260
259
  && choice.message.content != null
261
260
  && choice.message.content.length !== 0
262
261
  ) {
263
- const text: AgenticaTextHistory = createTextHistory({
264
- role: "assistant",
265
- text: choice.message.content,
266
- });
262
+ const text = createTextHistory({ text: choice.message.content });
267
263
  prompts.push(text);
268
264
 
269
265
  ctx.dispatch(
270
266
  createTextEvent({
271
- role: "assistant",
272
267
  stream: toAsyncGenerator(text.text),
273
268
  join: async () => Promise.resolve(text.text),
274
269
  done: () => true,
@@ -49,15 +49,17 @@ export interface IAgenticaExecutor<Model extends ILlmSchema.Model> {
49
49
  * conversate with the user.
50
50
  *
51
51
  * By the way, if you wanna skip the `initialize` agent, you can
52
- * do it by configuring the {@link IAgenticaConfig.executor} as
53
- * `null` value. In that case, the `initialize` agent will never be
54
- * called, and {@link Agentica} just starts from the {@link select}
55
- * agent.
52
+ * do it by configuring the {@link IAgenticaExecutor.initialize} as
53
+ * `undefined`, `false` or `null` value. In that case, the `initialize`
54
+ * agent will never be called, and {@link Agentica} just starts from the
55
+ * {@link select} agent.
56
56
  *
57
57
  * @param ctx Context of the agent
58
58
  * @returns List of prompts generated by the initializer
59
+ * @default false
59
60
  */
60
61
  initialize:
62
+ | boolean
61
63
  | null
62
64
  | ((ctx: AgenticaContext<Model>) => Promise<AgenticaHistory<Model>[]>);
63
65
 
@@ -124,14 +126,20 @@ export interface IAgenticaExecutor<Model extends ILlmSchema.Model> {
124
126
  * `Describe` agent explains the results of the function callings
125
127
  * to the user as markdown content.
126
128
  *
129
+ * If you configure this property as `false` or `null`, the describer
130
+ * agent will never be used.
131
+ *
127
132
  * @param ctx Context of the agent
128
133
  * @param executes List of function calling results
129
134
  * @returns List of prompts generated by the describer
130
135
  */
131
- describe: (
132
- ctx: AgenticaContext<Model>,
133
- executes: AgenticaExecuteHistory<Model>[],
134
- ) => Promise<AgenticaHistory<Model>[]>;
136
+ describe:
137
+ | boolean
138
+ | null
139
+ | ((
140
+ ctx: AgenticaContext<Model>,
141
+ executes: AgenticaExecuteHistory<Model>[],
142
+ ) => Promise<AgenticaHistory<Model>[]>);
135
143
 
136
144
  /**
137
145
  * Function canceler agent.
@@ -52,12 +52,18 @@ export interface IMicroAgenticaExecutor<Model extends ILlmSchema.Model> {
52
52
  * `Describe` agent explains the results of the function callings
53
53
  * to the user as markdown content.
54
54
  *
55
+ * If you configure this property as `false` or `null`, the describer
56
+ * agent never be used.
57
+ *
55
58
  * @param ctx Context of the agent
56
59
  * @param executes List of function calling results
57
60
  * @returns List of prompts generated by the describer
58
61
  */
59
- describe: (
60
- ctx: MicroAgenticaContext<Model>,
61
- executes: AgenticaExecuteHistory<Model>[],
62
- ) => Promise<MicroAgenticaHistory<Model>[]>;
62
+ describe:
63
+ | boolean
64
+ | null
65
+ | ((
66
+ ctx: MicroAgenticaContext<Model>,
67
+ executes: AgenticaExecuteHistory<Model>[],
68
+ ) => Promise<MicroAgenticaHistory<Model>[]>);
63
69
  }
@@ -175,7 +175,6 @@ function transformText(props: {
175
175
  event: IAgenticaEventJson.IText;
176
176
  }): AgenticaTextEvent {
177
177
  return createTextEvent({
178
- role: props.event.role,
179
178
  stream: toAsyncGenerator(props.event.text),
180
179
  done: () => true,
181
180
  get: () => props.event.text,