@agentica/core 0.7.0 → 0.8.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.
@@ -0,0 +1,123 @@
1
+ import OpenAI from "openai";
2
+
3
+ import { IAgenticaTokenUsage } from "../structures/IAgenticaTokenUsage";
4
+
5
+ export namespace AgenticaTokenUsageAggregator {
6
+ export const aggregate = (props: {
7
+ kind: Exclude<keyof IAgenticaTokenUsage, "aggregate">;
8
+ completion: OpenAI.ChatCompletion;
9
+ usage: IAgenticaTokenUsage;
10
+ }): void => {
11
+ if (!props.completion.usage) return;
12
+
13
+ //----
14
+ // COMPONENT
15
+ //----
16
+ const component: IAgenticaTokenUsage.IComponent<any> =
17
+ props.usage[props.kind];
18
+
19
+ // TOTAL
20
+ component.total += props.completion.usage.total_tokens;
21
+
22
+ // PROMPT
23
+ component.input.total += props.completion.usage.prompt_tokens;
24
+ props.completion.usage.prompt_tokens_details?.audio_tokens ?? 0;
25
+ component.input.cached +=
26
+ props.completion.usage.prompt_tokens_details?.cached_tokens ?? 0;
27
+
28
+ // COMPLETION
29
+ component.output.total += props.completion.usage.total_tokens;
30
+ component.output.accepted_prediction +=
31
+ props.completion.usage.completion_tokens_details
32
+ ?.accepted_prediction_tokens ?? 0;
33
+ component.output.reasoning +=
34
+ props.completion.usage.completion_tokens_details?.reasoning_tokens ?? 0;
35
+ component.output.rejected_prediction +=
36
+ props.completion.usage.completion_tokens_details
37
+ ?.rejected_prediction_tokens ?? 0;
38
+
39
+ //----
40
+ // RE-AGGREGATE
41
+ //----
42
+ const sum = (
43
+ getter: (comp: IAgenticaTokenUsage.IComponent<any>) => number,
44
+ ) =>
45
+ Object.entries(props.usage)
46
+ .filter(([key]) => key !== "aggregate")
47
+ .map(([_, comp]) => getter(comp))
48
+ .reduce((a, b) => a + b, 0);
49
+ const aggregate: IAgenticaTokenUsage.IComponent<"aggregate"> =
50
+ props.usage.aggregate;
51
+ aggregate.total = sum((comp) => comp.total);
52
+ aggregate.input.total = sum((comp) => comp.input.total);
53
+ aggregate.input.cached = sum((comp) => comp.input.cached);
54
+ aggregate.output.total = sum((comp) => comp.output.total);
55
+ aggregate.output.reasoning = sum((comp) => comp.output.reasoning);
56
+ aggregate.output.accepted_prediction = sum(
57
+ (comp) => comp.output.accepted_prediction,
58
+ );
59
+ aggregate.output.rejected_prediction = sum(
60
+ (comp) => comp.output.rejected_prediction,
61
+ );
62
+ };
63
+
64
+ export const plus = (
65
+ x: IAgenticaTokenUsage,
66
+ y: IAgenticaTokenUsage,
67
+ ): IAgenticaTokenUsage => {
68
+ const component = <Kind extends string>(
69
+ a: IAgenticaTokenUsage.IComponent<Kind>,
70
+ b: IAgenticaTokenUsage.IComponent<Kind>,
71
+ ): IAgenticaTokenUsage.IComponent<Kind> => ({
72
+ kind: a.kind,
73
+ total: a.total + b.total,
74
+ input: {
75
+ total: a.input.total + b.input.total,
76
+ cached: a.input.cached + b.input.cached,
77
+ },
78
+ output: {
79
+ total: a.output.total + b.output.total,
80
+ reasoning: a.output.reasoning + b.output.reasoning,
81
+ accepted_prediction:
82
+ a.output.accepted_prediction + b.output.accepted_prediction,
83
+ rejected_prediction:
84
+ a.output.rejected_prediction + b.output.rejected_prediction,
85
+ },
86
+ });
87
+ return {
88
+ aggregate: component(x.aggregate, y.aggregate),
89
+ initialize: component(x.initialize, y.initialize),
90
+ select: component(x.select, y.select),
91
+ cancel: component(x.cancel, y.cancel),
92
+ call: component(x.call, y.call),
93
+ describe: component(x.describe, y.describe),
94
+ };
95
+ };
96
+
97
+ export const zero = (): IAgenticaTokenUsage => {
98
+ const component = <Kind extends string>(
99
+ kind: Kind,
100
+ ): IAgenticaTokenUsage.IComponent<Kind> => ({
101
+ kind,
102
+ total: 0,
103
+ input: {
104
+ total: 0,
105
+ cached: 0,
106
+ },
107
+ output: {
108
+ total: 0,
109
+ reasoning: 0,
110
+ accepted_prediction: 0,
111
+ rejected_prediction: 0,
112
+ },
113
+ });
114
+ return {
115
+ aggregate: component("aggregate"),
116
+ initialize: component("initialize"),
117
+ select: component("select"),
118
+ cancel: component("cancel"),
119
+ call: component("call"),
120
+ describe: component("describe"),
121
+ };
122
+ };
123
+ }
@@ -18,35 +18,95 @@
18
18
  */
