@agentica/core 0.21.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 (62) hide show
  1. package/lib/Agentica.d.ts +2 -1
  2. package/lib/Agentica.js +15 -13
  3. package/lib/Agentica.js.map +1 -1
  4. package/lib/MicroAgentica.d.ts +2 -1
  5. package/lib/MicroAgentica.js +12 -10
  6. package/lib/MicroAgentica.js.map +1 -1
  7. package/lib/context/AgenticaContext.d.ts +4 -4
  8. package/lib/context/MicroAgenticaContext.d.ts +2 -2
  9. package/lib/events/AgenticaEvent.d.ts +3 -1
  10. package/lib/events/AgenticaTextEvent.d.ts +2 -2
  11. package/lib/events/AgenticaUserInputEvent.d.ts +10 -0
  12. package/lib/events/AgenticaUserInputEvent.js +3 -0
  13. package/lib/events/AgenticaUserInputEvent.js.map +1 -0
  14. package/lib/events/MicroAgenticaEvent.d.ts +3 -1
  15. package/lib/factory/events.d.ts +7 -3
  16. package/lib/factory/events.js +29 -4
  17. package/lib/factory/events.js.map +1 -1
  18. package/lib/factory/histories.d.ts +6 -3
  19. package/lib/factory/histories.js +59 -32
  20. package/lib/factory/histories.js.map +1 -1
  21. package/lib/histories/AgenticaHistory.d.ts +3 -1
  22. package/lib/histories/AgenticaTextHistory.d.ts +2 -2
  23. package/lib/histories/AgenticaUserInputHistory.d.ts +80 -0
  24. package/lib/histories/AgenticaUserInputHistory.js +3 -0
  25. package/lib/histories/AgenticaUserInputHistory.js.map +1 -0
  26. package/lib/histories/MicroAgenticaHistory.d.ts +2 -1
  27. package/lib/index.mjs +132 -101
  28. package/lib/index.mjs.map +1 -1
  29. package/lib/json/IAgenticaEventJson.d.ts +8 -1
  30. package/lib/json/IAgenticaHistoryJson.d.ts +15 -3
  31. package/lib/orchestrate/call.js +3 -7
  32. package/lib/orchestrate/call.js.map +1 -1
  33. package/lib/orchestrate/cancel.js +1 -1
  34. package/lib/orchestrate/cancel.js.map +1 -1
  35. package/lib/orchestrate/initialize.js +2 -6
  36. package/lib/orchestrate/initialize.js.map +1 -1
  37. package/lib/orchestrate/select.js +2 -6
  38. package/lib/orchestrate/select.js.map +1 -1
  39. package/lib/transformers/AgenticaEventTransformer.js +0 -1
  40. package/lib/transformers/AgenticaEventTransformer.js.map +1 -1
  41. package/package.json +2 -2
  42. package/src/Agentica.ts +21 -18
  43. package/src/MicroAgentica.ts +17 -15
  44. package/src/context/AgenticaContext.ts +4 -4
  45. package/src/context/MicroAgenticaContext.ts +2 -2
  46. package/src/events/AgenticaEvent.ts +4 -1
  47. package/src/events/AgenticaTextEvent.ts +2 -4
  48. package/src/events/AgenticaUserInputEvent.ts +12 -0
  49. package/src/events/MicroAgenticaEvent.ts +4 -1
  50. package/src/factory/events.ts +26 -8
  51. package/src/factory/histories.ts +76 -43
  52. package/src/histories/AgenticaHistory.ts +4 -1
  53. package/src/histories/AgenticaTextHistory.ts +2 -4
  54. package/src/histories/AgenticaUserInputHistory.ts +88 -0
  55. package/src/histories/MicroAgenticaHistory.ts +3 -1
  56. package/src/json/IAgenticaEventJson.ts +9 -1
  57. package/src/json/IAgenticaHistoryJson.ts +16 -4
  58. package/src/orchestrate/call.ts +5 -7
  59. package/src/orchestrate/cancel.ts +1 -1
  60. package/src/orchestrate/initialize.ts +2 -6
  61. package/src/orchestrate/select.ts +2 -7
  62. package/src/transformers/AgenticaEventTransformer.ts +0 -1
@@ -9,6 +9,7 @@ import type { AgenticaExecuteHistory } from "../histories/AgenticaExecuteHistory
9
9
  import type { AgenticaHistory } from "../histories/AgenticaHistory";
10
10
  import type { AgenticaSelectHistory } from "../histories/AgenticaSelectHistory";
11
11
  import type { AgenticaTextHistory } from "../histories/AgenticaTextHistory";
12
+ import type { AgenticaUserInputHistory } from "../histories/AgenticaUserInputHistory";
12
13
  import type { IAgenticaHistoryJson } from "../json/IAgenticaHistoryJson";
