@agentica/core 0.12.1 → 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 (77) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +461 -461
  3. package/package.json +1 -1
  4. package/prompts/cancel.md +4 -4
  5. package/prompts/common.md +2 -2
  6. package/prompts/describe.md +6 -6
  7. package/prompts/execute.md +6 -6
  8. package/prompts/initialize.md +2 -2
  9. package/prompts/select.md +6 -6
  10. package/src/Agentica.ts +359 -359
  11. package/src/chatgpt/ChatGptAgent.ts +76 -76
  12. package/src/chatgpt/ChatGptCallFunctionAgent.ts +466 -466
  13. package/src/chatgpt/ChatGptCancelFunctionAgent.ts +280 -280
  14. package/src/chatgpt/ChatGptCompletionMessageUtil.ts +166 -166
  15. package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +122 -122
  16. package/src/chatgpt/ChatGptHistoryDecoder.ts +88 -88
  17. package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +96 -96
  18. package/src/chatgpt/ChatGptSelectFunctionAgent.ts +311 -311
  19. package/src/chatgpt/ChatGptUsageAggregator.ts +62 -62
  20. package/src/context/AgenticaCancelPrompt.ts +32 -32
  21. package/src/context/AgenticaClassOperation.ts +23 -23
  22. package/src/context/AgenticaContext.ts +130 -130
  23. package/src/context/AgenticaHttpOperation.ts +27 -27
  24. package/src/context/AgenticaOperation.ts +66 -66
  25. package/src/context/AgenticaOperationBase.ts +57 -57
  26. package/src/context/AgenticaOperationCollection.ts +52 -52
  27. package/src/context/AgenticaOperationSelection.ts +27 -27
  28. package/src/context/AgenticaTokenUsage.ts +170 -170
  29. package/src/context/internal/AgenticaTokenUsageAggregator.ts +66 -66
  30. package/src/context/internal/__IChatCancelFunctionsApplication.ts +23 -23
  31. package/src/context/internal/__IChatFunctionReference.ts +21 -21
  32. package/src/context/internal/__IChatInitialApplication.ts +15 -15
  33. package/src/context/internal/__IChatSelectFunctionsApplication.ts +24 -24
  34. package/src/events/AgenticaCallEvent.ts +36 -36
  35. package/src/events/AgenticaCancelEvent.ts +28 -28
  36. package/src/events/AgenticaDescribeEvent.ts +66 -66
  37. package/src/events/AgenticaEvent.ts +36 -36
  38. package/src/events/AgenticaEventBase.ts +7 -7
  39. package/src/events/AgenticaEventSource.ts +6 -6
  40. package/src/events/AgenticaExecuteEvent.ts +50 -50
  41. package/src/events/AgenticaInitializeEvent.ts +14 -14
  42. package/src/events/AgenticaRequestEvent.ts +45 -45
  43. package/src/events/AgenticaResponseEvent.ts +48 -48
  44. package/src/events/AgenticaSelectEvent.ts +37 -37
  45. package/src/events/AgenticaTextEvent.ts +62 -62
  46. package/src/functional/assertHttpLlmApplication.ts +55 -55
  47. package/src/functional/validateHttpLlmApplication.ts +66 -66
  48. package/src/index.ts +44 -44
  49. package/src/internal/AgenticaConstant.ts +4 -4
  50. package/src/internal/AgenticaDefaultPrompt.ts +43 -43
  51. package/src/internal/AgenticaOperationComposer.ts +96 -96
  52. package/src/internal/ByteArrayUtil.ts +5 -5
  53. package/src/internal/MPSCUtil.ts +111 -111
  54. package/src/internal/MathUtil.ts +3 -3
  55. package/src/internal/Singleton.ts +22 -22
  56. package/src/internal/StreamUtil.ts +64 -64
  57. package/src/internal/__map_take.ts +15 -15
  58. package/src/json/IAgenticaEventJson.ts +178 -178
  59. package/src/json/IAgenticaOperationJson.ts +36 -36
  60. package/src/json/IAgenticaOperationSelectionJson.ts +19 -19
  61. package/src/json/IAgenticaPromptJson.ts +130 -130
  62. package/src/json/IAgenticaTokenUsageJson.ts +107 -107
  63. package/src/prompts/AgenticaCancelPrompt.ts +32 -32
  64. package/src/prompts/AgenticaDescribePrompt.ts +41 -41
  65. package/src/prompts/AgenticaExecutePrompt.ts +52 -52
  66. package/src/prompts/AgenticaPrompt.ts +14 -14
  67. package/src/prompts/AgenticaPromptBase.ts +27 -27
  68. package/src/prompts/AgenticaSelectPrompt.ts +32 -32
  69. package/src/prompts/AgenticaTextPrompt.ts +31 -31
  70. package/src/structures/IAgenticaConfig.ts +123 -123
  71. package/src/structures/IAgenticaController.ts +133 -133
  72. package/src/structures/IAgenticaExecutor.ts +157 -157
  73. package/src/structures/IAgenticaProps.ts +69 -69
  74. package/src/structures/IAgenticaSystemPrompt.ts +125 -125
  75. package/src/structures/IAgenticaVendor.ts +39 -39
  76. package/src/transformers/AgenticaEventTransformer.ts +165 -165
  77. package/src/transformers/AgenticaPromptTransformer.ts +134 -134
