@agentica/core 0.12.0 → 0.12.2-dev.20250314

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 (83) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +461 -461
  3. package/lib/Agentica.js +3 -1
  4. package/lib/Agentica.js.map +1 -1
  5. package/lib/context/internal/AgenticaTokenUsageAggregator.js +1 -1
  6. package/lib/context/internal/AgenticaTokenUsageAggregator.js.map +1 -1
  7. package/lib/index.mjs +5 -2
  8. package/lib/index.mjs.map +1 -1
  9. package/package.json +1 -1
  10. package/prompts/cancel.md +4 -4
  11. package/prompts/common.md +2 -2
  12. package/prompts/describe.md +6 -6
  13. package/prompts/execute.md +6 -6
  14. package/prompts/initialize.md +2 -2
  15. package/prompts/select.md +6 -6
  16. package/src/Agentica.ts +359 -356
  17. package/src/chatgpt/ChatGptAgent.ts +76 -76
  18. package/src/chatgpt/ChatGptCallFunctionAgent.ts +466 -466
  19. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +280 -280
  20. package/src/chatgpt/ChatGptCompletionMessageUtil.ts +166 -166
  21. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +122 -122
  22. package/src/chatgpt/ChatGptHistoryDecoder.ts +88 -88
  23. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +96 -96
  24. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +311 -311
  25. package/src/chatgpt/ChatGptUsageAggregator.ts +62 -62
  26. package/src/context/AgenticaCancelPrompt.ts +32 -32
  27. package/src/context/AgenticaClassOperation.ts +23 -23
  28. package/src/context/AgenticaContext.ts +130 -130
  29. package/src/context/AgenticaHttpOperation.ts +27 -27
  30. package/src/context/AgenticaOperation.ts +66 -66
  31. package/src/context/AgenticaOperationBase.ts +57 -57
  32. package/src/context/AgenticaOperationCollection.ts +52 -52
  33. package/src/context/AgenticaOperationSelection.ts +27 -27
  34. package/src/context/AgenticaTokenUsage.ts +170 -170
  35. package/src/context/internal/AgenticaTokenUsageAggregator.ts +66 -66
  36. package/src/context/internal/__IChatCancelFunctionsApplication.ts +23 -23
  37. package/src/context/internal/__IChatFunctionReference.ts +21 -21
  38. package/src/context/internal/__IChatInitialApplication.ts +15 -15
  39. package/src/context/internal/__IChatSelectFunctionsApplication.ts +24 -24
  40. package/src/events/AgenticaCallEvent.ts +36 -36
  41. package/src/events/AgenticaCancelEvent.ts +28 -28
  42. package/src/events/AgenticaDescribeEvent.ts +66 -66
  43. package/src/events/AgenticaEvent.ts +36 -36
  44. package/src/events/AgenticaEventBase.ts +7 -7
  45. package/src/events/AgenticaEventSource.ts +6 -6
  46. package/src/events/AgenticaExecuteEvent.ts +50 -50
  47. package/src/events/AgenticaInitializeEvent.ts +14 -14
  48. package/src/events/AgenticaRequestEvent.ts +45 -45
  49. package/src/events/AgenticaResponseEvent.ts +48 -48
  50. package/src/events/AgenticaSelectEvent.ts +37 -37
  51. package/src/events/AgenticaTextEvent.ts +62 -62
  52. package/src/functional/assertHttpLlmApplication.ts +55 -55
  53. package/src/functional/validateHttpLlmApplication.ts +66 -66
  54. package/src/index.ts +44 -44
  55. package/src/internal/AgenticaConstant.ts +4 -4
  56. package/src/internal/AgenticaDefaultPrompt.ts +43 -43
  57. package/src/internal/AgenticaOperationComposer.ts +96 -96
  58. package/src/internal/ByteArrayUtil.ts +5 -5
  59. package/src/internal/MPSCUtil.ts +111 -111
  60. package/src/internal/MathUtil.ts +3 -3
  61. package/src/internal/Singleton.ts +22 -22
  62. package/src/internal/StreamUtil.ts +64 -64
  63. package/src/internal/__map_take.ts +15 -15
  64. package/src/json/IAgenticaEventJson.ts +178 -178
  65. package/src/json/IAgenticaOperationJson.ts +36 -36
  66. package/src/json/IAgenticaOperationSelectionJson.ts +19 -19
  67. package/src/json/IAgenticaPromptJson.ts +130 -130
  68. package/src/json/IAgenticaTokenUsageJson.ts +107 -107
  69. package/src/prompts/AgenticaCancelPrompt.ts +32 -32
  70. package/src/prompts/AgenticaDescribePrompt.ts +41 -41
  71. package/src/prompts/AgenticaExecutePrompt.ts +52 -52
  72. package/src/prompts/AgenticaPrompt.ts +14 -14
  73. package/src/prompts/AgenticaPromptBase.ts +27 -27
  74. package/src/prompts/AgenticaSelectPrompt.ts +32 -32
  75. package/src/prompts/AgenticaTextPrompt.ts +31 -31
  76. package/src/structures/IAgenticaConfig.ts +123 -123
  77. package/src/structures/IAgenticaController.ts +133 -133
  78. package/src/structures/IAgenticaExecutor.ts +157 -157
  79. package/src/structures/IAgenticaProps.ts +69 -69
  80. package/src/structures/IAgenticaSystemPrompt.ts +125 -125
  81. package/src/structures/IAgenticaVendor.ts +39 -39
  82. package/src/transformers/AgenticaEventTransformer.ts +165 -165
  83. package/src/transformers/AgenticaPromptTransformer.ts +134 -134