13
14
 
14
15
  export function decodeHistory<Model extends ILlmSchema.Model>(history: AgenticaHistory<Model>): OpenAI.ChatCompletionMessageParam[] {
@@ -16,7 +17,8 @@ export function decodeHistory<Model extends ILlmSchema.Model>(history: AgenticaH
16
17
  if (history.type === "describe") {
17
18
  return [];
18
19
  }
19
- else if (history.type === "text") {
20
+
21
+ if (history.type === "text") {
20
22
  return [
21
23
  {
22
24
  role: history.role,
@@ -24,7 +26,8 @@ export function decodeHistory<Model extends ILlmSchema.Model>(history: AgenticaH
24
26
  },
25
27
  ];
26
28
  }
27
- else if (history.type === "select" || history.type === "cancel") {
29
+
30
+ if (history.type === "select" || history.type === "cancel") {
28
31
  return [
29
32
  {
30
33
  role: "assistant",
@@ -52,59 +55,89 @@ export function decodeHistory<Model extends ILlmSchema.Model>(history: AgenticaH
52
55
  ];
53
56
  }
54
57
 
55
- return [
56
- {
57
- role: "assistant",
58
- tool_calls: [
59
- {
60
- type: "function",
61
- id: history.id,
58
+ if (history.type === "execute") {
59
+ return [
60
+ {
61
+ role: "assistant",
62
+ tool_calls: [
63
+ {
64
+ type: "function",
65
+ id: history.id,
66
+ function: {
67
+ name: history.operation.name,
68
+ arguments: JSON.stringify(history.arguments),
69
+ },
70
+ },
71
+ ],
72
+ },
73
+ {
74
+ role: "tool",
75
+ tool_call_id: history.id,
76
+ content: JSON.stringify({
62
77
  function: {
63
- name: history.operation.name,
64
- arguments: JSON.stringify(history.arguments),
78
+ protocol: history.operation.protocol,
79
+ description: history.operation.function.description,
80
+ parameters: history.operation.function.parameters,
81
+ output: history.operation.function.output,
82
+ ...(history.operation.protocol === "http"
83
+ ? {
84
+ method: history.operation.function.method,
85
+ path: history.operation.function.path,
86
+ }
87
+ : {}),
65
88
  },
66
- },
67
- ],
68
- },
69
- {
70
- role: "tool",
71
- tool_call_id: history.id,
72
- content: JSON.stringify({
73
- function: {
74
- protocol: history.operation.protocol,
75
- description: history.operation.function.description,
76
- parameters: history.operation.function.parameters,
77
- output: history.operation.function.output,
78
89
  ...(history.operation.protocol === "http"
79
90
  ? {
80
- method: history.operation.function.method,
81
- path: history.operation.function.path,
91
+ status: (history.value as IHttpResponse).status,
92
+ data: (history.value as IHttpResponse).body,
82
93
  }
83
- : {}),
84
- },
85
- ...(history.operation.protocol === "http"
86
- ? {
87
- status: (history.value as IHttpResponse).status,
88
- data: (history.value as IHttpResponse).body,
89
- }
90
- : {
91
- value: history.value,
92
- }),
93
- }),
94
- },
95
- ];
94
+ : {
95
+ value: history.value,
96
+ }),
97
+ }),
98
+ },
99
+ ];
100
+ }
101
+
102
+ if (history.type === "user_input") {
103
+ return [
104
+ {
105
+ role: "user",
106
+ content: history.contents,
107
+ },
108
+ ];
109
+ }
110
+
111
+ history satisfies never;
112
+ throw new Error("Invalid history type");
113
+ }
114
+
115
+ /* -----------------------------------------------------------
116
+ USER INPUT PROMPTS
117
+ ----------------------------------------------------------- */
118
+ export function createUserInputHistory(props: {
119
+ contents: Array<AgenticaUserInputHistory.Contents>;
120
+ }): AgenticaUserInputHistory {
121
+ return {
122
+ type: "user_input",
123
+ role: "user",
124
+ contents: props.contents,
125
+ toJSON: () => ({
126
+ type: "user_input",
127
+ contents: props.contents,
128
+ }),
129
+ };
96
130
  }
97
131
 
98
132
  /* -----------------------------------------------------------
99
133
  TEXT PROMPTS
100
134
  ----------------------------------------------------------- */
101
- export function createTextHistory<Role extends "assistant" | "user" = "assistant" | "user">(props: {
102
- role: Role;
135
+ export function createTextHistory(props: {
103
136
  text: string;
104
- }): AgenticaTextHistory<Role> {
105
- const prompt: IAgenticaHistoryJson.IText<Role> = {
137
+ }): AgenticaTextHistory {
138
+ const prompt: IAgenticaHistoryJson.IText = {
106
139
  type: "text",
107
- role: props.role,
140
+ role: "assistant",
108
141
  text: props.text,
109
142
  };
110
143
  return {
@@ -5,13 +5,15 @@ import type { AgenticaDescribeHistory } from "./AgenticaDescribeHistory";
5
5
  import type { AgenticaExecuteHistory } from "./AgenticaExecuteHistory";
6
6
  import type { AgenticaSelectHistory } from "./AgenticaSelectHistory";
7
7
  import type { AgenticaTextHistory } from "./AgenticaTextHistory";
8
+ import type { AgenticaUserInputHistory } from "./AgenticaUserInputHistory";
8
9
 
9
10
  export type AgenticaHistory<Model extends ILlmSchema.Model> =
10
11
  | AgenticaCancelHistory<Model>
11
12
  | AgenticaDescribeHistory<Model>
12
13
  | AgenticaExecuteHistory<Model>
13
14
  | AgenticaSelectHistory<Model>
14
- | AgenticaTextHistory;
15
+ | AgenticaTextHistory
16
+ | AgenticaUserInputHistory;
15
17
  export namespace AgenticaHistory {
16
18
  export type Type = AgenticaHistory<any>["type"];
17
19
  export interface Mapper<Model extends ILlmSchema.Model> {
@@ -20,5 +22,6 @@ export namespace AgenticaHistory {
20
22
  execute: AgenticaExecuteHistory<Model>;
21
23
  select: AgenticaSelectHistory<Model>;
22
24
  text: AgenticaTextHistory;
25
+ user_input: AgenticaUserInputHistory;
23
26
  }
24
27
  }
@@ -2,9 +2,7 @@ import type { IAgenticaHistoryJson } from "../json/IAgenticaHistoryJson";
2
2
 
3
3
  import type { AgenticaHistoryBase } from "./AgenticaHistoryBase";
4
4
 
5
- export interface AgenticaTextHistory<
6
- Role extends "assistant" | "user" = "assistant" | "user",
7
- > extends AgenticaHistoryBase<"text", IAgenticaHistoryJson.IText> {
8
- role: Role;
5
+ export interface AgenticaTextHistory extends AgenticaHistoryBase<"text", IAgenticaHistoryJson.IText> {
6
+ role: "assistant";
9
7
  text: string;
10
8
  }
@@ -0,0 +1,88 @@
1
+ import type { IAgenticaHistoryJson } from "../json/IAgenticaHistoryJson";
2
+
3
+ import type { AgenticaHistoryBase } from "./AgenticaHistoryBase";
4
+
5
+ export interface AgenticaUserInputHistory extends AgenticaHistoryBase<"user_input", IAgenticaHistoryJson.IUserInput> {
6
+ role: "user";
7
+ contents: Array<AgenticaUserInputHistory.Contents>;
8
+ }
9
+
10
+ export namespace AgenticaUserInputHistory {
11
+ export type Contents = Contents.File | Contents.Image | Contents.InputAudio | Contents.Text;
12
+ export namespace Contents {
13
+ interface ContentsBase<Type extends string> {
14
+ /**
15
+ * The type of the content part.
16
+ */
17
+ type: Type;
18
+ }
19
+ /**
20
+ * Learn about
21
+ * [text inputs](https://platform.openai.com/docs/guides/text-generation).
22
+ */
23
+ export interface Text extends ContentsBase<"text"> {
24
+ /**
25
+ * The text content.
26
+ */
27
+ text: string;
28
+ }
29
+
30
+ /**
31
+ * Learn about [image inputs](https://platform.openai.com/docs/guides/vision).
32
+ */
33
+ export interface Image extends ContentsBase<"image_url"> {
34
+ image_url: {
35
+ /**
36
+ * Either a URL of the image or the base64 encoded image data.
37
+ */
38
+ url: string;
39
+ /**
40
+ * Specifies the detail level of the image. Learn more in the
41
+ * [Vision guide](https://platform.openai.com/docs/guides/vision#low-or-high-fidelity-image-understanding).
42
+ */
43
+ detail?: "auto" | "high" | "low";
44
+ };
45
+ }
46
+
47
+ /**
48
+ * Learn about [audio inputs](https://platform.openai.com/docs/guides/audio).
49
+ */
50
+ export interface InputAudio extends ContentsBase<"input_audio"> {
51
+ input_audio: {
52
+ /**
53
+ * Base64 encoded audio data.
54
+ */
55
+ data: string;
56
+
57
+ /**
58
+ * The format of the encoded audio data. Currently supports "wav" and "mp3".
59
+ */
60
+ format: "wav" | "mp3";
61
+ };
62
+ }
63
+
64
+ /**
65
+ * Learn about [file inputs](https://platform.openai.com/docs/guides/text) for text
66
+ * generation.
67
+ */
68
+ export interface File extends ContentsBase<"file"> {
69
+ file: {
70
+ /**
71
+ * The base64 encoded file data, used when passing the file to the model as a
72
+ * string.
73
+ */
74
+ file_data?: string;
75
+
76
+ /**
77
+ * The ID of an uploaded file to use as input.
78
+ */
79
+ file_id?: string;
80
+
81
+ /**
82
+ * The name of the file, used when passing the file to the model as a string.
83
+ */
84
+ filename?: string;
85
+ };
86
+ }
87
+ }
88
+ }
@@ -3,11 +3,13 @@ import type { ILlmSchema } from "@samchon/openapi";
3
3
  import type { AgenticaDescribeHistory } from "./AgenticaDescribeHistory";
4
4
  import type { AgenticaExecuteHistory } from "./AgenticaExecuteHistory";
5
5
  import type { AgenticaTextHistory } from "./AgenticaTextHistory";
6
+ import type { AgenticaUserInputHistory } from "./AgenticaUserInputHistory";
6
7
 
7
8
  export type MicroAgenticaHistory<Model extends ILlmSchema.Model> =
8
9
  | AgenticaDescribeHistory<Model>
9
10
  | AgenticaExecuteHistory<Model>
10
- | AgenticaTextHistory;
11
+ | AgenticaTextHistory
12
+ | AgenticaUserInputHistory;
11
13
  export namespace MicroAgenticaHistory {
12
14
  export type Type = MicroAgenticaHistory<any>["type"];
13
15
  export interface Mapper<Model extends ILlmSchema.Model> {
@@ -1,6 +1,7 @@
1
1
  import type OpenAI from "openai";
2
2
 
3
3
  import type { AgenticaEventSource } from "../events/AgenticaEventSource";
4
+ import type { AgenticaUserInputHistory } from "../histories/AgenticaUserInputHistory";
4
5
 
5
6
  import type { IAgenticaHistoryJson } from "./IAgenticaHistoryJson";
6
7
  import type { IAgenticaOperationJson } from "./IAgenticaOperationJson";
@@ -39,6 +40,13 @@ export namespace IAgenticaEventJson {
39
40
  request: IRequest;
40
41
  }
41
42
 
43
+ /**
44
+ * Event of user input.
45
+ */
46
+ export interface IUserInput extends IBase<"user_input"> {
47
+ contents: Array<AgenticaUserInputHistory.Contents>;
48
+ }
49
+
42
50
  /**
43
51
  * Event of initializing the chatbot.
44
52
  */
@@ -140,7 +148,7 @@ export namespace IAgenticaEventJson {
140
148
  /**
141
149
  * Role of the orator.
142
150
  */
143
- role: "assistant" | "user";
151
+ role: "assistant";
144
152
 
145
153
  /**
146
154
  * Conversation text.
@@ -1,3 +1,5 @@
1
+ import type { AgenticaUserInputHistory } from "../histories/AgenticaUserInputHistory";
2
+
1
3
  import type { IAgenticaOperationJson } from "./IAgenticaOperationJson";
2
4
  import type { IAgenticaOperationSelectionJson } from "./IAgenticaOperationSelectionJson";
3
5
 
@@ -18,12 +20,24 @@ import type { IAgenticaOperationSelectionJson } from "./IAgenticaOperationSelect
18
20
  * @author Samchon
19
21
  */
20
22
  export type IAgenticaHistoryJson =
23
+ | IAgenticaHistoryJson.IUserInput
21
24
  | IAgenticaHistoryJson.IText
22
25
  | IAgenticaHistoryJson.ISelect
23
26
  | IAgenticaHistoryJson.ICancel
24
27
  | IAgenticaHistoryJson.IExecute
25
28
  | IAgenticaHistoryJson.IDescribe;
26
29
  export namespace IAgenticaHistoryJson {
30
+ /**
31
+ * User input prompt.
32
+ *
33
+ * User input prompt about the user's input.
34
+ */
35
+ export interface IUserInput extends IBase<"user_input"> {
36
+ /**
37
+ * User input.
38
+ */
39
+ contents: Array<AgenticaUserInputHistory.Contents>;
40
+ }
27
41
  /**
28
42
  * Select prompt.
29
43
  *
@@ -107,13 +121,11 @@ export namespace IAgenticaHistoryJson {
107
121
  /**
108
122
  * Text prompt.
109
123
  */
110
- export interface IText<
111
- Role extends "assistant" | "user" = "assistant" | "user",
112
- > extends IBase<"text"> {
124
+ export interface IText extends IBase<"text"> {
113
125
  /**
114
126
  * Role of the orator.
115
127
  */
116
- role: Role;
128
+ role: "assistant";
117
129
 
118
130
  /**
119
131
  * The text content.
@@ -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
@@ -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),
@@ -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
@@ -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
  {
@@ -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,
@@ -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,