@@ -1,166 +1,166 @@
1
- import {
2
- ChatCompletion,
3
- ChatCompletionChunk,
4
- ChatCompletionMessage,
5
- ChatCompletionMessageToolCall,
6
- } from "openai/resources";
7
- import { json } from "typia";
8
-
9
- import { ByteArrayUtil } from "../internal/ByteArrayUtil";
10
- import { ChatGptUsageAggregator } from "./ChatGptUsageAggregator";
11
-
12
- export namespace ChatGptCompletionMessageUtil {
13
- export const transformCompletionChunk = (
14
- source: string | Uint8Array,
15
- ): ChatCompletionChunk => {
16
- const str =
17
- source instanceof Uint8Array ? ByteArrayUtil.toUtf8(source) : source;
18
- return json.assertParse<ChatCompletionChunk>(str);
19
- };
20
-
21
- export const accumulate = (
22
- origin: ChatCompletion,
23
- chunk: ChatCompletionChunk,
24
- ): ChatCompletion => {
25
- const choices = origin.choices;
26
- chunk.choices.forEach((choice) => {
27
- const accChoice = choices[choice.index];
28
- if (accChoice) {
29
- choices[choice.index] = mergeChoice(accChoice, choice);
30
- return;
31
- }
32
-
33
- choices[choice.index] = {
34
- index: choice.index,
35
-
36
- finish_reason:
37
- choice.finish_reason ??
38
- (null as unknown as ChatCompletion.Choice["finish_reason"]),
39
-
40
- logprobs: choice.logprobs ?? null,
41
- message: {
42
- tool_calls: choice.delta.tool_calls
43
- ? choice.delta.tool_calls.reduce((acc, cur) => {
44
- acc[cur.index] = {
45
- id: cur.id ?? "",
46
- type: "function",
47
- function: {
48
- name: cur.function?.name ?? "",
49
- arguments: cur.function?.arguments ?? "",
50
- },
51
- };
52
- return acc;
53
- }, [] as ChatCompletionMessageToolCall[])
54
- : undefined,
55
- content: choice.delta.content ?? null,
56
- refusal: choice.delta.refusal ?? null,
57
- role: "assistant",
58
- } satisfies ChatCompletionMessage,
59
- };
60
- });
61
-
62
- const usage = (() => {
63
- if (!chunk.usage) {
64
- return origin.usage;
65
- }
66
-
67
- if (!origin.usage) {
68
- return chunk.usage;
69
- }
70
-
71
- return ChatGptUsageAggregator.sum(origin.usage, chunk.usage);
72
- })();
73
-
74
- return {
75
- ...origin,
76
- choices,
77
- usage,
78
- };
79
- };
80
-
81
- export const merge = (chunks: ChatCompletionChunk[]): ChatCompletion => {
82
- const firstChunk = chunks[0];
83
- if (!firstChunk) throw new Error("No chunks received");
84
-
85
- return chunks.reduce(ChatGptCompletionMessageUtil.accumulate, {
86
- id: firstChunk.id,
87
- choices: [],
88
- created: firstChunk.created,
89
- model: firstChunk.model,
90
- object: "chat.completion",
91
- usage: undefined,
92
- service_tier: firstChunk.service_tier,
93
- system_fingerprint: firstChunk.system_fingerprint,
94
- } as ChatCompletion);
95
- };
96
-
97
- export const mergeChoice = (
98
- acc: ChatCompletion.Choice,
99
- cur: ChatCompletionChunk.Choice,
100
- ): ChatCompletion.Choice => {
101
- if (!acc.finish_reason && cur.finish_reason) {
102
- acc.finish_reason = cur.finish_reason;
103
- }
104
-
105
- if (!acc.logprobs && cur.logprobs) {
106
- acc.logprobs = cur.logprobs;
107
- }
108
-
109
- if (cur.delta.content) {
110
- if (!acc.message.content) {
111
- acc.message.content = cur.delta.content;
112
- } else {
113
- acc.message.content += cur.delta.content;
114
- }
115
- }
116
-
117
- if (cur.delta.refusal) {
118
- if (!acc.message.refusal) {
119
- acc.message.refusal = cur.delta.refusal;
120
- } else {
121
- acc.message.refusal += cur.delta.refusal;
122
- }
123
- }
124
-
125
- if (cur.delta.tool_calls) {
126
- acc.message.tool_calls ??= [];
127
- const toolCalls = acc.message.tool_calls;
128
-
129
- cur.delta.tool_calls.forEach((toolCall) => {
130
- const existingToolCall = toolCalls[toolCall.index];
131
- if (existingToolCall) {
132
- toolCalls[toolCall.index] = mergeToolCalls(
133
- existingToolCall,
134
- toolCall,
135
- );
136
- return;
137
- }
138
-
139
- toolCalls[toolCall.index] = {
140
- id: toolCall.id ?? "",
141
- type: "function",
142
- function: {
143
- name: toolCall.function?.name ?? "",
144
- arguments: toolCall.function?.arguments ?? "",
145
- },
146
- };
147
- });
148
- }
149
-
150
- return acc;
151
- };
152
-
153
- export const mergeToolCalls = (
154
- acc: ChatCompletionMessageToolCall,
155
- cur: ChatCompletionChunk.Choice.Delta.ToolCall,
156
- ): ChatCompletionMessageToolCall => {
157
- if (cur.function) {
158
- acc.function.arguments += cur.function.arguments ?? "";
159
- acc.function.name += cur.function.name ?? "";
160
- }
161
-
162
- acc.id += cur.id ?? "";
163
-
164
- return acc;
165
- };
166
- }
1
+ import {
2
+ ChatCompletion,
3
+ ChatCompletionChunk,
4
+ ChatCompletionMessage,
5
+ ChatCompletionMessageToolCall,
6
+ } from "openai/resources";
7
+ import { json } from "typia";
8
+
9
+ import { ByteArrayUtil } from "../internal/ByteArrayUtil";
10
+ import { ChatGptUsageAggregator } from "./ChatGptUsageAggregator";
11
+
12
+ export namespace ChatGptCompletionMessageUtil {
13
+ export const transformCompletionChunk = (
14
+ source: string | Uint8Array,
15
+ ): ChatCompletionChunk => {
16
+ const str =
17
+ source instanceof Uint8Array ? ByteArrayUtil.toUtf8(source) : source;
18
+ return json.assertParse<ChatCompletionChunk>(str);
19
+ };
20
+
21
+ export const accumulate = (
22
+ origin: ChatCompletion,
23
+ chunk: ChatCompletionChunk,
24
+ ): ChatCompletion => {
25
+ const choices = origin.choices;
26
+ chunk.choices.forEach((choice) => {
27
+ const accChoice = choices[choice.index];
28
+ if (accChoice) {
29
+ choices[choice.index] = mergeChoice(accChoice, choice);
30
+ return;
31
+ }
32
+
33
+ choices[choice.index] = {
34
+ index: choice.index,
35
+
36
+ finish_reason:
37
+ choice.finish_reason ??
38
+ (null as unknown as ChatCompletion.Choice["finish_reason"]),
39
+
40
+ logprobs: choice.logprobs ?? null,
41
+ message: {
42
+ tool_calls: choice.delta.tool_calls
43
+ ? choice.delta.tool_calls.reduce((acc, cur) => {
44
+ acc[cur.index] = {
45
+ id: cur.id ?? "",
46
+ type: "function",
47
+ function: {
48
+ name: cur.function?.name ?? "",
49
+ arguments: cur.function?.arguments ?? "",
50
+ },
51
+ };
52
+ return acc;
53
+ }, [] as ChatCompletionMessageToolCall[])
54
+ : undefined,
55
+ content: choice.delta.content ?? null,
56
+ refusal: choice.delta.refusal ?? null,
57
+ role: "assistant",
58
+ } satisfies ChatCompletionMessage,
59
+ };
60
+ });
61
+
62
+ const usage = (() => {
63
+ if (!chunk.usage) {
64
+ return origin.usage;
65
+ }
66
+
67
+ if (!origin.usage) {
68
+ return chunk.usage;
69
+ }
70
+
71
+ return ChatGptUsageAggregator.sum(origin.usage, chunk.usage);
72
+ })();
73
+
74
+ return {
75
+ ...origin,
76
+ choices,
77
+ usage,
78
+ };
79
+ };
80
+
81
+ export const merge = (chunks: ChatCompletionChunk[]): ChatCompletion => {
82
+ const firstChunk = chunks[0];
83
+ if (!firstChunk) throw new Error("No chunks received");
84
+
85
+ return chunks.reduce(ChatGptCompletionMessageUtil.accumulate, {
86
+ id: firstChunk.id,
87
+ choices: [],
88
+ created: firstChunk.created,
89
+ model: firstChunk.model,
90
+ object: "chat.completion",
91
+ usage: undefined,
92
+ service_tier: firstChunk.service_tier,
93
+ system_fingerprint: firstChunk.system_fingerprint,
94
+ } as ChatCompletion);
95
+ };
96
+
97
+ export const mergeChoice = (
98
+ acc: ChatCompletion.Choice,
99
+ cur: ChatCompletionChunk.Choice,
100
+ ): ChatCompletion.Choice => {
101
+ if (!acc.finish_reason && cur.finish_reason) {
102
+ acc.finish_reason = cur.finish_reason;
103
+ }
104
+
105
+ if (!acc.logprobs && cur.logprobs) {
106
+ acc.logprobs = cur.logprobs;
107
+ }
108
+
109
+ if (cur.delta.content) {
110
+ if (!acc.message.content) {
111
+ acc.message.content = cur.delta.content;
112
+ } else {
113
+ acc.message.content += cur.delta.content;
114
+ }
115
+ }
116
+
117
+ if (cur.delta.refusal) {
118
+ if (!acc.message.refusal) {
119
+ acc.message.refusal = cur.delta.refusal;
120
+ } else {
121
+ acc.message.refusal += cur.delta.refusal;
122
+ }
123
+ }
124
+
125
+ if (cur.delta.tool_calls) {
126
+ acc.message.tool_calls ??= [];
127
+ const toolCalls = acc.message.tool_calls;
128
+
129
+ cur.delta.tool_calls.forEach((toolCall) => {
130
+ const existingToolCall = toolCalls[toolCall.index];
131
+ if (existingToolCall) {
132
+ toolCalls[toolCall.index] = mergeToolCalls(
133
+ existingToolCall,
134
+ toolCall,
135
+ );
136
+ return;
137
+ }
138
+
139
+ toolCalls[toolCall.index] = {
140
+ id: toolCall.id ?? "",
141
+ type: "function",
142
+ function: {
143
+ name: toolCall.function?.name ?? "",
144
+ arguments: toolCall.function?.arguments ?? "",
145
+ },
146
+ };
147
+ });
148
+ }
149
+
150
+ return acc;
151
+ };
152
+
153
+ export const mergeToolCalls = (
154
+ acc: ChatCompletionMessageToolCall,
155
+ cur: ChatCompletionChunk.Choice.Delta.ToolCall,
156
+ ): ChatCompletionMessageToolCall => {
157
+ if (cur.function) {
158
+ acc.function.arguments += cur.function.arguments ?? "";
159
+ acc.function.name += cur.function.name ?? "";
160
+ }
161
+
162
+ acc.id += cur.id ?? "";
163
+
164
+ return acc;
165
+ };
166
+ }
@@ -1,122 +1,122 @@
1
- import { ILlmSchema } from "@samchon/openapi";
2
- import OpenAI from "openai";
3
-
4
- import { AgenticaContext } from "../context/AgenticaContext";
5
- import { AgenticaDescribeEvent } from "../events/AgenticaDescribeEvent";
6
- import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
7
- import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
8
- import { MPSCUtil } from "../internal/MPSCUtil";
9
- import { StreamUtil } from "../internal/StreamUtil";
10
- import { AgenticaDescribePrompt } from "../prompts/AgenticaDescribePrompt";
11
- import { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
12
- import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
13
- import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
14
-
15
- export namespace ChatGptDescribeFunctionAgent {
16
- export const execute = async <Model extends ILlmSchema.Model>(
17
- ctx: AgenticaContext<Model>,
18
- histories: AgenticaExecutePrompt<Model>[],
19
- ): Promise<AgenticaDescribePrompt<Model>[]> => {
20
- if (histories.length === 0) return [];
21
- const completionStream = await ctx.request("describe", {
22
- messages: [
23
- // COMMON SYSTEM PROMPT
24
- {
25
- role: "system",
26
- content: AgenticaDefaultPrompt.write(ctx.config),
27
- } satisfies OpenAI.ChatCompletionSystemMessageParam,
28
- // FUNCTION CALLING HISTORIES
29
- ...histories.map(ChatGptHistoryDecoder.decode).flat(),
30
- // SYSTEM PROMPT
31
- {
32
- role: "system",
33
- content:
34
- ctx.config?.systemPrompt?.describe?.(histories) ??
35
- AgenticaSystemPrompt.DESCRIBE,
36
- },
37
- ],
38
- });
39
-
40
- const describeContext: ({
41
- content: string;
42
- } & ReturnType<typeof MPSCUtil.create<string>>)[] = [];
43
-
44
- const completion = await StreamUtil.reduce<
45
- OpenAI.ChatCompletionChunk,
46
- Promise<OpenAI.ChatCompletion>
47
- >(completionStream, async (accPromise, chunk) => {
48
- const acc = await accPromise;
49
- const registerContext = (
50
- choices: OpenAI.ChatCompletionChunk.Choice[],
51
- ) => {
52
- for (const choice of choices) {
53
- if (choice.finish_reason) {
54
- describeContext[choice.index]!.close();
55
- continue;
56
- }
57
- if (!choice.delta.content) {
58
- continue;
59
- }
60
-
61
- if (describeContext[choice.index]) {
62
- describeContext[choice.index]!.content += choice.delta.content;
63
- describeContext[choice.index]!.produce(choice.delta.content);
64
- continue;
65
- }
66
-
67
- const { consumer, produce, close, waitClosed, waitUntilEmpty, done } =
68
- MPSCUtil.create<string>();
69
-
70
- describeContext[choice.index] = {
71
- content: choice.delta.content,
72
- consumer,
73
- produce,
74
- close,
75
- waitClosed,
76
- waitUntilEmpty,
77
- done,
78
- };
79
- produce(choice.delta.content);
80
-
81
- void ctx.dispatch(
82
- new AgenticaDescribeEvent({
83
- executes: histories,
84
- stream: consumer,
85
- done,
86
- get: () => describeContext[choice.index]?.content ?? "",
87
- join: async () => {
88
- await waitClosed();
89
- return describeContext[choice.index]!.content;
90
- },
91
- }),
92
- );
93
- }
94
- };
95
-
96
- if (acc.object === "chat.completion.chunk") {
97
- registerContext([acc, chunk].flatMap((acc) => acc.choices));
98
- return ChatGptCompletionMessageUtil.merge([acc, chunk]);
99
- }
100
-
101
- registerContext(chunk.choices);
102
- return ChatGptCompletionMessageUtil.accumulate(acc, chunk);
103
- });
104
-
105
- if (!completion) throw new Error("No completion received");
106
- const descriptions: AgenticaDescribePrompt<Model>[] = completion.choices
107
- .map((choice) =>
108
- choice.message.role === "assistant" && !!choice.message.content?.length
109
- ? choice.message.content
110
- : null,
111
- )
112
- .filter((str) => str !== null)
113
- .map(
114
- (content) =>
115
- new AgenticaDescribePrompt({
116
- executes: histories,
117
- text: content,
118
- }),
119
- );
120
- return descriptions;
121
- };
122
- }
1
+ import { ILlmSchema } from "@samchon/openapi";
2
+ import OpenAI from "openai";
3
+
4
+ import { AgenticaContext } from "../context/AgenticaContext";
5
+ import { AgenticaDescribeEvent } from "../events/AgenticaDescribeEvent";
6
+ import { AgenticaDefaultPrompt } from "../internal/AgenticaDefaultPrompt";
7
+ import { AgenticaSystemPrompt } from "../internal/AgenticaSystemPrompt";
8
+ import { MPSCUtil } from "../internal/MPSCUtil";
9
+ import { StreamUtil } from "../internal/StreamUtil";
10
+ import { AgenticaDescribePrompt } from "../prompts/AgenticaDescribePrompt";
11
+ import { AgenticaExecutePrompt } from "../prompts/AgenticaExecutePrompt";
12
+ import { ChatGptCompletionMessageUtil } from "./ChatGptCompletionMessageUtil";
13
+ import { ChatGptHistoryDecoder } from "./ChatGptHistoryDecoder";
14
+
15
+ export namespace ChatGptDescribeFunctionAgent {
16
+ export const execute = async <Model extends ILlmSchema.Model>(
17
+ ctx: AgenticaContext<Model>,
18
+ histories: AgenticaExecutePrompt<Model>[],
19
+ ): Promise<AgenticaDescribePrompt<Model>[]> => {
20
+ if (histories.length === 0) return [];
21
+ const completionStream = await ctx.request("describe", {
22
+ messages: [
23
+ // COMMON SYSTEM PROMPT
24
+ {
25
+ role: "system",
26
+ content: AgenticaDefaultPrompt.write(ctx.config),
27
+ } satisfies OpenAI.ChatCompletionSystemMessageParam,
28
+ // FUNCTION CALLING HISTORIES
29
+ ...histories.map(ChatGptHistoryDecoder.decode).flat(),
30
+ // SYSTEM PROMPT
31
+ {
32
+ role: "system",
33
+ content:
34
+ ctx.config?.systemPrompt?.describe?.(histories) ??
35
+ AgenticaSystemPrompt.DESCRIBE,
36
+ },
37
+ ],
38
+ });
39
+
40
+ const describeContext: ({
41
+ content: string;
42
+ } & ReturnType<typeof MPSCUtil.create<string>>)[] = [];
43
+
44
+ const completion = await StreamUtil.reduce<
45
+ OpenAI.ChatCompletionChunk,
46
+ Promise<OpenAI.ChatCompletion>
47
+ >(completionStream, async (accPromise, chunk) => {
48
+ const acc = await accPromise;
49
+ const registerContext = (
50
+ choices: OpenAI.ChatCompletionChunk.Choice[],
51
+ ) => {
52
+ for (const choice of choices) {
53
+ if (choice.finish_reason) {
54
+ describeContext[choice.index]!.close();
55
+ continue;
56
+ }
57
+ if (!choice.delta.content) {
58
+ continue;
59
+ }
60
+
61
+ if (describeContext[choice.index]) {
62
+ describeContext[choice.index]!.content += choice.delta.content;
63
+ describeContext[choice.index]!.produce(choice.delta.content);
64
+ continue;
65
+ }
66
+
67
+ const { consumer, produce, close, waitClosed, waitUntilEmpty, done } =
68
+ MPSCUtil.create<string>();
69
+
70
+ describeContext[choice.index] = {
71
+ content: choice.delta.content,
72
+ consumer,
73
+ produce,
74
+ close,
75
+ waitClosed,
76
+ waitUntilEmpty,
77
+ done,
78
+ };
79
+ produce(choice.delta.content);
80
+
81
+ void ctx.dispatch(
82
+ new AgenticaDescribeEvent({
83
+ executes: histories,
84
+ stream: consumer,
85
+ done,
86
+ get: () => describeContext[choice.index]?.content ?? "",
87
+ join: async () => {
88
+ await waitClosed();
89
+ return describeContext[choice.index]!.content;
90
+ },
91
+ }),
92
+ );
93
+ }
94
+ };
95
+
96
+ if (acc.object === "chat.completion.chunk") {
97
+ registerContext([acc, chunk].flatMap((acc) => acc.choices));
98
+ return ChatGptCompletionMessageUtil.merge([acc, chunk]);
99
+ }
100
+
101
+ registerContext(chunk.choices);
102
+ return ChatGptCompletionMessageUtil.accumulate(acc, chunk);
103
+ });
104
+
105
+ if (!completion) throw new Error("No completion received");
106
+ const descriptions: AgenticaDescribePrompt<Model>[] = completion.choices
107
+ .map((choice) =>
108
+ choice.message.role === "assistant" && !!choice.message.content?.length
109
+ ? choice.message.content
110
+ : null,
111
+ )
112
+ .filter((str) => str !== null)
113
+ .map(
114
+ (content) =>
115
+ new AgenticaDescribePrompt({
116
+ executes: histories,
117
+ text: content,
118
+ }),
119
+ );
120
+ return descriptions;
121
+ };
122
+ }