@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
@@ -1,6 +1,6 @@
1
1
  import type { IAgenticaHistoryJson } from "../json/IAgenticaHistoryJson";
2
2
  import type { AgenticaHistoryBase } from "./AgenticaHistoryBase";
3
- export interface AgenticaTextHistory<Role extends "assistant" | "user" = "assistant" | "user"> extends AgenticaHistoryBase<"text", IAgenticaHistoryJson.IText> {
4
- role: Role;
3
+ export interface AgenticaTextHistory extends AgenticaHistoryBase<"text", IAgenticaHistoryJson.IText> {
4
+ role: "assistant";
5
5
  text: string;
6
6
  }
@@ -0,0 +1,80 @@
1
+ import type { IAgenticaHistoryJson } from "../json/IAgenticaHistoryJson";
2
+ import type { AgenticaHistoryBase } from "./AgenticaHistoryBase";
3
+ export interface AgenticaUserInputHistory extends AgenticaHistoryBase<"user_input", IAgenticaHistoryJson.IUserInput> {
4
+ role: "user";
5
+ contents: Array<AgenticaUserInputHistory.Contents>;
6
+ }
7
+ export declare namespace AgenticaUserInputHistory {
8
+ type Contents = Contents.File | Contents.Image | Contents.InputAudio | Contents.Text;
9
+ namespace Contents {
10
+ interface ContentsBase<Type extends string> {
11
+ /**
12
+ * The type of the content part.
13
+ */
14
+ type: Type;
15
+ }
16
+ /**
17
+ * Learn about
18
+ * [text inputs](https://platform.openai.com/docs/guides/text-generation).
19
+ */
20
+ export interface Text extends ContentsBase<"text"> {
21
+ /**
22
+ * The text content.
23
+ */
24
+ text: string;
25
+ }
26
+ /**
27
+ * Learn about [image inputs](https://platform.openai.com/docs/guides/vision).
28
+ */
29
+ export interface Image extends ContentsBase<"image_url"> {
30
+ image_url: {
31
+ /**
32
+ * Either a URL of the image or the base64 encoded image data.
33
+ */
34
+ url: string;
35
+ /**
36
+ * Specifies the detail level of the image. Learn more in the
37
+ * [Vision guide](https://platform.openai.com/docs/guides/vision#low-or-high-fidelity-image-understanding).
38
+ */
39
+ detail?: "auto" | "high" | "low";
40
+ };
41
+ }
42
+ /**
43
+ * Learn about [audio inputs](https://platform.openai.com/docs/guides/audio).
44
+ */
45
+ export interface InputAudio extends ContentsBase<"input_audio"> {
46
+ input_audio: {
47
+ /**
48
+ * Base64 encoded audio data.
49
+ */
50
+ data: string;
51
+ /**
52
+ * The format of the encoded audio data. Currently supports "wav" and "mp3".
53
+ */
54
+ format: "wav" | "mp3";
55
+ };
56
+ }
57
+ /**
58
+ * Learn about [file inputs](https://platform.openai.com/docs/guides/text) for text
59
+ * generation.
60
+ */
61
+ export interface File extends ContentsBase<"file"> {
62
+ file: {
63
+ /**
64
+ * The base64 encoded file data, used when passing the file to the model as a
65
+ * string.
66
+ */
67
+ file_data?: string;
68
+ /**
69
+ * The ID of an uploaded file to use as input.
70
+ */
71
+ file_id?: string;
72
+ /**
73
+ * The name of the file, used when passing the file to the model as a string.
74
+ */
75
+ filename?: string;
76
+ };
77
+ }
78
+ export {};
79
+ }
80
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=AgenticaUserInputHistory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AgenticaUserInputHistory.js","sourceRoot":"","sources":["../../src/histories/AgenticaUserInputHistory.ts"],"names":[],"mappings":""}
@@ -2,7 +2,8 @@ import type { ILlmSchema } from "@samchon/openapi";
2
2
  import type { AgenticaDescribeHistory } from "./AgenticaDescribeHistory";
3
3
  import type { AgenticaExecuteHistory } from "./AgenticaExecuteHistory";
4
4
  import type { AgenticaTextHistory } from "./AgenticaTextHistory";
5
- export type MicroAgenticaHistory<Model extends ILlmSchema.Model> = AgenticaDescribeHistory<Model> | AgenticaExecuteHistory<Model> | AgenticaTextHistory;
5
+ import type { AgenticaUserInputHistory } from "./AgenticaUserInputHistory";
6
+ export type MicroAgenticaHistory<Model extends ILlmSchema.Model> = AgenticaDescribeHistory<Model> | AgenticaExecuteHistory<Model> | AgenticaTextHistory | AgenticaUserInputHistory;
6
7
  export declare namespace MicroAgenticaHistory {
7
8
  type Type = MicroAgenticaHistory<any>["type"];
8
9
  interface Mapper<Model extends ILlmSchema.Model> {
package/lib/index.mjs CHANGED
@@ -273,12 +273,14 @@ const AgenticaTokenUsageAggregator = {
273
273
  function decodeHistory(history) {
274
274
  if (history.type === "describe") {
275
275
  return [];
276
- } else if (history.type === "text") {
276
+ }
277
+ if (history.type === "text") {
277
278
  return [ {
278
279
  role: history.role,
279
280
  content: history.text
280
281
  } ];
281
- } else if (history.type === "select" || history.type === "cancel") {
282
+ }
283
+ if (history.type === "select" || history.type === "cancel") {
282
284
  return [ {
283
285
  role: "assistant",
284
286
  tool_calls: [ {
@@ -300,44 +302,65 @@ function decodeHistory(history) {
300
302
  content: ""
301
303
  } ];
302
304
  }
303
- return [ {
304
- role: "assistant",
305
- tool_calls: [ {
306
- type: "function",
307
- id: history.id,
308
- function: {
309
- name: history.operation.name,
310
- arguments: JSON.stringify(history.arguments)
311
- }
312
- } ]
313
- }, {
314
- role: "tool",
315
- tool_call_id: history.id,
316
- content: JSON.stringify({
317
- function: {
318
- protocol: history.operation.protocol,
319
- description: history.operation.function.description,
320
- parameters: history.operation.function.parameters,
321
- output: history.operation.function.output,
305
+ if (history.type === "execute") {
306
+ return [ {
307
+ role: "assistant",
308
+ tool_calls: [ {
309
+ type: "function",
310
+ id: history.id,
311
+ function: {
312
+ name: history.operation.name,
313
+ arguments: JSON.stringify(history.arguments)
314
+ }
315
+ } ]
316
+ }, {
317
+ role: "tool",
318
+ tool_call_id: history.id,
319
+ content: JSON.stringify({
320
+ function: {
321
+ protocol: history.operation.protocol,
322
+ description: history.operation.function.description,
323
+ parameters: history.operation.function.parameters,
324
+ output: history.operation.function.output,
325
+ ...history.operation.protocol === "http" ? {
326
+ method: history.operation.function.method,
327
+ path: history.operation.function.path
328
+ } : {}
329
+ },
322
330
  ...history.operation.protocol === "http" ? {
323
- method: history.operation.function.method,
324
- path: history.operation.function.path
325
- } : {}
326
- },
327
- ...history.operation.protocol === "http" ? {
328
- status: history.value.status,
329
- data: history.value.body
330
- } : {
331
- value: history.value
332
- }
331
+ status: history.value.status,
332
+ data: history.value.body
333
+ } : {
334
+ value: history.value
335
+ }
336
+ })
337
+ } ];
338
+ }
339
+ if (history.type === "user_input") {
340
+ return [ {
341
+ role: "user",
342
+ content: history.contents
343
+ } ];
344
+ }
345
+ throw new Error("Invalid history type");
346
+ }
347
+
348
+ function createUserInputHistory(props) {
349
+ return {
350
+ type: "user_input",
351
+ role: "user",
352
+ contents: props.contents,
353
+ toJSON: () => ({
354
+ type: "user_input",
355
+ contents: props.contents
333
356
  })
334
- } ];
357
+ };
335
358
  }
336
359
 
337
360
  function createTextHistory(props) {
338
361
  const prompt = {
339
362
  type: "text",
340
- role: props.role,
363
+ role: "assistant",
341
364
  text: props.text
342
365
  };
343
366
  return {
@@ -414,6 +437,22 @@ function createInitializeEvent() {
414
437
  };
415
438
  }
416
439
 
440
+ function createUserInputEvent(props) {
441
+ return {
442
+ type: "user_input",
443
+ role: "user",
444
+ contents: props.contents,
445
+ join: async () => props.contents,
446
+ toJSON: () => ({
447
+ type: "user_input",
448
+ contents: props.contents
449
+ }),
450
+ toHistory: () => createUserInputHistory({
451
+ contents: props.contents
452
+ })
453
+ };
454
+ }
455
+
417
456
  function createSelectEvent(props) {
418
457
  return {
419
458
  type: "select",
@@ -493,22 +532,22 @@ function createExecuteEvent(props) {
493
532
  function createTextEvent(props) {
494
533
  return {
495
534
  type: "text",
496
- role: props.role,
535
+ role: "assistant",
497
536
  stream: props.stream,
498
537
  join: props.join,
499
538
  toJSON: () => ({
500
539
  type: "text",
501
- role: props.role,
540
+ role: "assistant",
502
541
  done: props.done(),
503
542
  text: props.get()
504
543
  }),
505
544
  toHistory: () => ({
506
545
  type: "text",
507
- role: props.role,
546
+ role: "assistant",
508
547
  text: props.get(),
509
548
  toJSON: () => ({
510
549
  type: "text",
511
- role: props.role,
550
+ role: "assistant",
512
551
  text: props.get()
513
552
  })
514
553
  })
@@ -560,6 +599,40 @@ function createResponseEvent(props) {
560
599
  };
561
600
  }
562
601
 
602
+ function createOperationSelection(props) {
603
+ return {
604
+ operation: props.operation,
605
+ reason: props.reason,
606
+ toJSON: () => ({
607
+ operation: props.operation.toJSON(),
608
+ reason: props.reason
609
+ })
610
+ };
611
+ }
612
+
613
+ var index$2 = Object.freeze({
614
+ __proto__: null,
615
+ createCallEvent,
616
+ createCancelEvent,
617
+ createCancelHistory,
618
+ createDescribeEvent,
619
+ createDescribeHistory,
620
+ createExecuteEvent,
621
+ createExecuteHistory,
622
+ createInitializeEvent,
623
+ createOperationSelection,
624
+ createRequestEvent,
625
+ createResponseEvent,
626
+ createSelectEvent,
627
+ createSelectHistory,
628
+ createTextEvent,
629
+ createTextHistory,
630
+ createUserInputEvent,
631
+ createUserInputHistory,
632
+ createValidateEvent,
633
+ decodeHistory
634
+ });
635
+
563
636
  var AgenticaConstant;
564
637
 
565
638
  (function(AgenticaConstant) {
@@ -617,17 +690,6 @@ function isAgenticaContext(ctx) {
617
690
  return typeof ctx.initialize === "function";
618
691
  }
619
692
 
620
- function createOperationSelection(props) {
621
- return {
622
- operation: props.operation,
623
- reason: props.reason,
624
- toJSON: () => ({
625
- operation: props.operation.toJSON(),
626
- reason: props.reason
627
- })
628
- };
629
- }
630
-
631
693
  var ByteArrayUtil;
632
694
 
633
695
  (function(ByteArrayUtil) {
@@ -1251,7 +1313,7 @@ async function call(ctx, operations) {
1251
1313
  content: AgenticaDefaultPrompt.write(ctx.config)
1252
1314
  }, ...ctx.histories.map(decodeHistory).flat(), {
1253
1315
  role: "user",
1254
- content: ctx.prompt.text
1316
+ content: ctx.prompt.contents
1255
1317
  }, ...ctx.config?.systemPrompt?.execute === null ? [] : [ {
1256
1318
  role: "system",
1257
1319
  content: ctx.config?.systemPrompt?.execute?.(ctx.histories) ?? AgenticaSystemPrompt.EXECUTE
@@ -1323,11 +1385,9 @@ async function call(ctx, operations) {
1323
1385
  if (choice.message.role === "assistant" && choice.message.content != null && choice.message.content.length !== 0) {
1324
1386
  closures.push((async () => {
1325
1387
  const value = createTextHistory({
1326
- role: "assistant",
1327
1388
  text: choice.message.content
1328
1389
  });
1329
1390
  ctx.dispatch(createTextEvent({
1330
- role: "assistant",
1331
1391
  get: () => value.text,
1332
1392
  done: () => true,
1333
1393
  stream: toAsyncGenerator(value.text),
@@ -1528,7 +1588,7 @@ async function correct(ctx, call, retry, error) {
1528
1588
  content: AgenticaDefaultPrompt.write(ctx.config)
1529
1589
  }, ...ctx.histories.map(decodeHistory).flat(), {
1530
1590
  role: "user",
1531
- content: ctx.prompt.text
1591
+ content: ctx.prompt.contents
1532
1592
  }, ...ctx.config?.systemPrompt?.execute === null ? [] : [ {
1533
1593
  role: "system",
1534
1594
  content: ctx.config?.systemPrompt?.execute?.(ctx.histories) ?? AgenticaSystemPrompt.EXECUTE
@@ -1759,7 +1819,7 @@ async function step$1(ctx, operations, retry, failures) {
1759
1819
  }))))
1760
1820
  }, ...ctx.histories.map(decodeHistory).flat(), {
1761
1821
  role: "user",
1762
- content: ctx.prompt.text
1822
+ content: ctx.prompt.contents
1763
1823
  }, {
1764
1824
  role: "system",
1765
1825
  content: ctx.config?.systemPrompt?.cancel?.(ctx.histories) ?? AgenticaSystemPrompt.CANCEL
@@ -3756,7 +3816,7 @@ async function initialize(ctx) {
3756
3816
  content: AgenticaDefaultPrompt.write(ctx.config)
3757
3817
  }, ...ctx.histories.map(decodeHistory).flat(), {
3758
3818
  role: "user",
3759
- content: ctx.prompt.text
3819
+ content: ctx.prompt.contents
3760
3820
  }, {
3761
3821
  role: "system",
3762
3822
  content: ctx.config?.systemPrompt?.initialize?.(ctx.histories) ?? AgenticaSystemPrompt.INITIALIZE
@@ -3796,7 +3856,6 @@ async function initialize(ctx) {
3796
3856
  };
3797
3857
  mpsc.produce(choice.delta.content);
3798
3858
  ctx.dispatch(createTextEvent({
3799
- role: "assistant",
3800
3859
  stream: streamDefaultReaderToAsyncGenerator(mpsc.consumer.getReader()),
3801
3860
  done: () => mpsc.done(),
3802
3861
  get: () => textContext[choice.index].content,
@@ -3821,7 +3880,6 @@ async function initialize(ctx) {
3821
3880
  for (const choice of completion.choices) {
3822
3881
  if (choice.message.role === "assistant" && choice.message.content != null && choice.message.content.length !== 0) {
3823
3882
  prompts.push(createTextHistory({
3824
- role: "assistant",
3825
3883
  text: choice.message.content
3826
3884
  }));
3827
3885
  }
@@ -4010,7 +4068,7 @@ async function step(ctx, operations, retry, failures) {
4010
4068
  }))))
4011
4069
  }, ...ctx.histories.map(decodeHistory).flat(), {
4012
4070
  role: "user",
4013
- content: ctx.prompt.text
4071
+ content: ctx.prompt.contents
4014
4072
  }, {
4015
4073
  role: "system",
4016
4074
  content: ctx.config?.systemPrompt?.select?.(ctx.histories) ?? AgenticaSystemPrompt.SELECT
@@ -4153,12 +4211,10 @@ async function step(ctx, operations, retry, failures) {
4153
4211
  }
4154
4212
  if (choice.message.role === "assistant" && choice.message.content != null && choice.message.content.length !== 0) {
4155
4213
  const text = createTextHistory({
4156
- role: "assistant",
4157
4214
  text: choice.message.content
4158
4215
  });
4159
4216
  prompts.push(text);
4160
4217
  ctx.dispatch(createTextEvent({
4161
- role: "assistant",
4162
4218
  stream: toAsyncGenerator(text.text),
4163
4219
  join: async () => Promise.resolve(text.text),
4164
4220
  done: () => true,
@@ -4347,23 +4403,21 @@ class Agentica {
4347
4403
  });
4348
4404
  }
4349
4405
  async conversate(content) {
4350
- const text = createTextHistory({
4351
- role: "user",
4352
- text: content
4406
+ const prompt = createUserInputHistory({
4407
+ contents: Array.isArray(content) ? content : typeof content === "string" ? [ {
4408
+ type: "text",
4409
+ text: content
4410
+ } ] : [ content ]
4353
4411
  });
4354
- this.dispatch(createTextEvent({
4355
- role: "user",
4356
- stream: toAsyncGenerator(content),
4357
- done: () => true,
4358
- get: () => content,
4359
- join: async () => Promise.resolve(content)
4412
+ this.dispatch(createUserInputEvent({
4413
+ contents: prompt.contents
4360
4414
  })).catch((() => {}));
4361
4415
  const newbie = await this.executor_(this.getContext({
4362
- prompt: text,
4416
+ prompt,
4363
4417
  usage: this.token_usage_
4364
4418
  }));
4365
- this.histories_.push(text, ...newbie);
4366
- return [ text, ...newbie ];
4419
+ this.histories_.push(prompt, ...newbie);
4420
+ return [ prompt, ...newbie ];
4367
4421
  }
4368
4422
  getConfig() {
4369
4423
  return this.props.config;
@@ -4472,27 +4526,6 @@ class Agentica {
4472
4526
  }
4473
4527
  }
4474
4528
 
4475
- var index$2 = Object.freeze({
4476
- __proto__: null,
4477
- createCallEvent,
4478
- createCancelEvent,
4479
- createCancelHistory,
4480
- createDescribeEvent,
4481
- createDescribeHistory,
4482
- createExecuteEvent,
4483
- createExecuteHistory,
4484
- createInitializeEvent,
4485
- createOperationSelection,
4486
- createRequestEvent,
4487
- createResponseEvent,
4488
- createSelectEvent,
4489
- createSelectHistory,
4490
- createTextEvent,
4491
- createTextHistory,
4492
- createValidateEvent,
4493
- decodeHistory
4494
- });
4495
-
4496
4529
  function assertHttpLlmApplication(props) {
4497
4530
  return HttpLlm.application({
4498
4531
  model: props.model,
@@ -23189,16 +23222,14 @@ class MicroAgentica {
23189
23222
  });
23190
23223
  }
23191
23224
  async conversate(content) {
23192
- const talk = createTextHistory({
23193
- role: "user",
23194
- text: content
23225
+ const talk = createUserInputHistory({
23226
+ contents: Array.isArray(content) ? content : typeof content === "string" ? [ {
23227
+ type: "text",
23228
+ text: content
23229
+ } ] : [ content ]
23195
23230
  });
23196
- this.dispatch(createTextEvent({
23197
- role: "user",
23198
- stream: toAsyncGenerator(content),
23199
- done: () => true,
23200
- get: () => content,
23201
- join: async () => Promise.resolve(content)
23231
+ this.dispatch(createUserInputEvent({
23232
+ contents: talk.contents
23202
23233
  })).catch((() => {}));
23203
23234
  const ctx = this.getContext({
23204
23235
  prompt: talk,