19
19
  export interface IAgenticaTokenUsage {
20
20
  /**
21
- * Total token usage.
21
+ * Aggregated token usage.
22
22
  */
23
- total: number;
23
+ aggregate: IAgenticaTokenUsage.IComponent<"aggregate">;
24
24
 
25
25
  /**
26
- * Token usage in the prompt.
27
- *
28
- * In other words, it is called as the input token.
26
+ * Token uasge of initializer agent.
29
27
  */
30
- prompt: IAgenticaTokenUsage.IPrompt;
28
+ initialize: IAgenticaTokenUsage.IComponent<"initialize">;
31
29
 
32
30
  /**
33
- * Token usage in the completion.
34
- *
35
- * In other words, it is called as the output token.
31
+ * Token usage of function selector agent.
36
32
  */
37
- completion: IAgenticaTokenUsage.ICompletion;
33
+ select: IAgenticaTokenUsage.IComponent<"select">;
34
+
35
+ /**
36
+ * Token usage of function canceler agent.
37
+ */
38
+ cancel: IAgenticaTokenUsage.IComponent<"cancel">;
39
+
40
+ /**
41
+ * Token usage of function caller agent.
42
+ */
43
+ call: IAgenticaTokenUsage.IComponent<"call">;
44
+
45
+ /**
46
+ * Token usage of function calling describer agent.
47
+ */
48
+ describe: IAgenticaTokenUsage.IComponent<"describe">;
38
49
  }