@@ -1,165 +1,165 @@
1
- import { ILlmSchema } from "@samchon/openapi";
2
-
3
- import { AgenticaOperation } from "../context/AgenticaOperation";
4
- import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
5
- import { AgenticaCallEvent } from "../events/AgenticaCallEvent";
6
- import { AgenticaCancelEvent } from "../events/AgenticaCancelEvent";
7
- import { AgenticaDescribeEvent } from "../events/AgenticaDescribeEvent";
8
- import { AgenticaEvent } from "../events/AgenticaEvent";
9
- import { AgenticaExecuteEvent } from "../events/AgenticaExecuteEvent";
10
- import { AgenticaInitializeEvent } from "../events/AgenticaInitializeEvent";
11
- import { AgenticaRequestEvent } from "../events/AgenticaRequestEvent";
12
- import { AgenticaSelectEvent } from "../events/AgenticaSelectEvent";
13
- import { AgenticaTextEvent } from "../events/AgenticaTextEvent";
14
- import { StreamUtil } from "../internal/StreamUtil";
15
- import { IAgenticaEventJson } from "../json/IAgenticaEventJson";
16
-
17
- export namespace AgenticaEventTransformer {
18
- export const transform = <Model extends ILlmSchema.Model>(props: {
19
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
20
- event: IAgenticaEventJson;
21
- }): AgenticaEvent<Model> => {
22
- if (props.event.type === "call")
23
- return transformCall({
24
- operations: props.operations,
25
- event: props.event,
26
- });
27
- else if (props.event.type === "cancel")
28
- return transformCancel({
29
- operations: props.operations,
30
- event: props.event,
31
- });
32
- else if (props.event.type === "describe")
33
- return transformDescribe({
34
- operations: props.operations,
35
- event: props.event,
36
- });
37
- else if (props.event.type === "execute")
38
- return transformExecute({
39
- operations: props.operations,
40
- event: props.event,
41
- });
42
- else if (props.event.type === "initialize") return transformInitialize();
43
- else if (props.event.type === "request")
44
- return transformRequest({
45
- event: props.event,
46
- });
47
- else if (props.event.type === "select")
48
- return transformSelect({
49
- operations: props.operations,
50
- event: props.event,
51
- });
52
- else if (props.event.type === "text")
53
- return transformText({
54
- event: props.event,
55
- });
56
- else throw new Error("Unknown event type");
57
- };
58
-
59
- export const transformCall = <Model extends ILlmSchema.Model>(props: {
60
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
61
- event: IAgenticaEventJson.ICall;
62
- }): AgenticaCallEvent<Model> =>
63
- new AgenticaCallEvent({
64
- id: props.event.id,
65
- operation: findOperation({
66
- operations: props.operations,
67
- input: props.event.operation,
68
- }),
69
- arguments: props.event.arguments,
70
- });
71
-
72
- export const transformCancel = <Model extends ILlmSchema.Model>(props: {
73
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
74
- event: IAgenticaEventJson.ICancel;
75
- }): AgenticaCancelEvent<Model> =>
76
- new AgenticaCancelEvent({
77
- selection: new AgenticaOperationSelection({
78
- operation: findOperation({
79
- operations: props.operations,
80
- input: props.event.selection.operation,
81
- }),
82
- reason: props.event.selection.reason,
83
- }),
84
- });
85
-
86
- export const transformDescribe = <Model extends ILlmSchema.Model>(props: {
87
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
88
- event: IAgenticaEventJson.IDescribe;
89
- }): AgenticaDescribeEvent<Model> =>
90
- new AgenticaDescribeEvent({
91
- executes: props.event.executes.map((next) =>
92
- transformExecute({
93
- operations: props.operations,
94
- event: next,
95
- }),
96
- ),
97
- stream: StreamUtil.to(props.event.text),
98
- done: () => true,
99
- get: () => props.event.text,
100
- join: async () => props.event.text,
101
- });
102
-
103
- export const transformExecute = <Model extends ILlmSchema.Model>(props: {
104
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
105
- event: IAgenticaEventJson.IExecute;
106
- }): AgenticaExecuteEvent<Model> =>
107
- new AgenticaExecuteEvent({
108
- id: props.event.id,
109
- operation: findOperation({
110
- operations: props.operations,
111
- input: props.event.operation,
112
- }),
113
- arguments: props.event.arguments,
114
- value: props.event.value,
115
- });
116
-
117
- export const transformInitialize = (): AgenticaInitializeEvent =>
118
- new AgenticaInitializeEvent();
119
-
120
- export const transformRequest = (props: {
121
- event: IAgenticaEventJson.IRequest;
122
- }): AgenticaRequestEvent => new AgenticaRequestEvent(props.event);
123
-
124
- export const transformSelect = <Model extends ILlmSchema.Model>(props: {
125
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
126
- event: IAgenticaEventJson.ISelect;
127
- }): AgenticaSelectEvent<Model> =>
128
- new AgenticaSelectEvent({
129
- selection: new AgenticaOperationSelection({
130
- operation: findOperation({
131
- operations: props.operations,
132
- input: props.event.selection.operation,
133
- }),
134
- reason: props.event.selection.reason,
135
- }),
136
- });
137
-
138
- export const transformText = (props: {
139
- event: IAgenticaEventJson.IText;
140
- }): AgenticaTextEvent =>
141
- new AgenticaTextEvent({
142
- role: props.event.role,
143
- stream: StreamUtil.to(props.event.text),
144
- done: () => true,
145
- get: () => props.event.text,
146
- join: async () => props.event.text,
147
- });
148
- }
149
-
150
- const findOperation = <Model extends ILlmSchema.Model>(props: {
151
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
152
- input: {
153
- controller: string;
154
- function: string;
155
- };
156
- }): AgenticaOperation<Model> => {
157
- const found: AgenticaOperation<Model> | undefined = props.operations
158
- .get(props.input.controller)
159
- ?.get(props.input.function);
160
- if (found === undefined)
161
- throw new Error(
162
- `No operation found: (controller: ${props.input.controller}, function: ${props.input.function})`,
163
- );
164
- return found;
165
- };
1
+ import { ILlmSchema } from "@samchon/openapi";
2
+
3
+ import { AgenticaOperation } from "../context/AgenticaOperation";
4
+ import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
5
+ import { AgenticaCallEvent } from "../events/AgenticaCallEvent";
6
+ import { AgenticaCancelEvent } from "../events/AgenticaCancelEvent";
7
+ import { AgenticaDescribeEvent } from "../events/AgenticaDescribeEvent";
8
+ import { AgenticaEvent } from "../events/AgenticaEvent";
9
+ import { AgenticaExecuteEvent } from "../events/AgenticaExecuteEvent";
10
+ import { AgenticaInitializeEvent } from "../events/AgenticaInitializeEvent";
11
+ import { AgenticaRequestEvent } from "../events/AgenticaRequestEvent";
12
+ import { AgenticaSelectEvent } from "../events/AgenticaSelectEvent";
13
+ import { AgenticaTextEvent } from "../events/AgenticaTextEvent";
14
+ import { StreamUtil } from "../internal/StreamUtil";
15
+ import { IAgenticaEventJson } from "../json/IAgenticaEventJson";
16
+
17
+ export namespace AgenticaEventTransformer {
18
+ export const transform = <Model extends ILlmSchema.Model>(props: {
19
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
20
+ event: IAgenticaEventJson;
21
+ }): AgenticaEvent<Model> => {
22
+ if (props.event.type === "call")
23
+ return transformCall({
24
+ operations: props.operations,
25
+ event: props.event,
26
+ });
27
+ else if (props.event.type === "cancel")
28
+ return transformCancel({
29
+ operations: props.operations,
30
+ event: props.event,
31
+ });
32
+ else if (props.event.type === "describe")
33
+ return transformDescribe({
34
+ operations: props.operations,
35
+ event: props.event,
36
+ });
37
+ else if (props.event.type === "execute")
38
+ return transformExecute({
39
+ operations: props.operations,
40
+ event: props.event,
41
+ });
42
+ else if (props.event.type === "initialize") return transformInitialize();
43
+ else if (props.event.type === "request")
44
+ return transformRequest({
45
+ event: props.event,
46
+ });
47
+ else if (props.event.type === "select")
48
+ return transformSelect({
49
+ operations: props.operations,
50
+ event: props.event,
51
+ });
52
+ else if (props.event.type === "text")
53
+ return transformText({
54
+ event: props.event,
55
+ });
56
+ else throw new Error("Unknown event type");
57
+ };
58
+
59
+ export const transformCall = <Model extends ILlmSchema.Model>(props: {
60
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
61
+ event: IAgenticaEventJson.ICall;
62
+ }): AgenticaCallEvent<Model> =>
63
+ new AgenticaCallEvent({
64
+ id: props.event.id,
65
+ operation: findOperation({
66
+ operations: props.operations,
67
+ input: props.event.operation,
68
+ }),
69
+ arguments: props.event.arguments,
70
+ });
71
+
72
+ export const transformCancel = <Model extends ILlmSchema.Model>(props: {
73
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
74
+ event: IAgenticaEventJson.ICancel;
75
+ }): AgenticaCancelEvent<Model> =>
76
+ new AgenticaCancelEvent({
77
+ selection: new AgenticaOperationSelection({
78
+ operation: findOperation({
79
+ operations: props.operations,
80
+ input: props.event.selection.operation,
81
+ }),
82
+ reason: props.event.selection.reason,
83
+ }),
84
+ });
85
+
86
+ export const transformDescribe = <Model extends ILlmSchema.Model>(props: {
87
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
88
+ event: IAgenticaEventJson.IDescribe;
89
+ }): AgenticaDescribeEvent<Model> =>
90
+ new AgenticaDescribeEvent({
91
+ executes: props.event.executes.map((next) =>
92
+ transformExecute({
93
+ operations: props.operations,
94
+ event: next,
95
+ }),
96
+ ),
97
+ stream: StreamUtil.to(props.event.text),
98
+ done: () => true,
99
+ get: () => props.event.text,
100
+ join: async () => props.event.text,
101
+ });
102
+
103
+ export const transformExecute = <Model extends ILlmSchema.Model>(props: {
104
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
105
+ event: IAgenticaEventJson.IExecute;
106
+ }): AgenticaExecuteEvent<Model> =>
107
+ new AgenticaExecuteEvent({
108
+ id: props.event.id,
109
+ operation: findOperation({
110
+ operations: props.operations,
111
+ input: props.event.operation,
112
+ }),
113
+ arguments: props.event.arguments,
114
+ value: props.event.value,
115
+ });
116
+
117
+ export const transformInitialize = (): AgenticaInitializeEvent =>
118
+ new AgenticaInitializeEvent();
119
+
120
+ export const transformRequest = (props: {
121
+ event: IAgenticaEventJson.IRequest;
122
+ }): AgenticaRequestEvent => new AgenticaRequestEvent(props.event);
123
+
124
+ export const transformSelect = <Model extends ILlmSchema.Model>(props: {
125
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
126
+ event: IAgenticaEventJson.ISelect;
127
+ }): AgenticaSelectEvent<Model> =>
128
+ new AgenticaSelectEvent({
129
+ selection: new AgenticaOperationSelection({
130
+ operation: findOperation({
131
+ operations: props.operations,
132
+ input: props.event.selection.operation,
133
+ }),
134
+ reason: props.event.selection.reason,
135
+ }),
136
+ });
137
+
138
+ export const transformText = (props: {
139
+ event: IAgenticaEventJson.IText;
140
+ }): AgenticaTextEvent =>
141
+ new AgenticaTextEvent({
142
+ role: props.event.role,
143
+ stream: StreamUtil.to(props.event.text),
144
+ done: () => true,
145
+ get: () => props.event.text,
146
+ join: async () => props.event.text,
147
+ });
148
+ }
149
+
150
+ const findOperation = <Model extends ILlmSchema.Model>(props: {
151
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
152
+ input: {
153
+ controller: string;
154
+ function: string;
155
+ };
156
+ }): AgenticaOperation<Model> => {
157
+ const found: AgenticaOperation<Model> | undefined = props.operations
158
+ .get(props.input.controller)
159
+ ?.get(props.input.function);
160
+ if (found === undefined)
161
+ throw new Error(
162
+ `No operation found: (controller: ${props.input.controller}, function: ${props.input.function})`,
163
+ );
164
+ return found;
165
+ };
@@ -1,134 +1,134 @@
1
- import { ILlmSchema } from "@samchon/openapi";
2
-
3
- import { AgenticaOperation } from "../context/AgenticaOperation";
4
- import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
5
- import { IAgenticaPromptJson } from "../json/IAgenticaPromptJson";
6
- import { AgenticaCancelPrompt } from "../prompts/AgenticaCancelPrompt";
7
- import { AgenticaDescribePrompt } from "../prompts/AgenticaDescribePrompt";
8
- import { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
9
- import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
10
- import { AgenticaSelectPrompt } from "../prompts/AgenticaSelectPrompt";
11
- import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
12
-
13
- export namespace AgenticaPromptTransformer {
14
- export const transform = <Model extends ILlmSchema.Model>(props: {
15
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
16
- prompt: IAgenticaPromptJson;
17
- }): AgenticaPrompt<Model> => {
18
- // TEXT
19
- if (props.prompt.type === "text")
20
- return transformText({
21
- prompt: props.prompt,
22
- });
23
- // SELECT & CANCEL
24
- else if (props.prompt.type === "select")
25
- return transformSelect({
26
- operations: props.operations,
27
- prompt: props.prompt,
28
- });
29
- else if (props.prompt.type === "cancel")
30
- return transformCancel({
31
- operations: props.operations,
32
- prompt: props.prompt,
33
- });
34
- // EXECUTE
35
- else if (props.prompt.type === "execute")
36
- return transformExecute({
37
- operations: props.operations,
38
- prompt: props.prompt,
39
- });
40
- else if (props.prompt.type === "describe")
41
- return transformDescribe({
42
- operations: props.operations,
43
- prompt: props.prompt,
44
- });
45
- throw new Error("Invalid prompt type.");
46
- };
47
-
48
- export const transformText = (props: {
49
- prompt: IAgenticaPromptJson.IText;
50
- }): AgenticaTextPrompt => {
51
- return new AgenticaTextPrompt(props.prompt);
52
- };
53
-
54
- export const transformSelect = <Model extends ILlmSchema.Model>(props: {
55
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
56
- prompt: IAgenticaPromptJson.ISelect;
57
- }): AgenticaSelectPrompt<Model> =>
58
- new AgenticaSelectPrompt({
59
- id: props.prompt.id,
60
- selections: props.prompt.selections.map(
61
- (select) =>
62
- new AgenticaOperationSelection({
63
- operation: findOperation({
64
- operations: props.operations,
65
- input: select.operation,
66
- }),
67
- reason: select.reason,
68
- }),
69
- ),
70
- });
71
-
72
- export const transformCancel = <Model extends ILlmSchema.Model>(props: {
73
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
74
- prompt: IAgenticaPromptJson.ICancel;
75
- }): AgenticaCancelPrompt<Model> =>
76
- new AgenticaCancelPrompt({
77
- id: props.prompt.id,
78
- selections: props.prompt.selections.map(
79
- (select) =>
80
- new AgenticaOperationSelection({
81
- operation: findOperation({
82
- operations: props.operations,
83
- input: select.operation,
84
- }),
85
- reason: select.reason,
86
- }),
87
- ),
88
- });
89
-
90
- export const transformExecute = <Model extends ILlmSchema.Model>(props: {
91
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
92
- prompt: IAgenticaPromptJson.IExecute;
93
- }): AgenticaExecutePrompt<Model> =>
94
- new AgenticaExecutePrompt({
95
- id: props.prompt.id,
96
- operation: findOperation({
97
- operations: props.operations,
98
- input: props.prompt.operation,
99
- }),
100
- arguments: props.prompt.arguments,
101
- value: props.prompt.value,
102
- });
103
-
104
- export const transformDescribe = <Model extends ILlmSchema.Model>(props: {
105
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
106
- prompt: IAgenticaPromptJson.IDescribe;
107
- }): AgenticaDescribePrompt<Model> =>
108
- new AgenticaDescribePrompt({
109
- text: props.prompt.text,
110
- executes: props.prompt.executes.map((next) =>
111
- transformExecute({
112
- operations: props.operations,
113
- prompt: next,
114
- }),
115
- ),
116
- });
117
- }
118
-
119
- const findOperation = <Model extends ILlmSchema.Model>(props: {
120
- operations: Map<string, Map<string, AgenticaOperation<Model>>>;
121
- input: {
122
- controller: string;
123
- function: string;
124
- };
125
- }): AgenticaOperation<Model> => {
126
- const found: AgenticaOperation<Model> | undefined = props.operations
127
- .get(props.input.controller)
128
- ?.get(props.input.function);
129
- if (found === undefined)
130
- throw new Error(
131
- `No operation found: (controller: ${props.input.controller}, function: ${props.input.function})`,
132
- );
133
- return found;
134
- };
1
+ import { ILlmSchema } from "@samchon/openapi";
2
+
3
+ import { AgenticaOperation } from "../context/AgenticaOperation";
4
+ import { AgenticaOperationSelection } from "../context/AgenticaOperationSelection";
5
+ import { IAgenticaPromptJson } from "../json/IAgenticaPromptJson";
6
+ import { AgenticaCancelPrompt } from "../prompts/AgenticaCancelPrompt";
7
+ import { AgenticaDescribePrompt } from "../prompts/AgenticaDescribePrompt";
8
+ import { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
9
+ import { AgenticaPrompt } from "../prompts/AgenticaPrompt";
10
+ import { AgenticaSelectPrompt } from "../prompts/AgenticaSelectPrompt";
11
+ import { AgenticaTextPrompt } from "../prompts/AgenticaTextPrompt";
12
+
13
+ export namespace AgenticaPromptTransformer {
14
+ export const transform = <Model extends ILlmSchema.Model>(props: {
15
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
16
+ prompt: IAgenticaPromptJson;
17
+ }): AgenticaPrompt<Model> => {
18
+ // TEXT
19
+ if (props.prompt.type === "text")
20
+ return transformText({
21
+ prompt: props.prompt,
22
+ });
23
+ // SELECT & CANCEL
24
+ else if (props.prompt.type === "select")
25
+ return transformSelect({
26
+ operations: props.operations,
27
+ prompt: props.prompt,
28
+ });
29
+ else if (props.prompt.type === "cancel")
30
+ return transformCancel({
31
+ operations: props.operations,
32
+ prompt: props.prompt,
33
+ });
34
+ // EXECUTE
35
+ else if (props.prompt.type === "execute")
36
+ return transformExecute({
37
+ operations: props.operations,
38
+ prompt: props.prompt,
39
+ });
40
+ else if (props.prompt.type === "describe")
41
+ return transformDescribe({
42
+ operations: props.operations,
43
+ prompt: props.prompt,
44
+ });
45
+ throw new Error("Invalid prompt type.");
46
+ };
47
+
48
+ export const transformText = (props: {
49
+ prompt: IAgenticaPromptJson.IText;
50
+ }): AgenticaTextPrompt => {
51
+ return new AgenticaTextPrompt(props.prompt);
52
+ };
53
+
54
+ export const transformSelect = <Model extends ILlmSchema.Model>(props: {
55
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
56
+ prompt: IAgenticaPromptJson.ISelect;
57
+ }): AgenticaSelectPrompt<Model> =>
58
+ new AgenticaSelectPrompt({
59
+ id: props.prompt.id,
60
+ selections: props.prompt.selections.map(
61
+ (select) =>
62
+ new AgenticaOperationSelection({
63
+ operation: findOperation({
64
+ operations: props.operations,
65
+ input: select.operation,
66
+ }),
67
+ reason: select.reason,
68
+ }),
69
+ ),
70
+ });
71
+
72
+ export const transformCancel = <Model extends ILlmSchema.Model>(props: {
73
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
74
+ prompt: IAgenticaPromptJson.ICancel;
75
+ }): AgenticaCancelPrompt<Model> =>
76
+ new AgenticaCancelPrompt({
77
+ id: props.prompt.id,
78
+ selections: props.prompt.selections.map(
79
+ (select) =>
80
+ new AgenticaOperationSelection({
81
+ operation: findOperation({
82
+ operations: props.operations,
83
+ input: select.operation,
84
+ }),
85
+ reason: select.reason,
86
+ }),
87
+ ),
88
+ });
89
+
90
+ export const transformExecute = <Model extends ILlmSchema.Model>(props: {
91
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
92
+ prompt: IAgenticaPromptJson.IExecute;
93
+ }): AgenticaExecutePrompt<Model> =>
94
+ new AgenticaExecutePrompt({
95
+ id: props.prompt.id,
96
+ operation: findOperation({
97
+ operations: props.operations,
98
+ input: props.prompt.operation,
99
+ }),
100
+ arguments: props.prompt.arguments,
101
+ value: props.prompt.value,
102
+ });
103
+
104
+ export const transformDescribe = <Model extends ILlmSchema.Model>(props: {
105
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
106
+ prompt: IAgenticaPromptJson.IDescribe;
107
+ }): AgenticaDescribePrompt<Model> =>
108
+ new AgenticaDescribePrompt({
109
+ text: props.prompt.text,
110
+ executes: props.prompt.executes.map((next) =>
111
+ transformExecute({
112
+ operations: props.operations,
113
+ prompt: next,
114
+ }),
115
+ ),
116
+ });
117
+ }
118
+
119
+ const findOperation = <Model extends ILlmSchema.Model>(props: {
120
+ operations: Map<string, Map<string, AgenticaOperation<Model>>>;
121
+ input: {
122
+ controller: string;
123
+ function: string;
124
+ };
125
+ }): AgenticaOperation<Model> => {
126
+ const found: AgenticaOperation<Model> | undefined = props.operations
127
+ .get(props.input.controller)
128
+ ?.get(props.input.function);
129
+ if (found === undefined)
130
+ throw new Error(
131
+ `No operation found: (controller: ${props.input.controller}, function: ${props.input.function})`,
132
+ );
133
+ return found;
134
+ };