39
50
  export namespace IAgenticaTokenUsage {
40
- export interface IPrompt {
51
+ export interface IComponent<Kind extends string> {
52
+ /**
53
+ * Kind of the token usage.
54
+ */
55
+ kind: Kind;
56
+
57
+ /**
58
+ * Total token usage.
59
+ */
60
+ total: number;
61
+
62
+ /**
63
+ * Input token usage of detailed.
64
+ */
65
+ input: IInput;
66
+
67
+ /**
68
+ * Output token usage of detailed.
69
+ */
70
+ output: IOutput;
71
+ }
72
+
73
+ /**
74
+ * Input token usage of detailed.
75
+ */
76
+ export interface IInput {
77
+ /**
78
+ * Total amount of input token uasge.
79
+ */
41
80
  total: number;
42
- audio: number;
81
+
82
+ /**
83
+ * Cached token usage.
84
+ */
43
85
  cached: number;
44
86
  }
45
- export interface ICompletion {
87
+
88
+ /**
89
+ * Output token usage of detailed.
90
+ */
91
+ export interface IOutput {
92
+ /**
93
+ * Total amount of output token usage.
94
+ */
46
95
  total: number;
47
- accepted_prediction: number;
48
- audio: number;
96
+
97
+ /**
98
+ * Reasoning token usage.
99
+ */
49
100
  reasoning: number;
101
+
102
+ /**
103
+ * Prediction token usage.
104
+ */
105
+ accepted_prediction: number;
106
+
107
+ /**
108
+ * Rejected prediction token usage.
109
+ */
50
110
  rejected_prediction: number;
51
111
  }
52
112
  }
@@ -2,5 +2,5 @@ export type AgenticaSource =
2
2
  | "initialize"
3
3
  | "select"
4
4
  | "cancel"
5
- | "execute"
5
+ | "call"
6
6
  | "describe";
@@ -1,5 +0,0 @@
1
- import OpenAI from "openai";
2
- import { IAgenticaTokenUsage } from "../structures/IAgenticaTokenUsage";
3
- export declare namespace AgenticaCostAggregator {
4
- const aggregate: (cost: IAgenticaTokenUsage, completion: OpenAI.ChatCompletion) => void;
5
- }
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AgenticaCostAggregator = void 0;
4
- var AgenticaCostAggregator;
5
- (function (AgenticaCostAggregator) {
6
- AgenticaCostAggregator.aggregate = (cost, completion) => {
7
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
8
- if (!completion.usage)
9
- return;
10
- // TOTAL
11
- cost.total += completion.usage.total_tokens;
12
- // PROMPT
13
- cost.prompt.total += completion.usage.prompt_tokens;
14
- cost.prompt.audio +=
15
- (_b = (_a = completion.usage.prompt_tokens_details) === null || _a === void 0 ? void 0 : _a.audio_tokens) !== null && _b !== void 0 ? _b : 0;
16
- cost.prompt.cached +=
17
- (_d = (_c = completion.usage.prompt_tokens_details) === null || _c === void 0 ? void 0 : _c.cached_tokens) !== null && _d !== void 0 ? _d : 0;
18
- // COMPLETION
19
- cost.completion.total += completion.usage.total_tokens;
20
- cost.completion.accepted_prediction +=
21
- (_f = (_e = completion.usage.completion_tokens_details) === null || _e === void 0 ? void 0 : _e.accepted_prediction_tokens) !== null && _f !== void 0 ? _f : 0;
22
- cost.completion.audio +=
23
- (_h = (_g = completion.usage.completion_tokens_details) === null || _g === void 0 ? void 0 : _g.audio_tokens) !== null && _h !== void 0 ? _h : 0;
24
- cost.completion.reasoning +=
25
- (_k = (_j = completion.usage.completion_tokens_details) === null || _j === void 0 ? void 0 : _j.reasoning_tokens) !== null && _k !== void 0 ? _k : 0;
26
- cost.completion.rejected_prediction +=
27
- (_m = (_l = completion.usage.completion_tokens_details) === null || _l === void 0 ? void 0 : _l.rejected_prediction_tokens) !== null && _m !== void 0 ? _m : 0;
28
- };
29
- })(AgenticaCostAggregator || (exports.AgenticaCostAggregator = AgenticaCostAggregator = {}));
30
- //# sourceMappingURL=AgenticaCostAggregator.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AgenticaCostAggregator.js","sourceRoot":"","sources":["../../src/internal/AgenticaCostAggregator.ts"],"names":[],"mappings":";;;AAIA,IAAiB,sBAAsB,CA8BtC;AA9BD,WAAiB,sBAAsB;IACxB,gCAAS,GAAG,CACvB,IAAyB,EACzB,UAAiC,EAC3B,EAAE;;QACR,IAAI,CAAC,UAAU,CAAC,KAAK;YAAE,OAAO;QAE9B,QAAQ;QACR,IAAI,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC;QAE5C,SAAS;QACT,IAAI,CAAC,MAAM,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC,aAAa,CAAC;QACpD,IAAI,CAAC,MAAM,CAAC,KAAK;YACf,MAAA,MAAA,UAAU,CAAC,KAAK,CAAC,qBAAqB,0CAAE,YAAY,mCAAI,CAAC,CAAC;QAC5D,IAAI,CAAC,MAAM,CAAC,MAAM;YAChB,MAAA,MAAA,UAAU,CAAC,KAAK,CAAC,qBAAqB,0CAAE,aAAa,mCAAI,CAAC,CAAC;QAE7D,aAAa;QACb,IAAI,CAAC,UAAU,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC;QACvD,IAAI,CAAC,UAAU,CAAC,mBAAmB;YACjC,MAAA,MAAA,UAAU,CAAC,KAAK,CAAC,yBAAyB,0CAAE,0BAA0B,mCACtE,CAAC,CAAC;QACJ,IAAI,CAAC,UAAU,CAAC,KAAK;YACnB,MAAA,MAAA,UAAU,CAAC,KAAK,CAAC,yBAAyB,0CAAE,YAAY,mCAAI,CAAC,CAAC;QAChE,IAAI,CAAC,UAAU,CAAC,SAAS;YACvB,MAAA,MAAA,UAAU,CAAC,KAAK,CAAC,yBAAyB,0CAAE,gBAAgB,mCAAI,CAAC,CAAC;QACpE,IAAI,CAAC,UAAU,CAAC,mBAAmB;YACjC,MAAA,MAAA,UAAU,CAAC,KAAK,CAAC,yBAAyB,0CAAE,0BAA0B,mCACtE,CAAC,CAAC;IACN,CAAC,CAAC;AACJ,CAAC,EA9BgB,sBAAsB,sCAAtB,sBAAsB,QA8BtC"}
@@ -1,35 +0,0 @@
1
- import OpenAI from "openai";
2
-
3
- import { IAgenticaTokenUsage } from "../structures/IAgenticaTokenUsage";
4
-
5
- export namespace AgenticaCostAggregator {
6
- export const aggregate = (
7
- cost: IAgenticaTokenUsage,
8
- completion: OpenAI.ChatCompletion,
9
- ): void => {
10
- if (!completion.usage) return;
11
-
12
- // TOTAL
13
- cost.total += completion.usage.total_tokens;
14
-
15
- // PROMPT
16
- cost.prompt.total += completion.usage.prompt_tokens;
17
- cost.prompt.audio +=
18
- completion.usage.prompt_tokens_details?.audio_tokens ?? 0;
19
- cost.prompt.cached +=
20
- completion.usage.prompt_tokens_details?.cached_tokens ?? 0;
21
-
22
- // COMPLETION
23
- cost.completion.total += completion.usage.total_tokens;
24
- cost.completion.accepted_prediction +=
25
- completion.usage.completion_tokens_details?.accepted_prediction_tokens ??
26
- 0;
27
- cost.completion.audio +=
28
- completion.usage.completion_tokens_details?.audio_tokens ?? 0;
29
- cost.completion.reasoning +=
30
- completion.usage.completion_tokens_details?.reasoning_tokens ?? 0;
31
- cost.completion.rejected_prediction +=
32
- completion.usage.completion_tokens_details?.rejected_prediction_tokens ??
33
- 0;
34
- };
35
- }