@agentica/core 0.32.3-dev.2 → 0.32.3

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.
package/lib/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import { is_node, Semaphore } from "tstl";
2
2
 
3
- import { v4 } from "uuid";
4
-
5
3
  import "typia";
6
4
 
5
+ import { v4 } from "uuid";
6
+
7
7
  import { HttpLlm, OpenApi, McpLlm } from "@samchon/openapi";
8
8
 
9
9
  import * as __typia_transform__validateReport from "typia/lib/internal/_validateReport.js";
@@ -1375,7 +1375,7 @@ async function call(ctx, operations) {
1375
1375
  });
1376
1376
  const completion = await reduceStreamingWithDispatch(stream, (props => {
1377
1377
  const event = createAssistantMessageEvent(props);
1378
- ctx.dispatch(event);
1378
+ void ctx.dispatch(event).catch((() => {}));
1379
1379
  }));
1380
1380
  const allAssistantMessagesEmpty = completion.choices.every((v => v.message.tool_calls == null && v.message.content === ""));
1381
1381
  if (allAssistantMessagesEmpty) {
@@ -1394,7 +1394,7 @@ async function call(ctx, operations) {
1394
1394
  get: () => "",
1395
1395
  join: async () => ""
1396
1396
  });
1397
- ctx.dispatch(event);
1397
+ void ctx.dispatch(event).catch((() => {}));
1398
1398
  return [];
1399
1399
  }
1400
1400
  const executes = [];
@@ -1407,7 +1407,7 @@ async function call(ctx, operations) {
1407
1407
  continue;
1408
1408
  }
1409
1409
  const event = await predicate(ctx, operation, tc, [], retry);
1410
- ctx.dispatch(event);
1410
+ await ctx.dispatch(event);
1411
1411
  executes.push(event);
1412
1412
  if (isAgenticaContext(ctx)) {
1413
1413
  cancelFunctionFromContext(ctx, {
@@ -1423,7 +1423,7 @@ async function call(ctx, operations) {
1423
1423
 
1424
1424
  async function predicate(ctx, operation, toolCall, previousValidationErrors, life) {
1425
1425
  const call = parseArguments(operation, toolCall);
1426
- ctx.dispatch(call);
1426
+ await ctx.dispatch(call);
1427
1427
  if (call.type === "jsonParseError") {
1428
1428
  return correctJsonError(ctx, call, previousValidationErrors, life - 1);
1429
1429
  }
@@ -1434,7 +1434,7 @@ async function predicate(ctx, operation, toolCall, previousValidationErrors, lif
1434
1434
  operation,
1435
1435
  result: check
1436
1436
  });
1437
- ctx.dispatch(event);
1437
+ await ctx.dispatch(event);
1438
1438
  return correctTypeError(ctx, call, event, [ ...previousValidationErrors, event ], life - 1);
1439
1439
  }
1440
1440
  return executeFunction(call, operation);
@@ -1741,16 +1741,17 @@ async function cancel(ctx) {
1741
1741
  await Promise.all(ctx.operations.divided.map((async (operations, i) => step$1({
1742
1742
  ...ctx,
1743
1743
  stack: stacks[i],
1744
- dispatch: e => {
1744
+ dispatch: async e => {
1745
1745
  events.push(e);
1746
- return e;
1747
1746
  }
1748
1747
  }, operations, 0))));
1749
1748
  if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true && stacks.some((s => s.length !== 0))) {
1750
1749
  return step$1(ctx, stacks.flat().map((s => ctx.operations.group.get(s.operation.controller.name).get(s.operation.function.name))), 0);
1751
1750
  } else {
1752
1751
  const cancelled = events.filter((e => e.type === "cancel"));
1753
- (cancelled.length !== 0 ? cancelled : events).forEach(ctx.dispatch);
1752
+ (cancelled.length !== 0 ? cancelled : events).forEach((e => {
1753
+ void ctx.dispatch(e).catch((() => {}));
1754
+ }));
1754
1755
  }
1755
1756
  }
1756
1757
 
@@ -2750,16 +2751,17 @@ async function select(ctx) {
2750
2751
  await Promise.all(ctx.operations.divided.map((async (operations, i) => step({
2751
2752
  ...ctx,
2752
2753
  stack: stacks[i],
2753
- dispatch: e => {
2754
+ dispatch: async e => {
2754
2755
  events.push(e);
2755
- return e;
2756
2756
  }
2757
2757
  }, operations, 0))));
2758
2758
  if ((ctx.config?.eliticism ?? AgenticaConstant.ELITICISM) === true && stacks.some((s => s.length !== 0))) {
2759
2759
  return step(ctx, stacks.flat().map((s => ctx.operations.group.get(s.operation.controller.name).get(s.operation.function.name))), 0);
2760
2760
  } else {
2761
2761
  const selected = events.filter((e => e.type === "select"));
2762
- (selected.length !== 0 ? selected : events).forEach(ctx.dispatch);
2762
+ (selected.length !== 0 ? selected : events).forEach((e => {
2763
+ void ctx.dispatch(e).catch((() => {}));
2764
+ }));
2763
2765
  }
2764
2766
  }
2765
2767
 
@@ -2820,7 +2822,7 @@ async function step(ctx, operations, retry, failures) {
2820
2822
  });
2821
2823
  const completion = await reduceStreamingWithDispatch(stream, (props => {
2822
2824
  const event = createAssistantMessageEvent(props);
2823
- ctx.dispatch(event);
2825
+ void ctx.dispatch(event).catch((() => {}));
2824
2826
  }));
2825
2827
  const allAssistantMessagesEmpty = completion.choices.every((v => v.message.tool_calls == null && v.message.content === ""));
2826
2828
  if (allAssistantMessagesEmpty) {
@@ -2839,7 +2841,7 @@ async function step(ctx, operations, retry, failures) {
2839
2841
  get: () => "",
2840
2842
  join: async () => ""
2841
2843
  });
2842
- ctx.dispatch(event);
2844
+ void ctx.dispatch(event).catch((() => {}));
2843
2845
  return;
2844
2846
  }
2845
2847
  if (retry++ < (ctx.config?.retry ?? AgenticaConstant.RETRY)) {
@@ -3113,6 +3115,102 @@ function findOperation(props) {
3113
3115
  return found;
3114
3116
  }
3115
3117
 
3118
+ var index$1 = Object.freeze({
3119
+ __proto__: null,
3120
+ createAssistantMessageEvent,
3121
+ createAssistantMessageHistory,
3122
+ createCallEvent,
3123
+ createCancelEvent,
3124
+ createCancelHistory,
3125
+ createDescribeEvent,
3126
+ createDescribeHistory,
3127
+ createExecuteEvent,
3128
+ createExecuteHistory,
3129
+ createInitializeEvent,
3130
+ createJsonParseErrorEvent,
3131
+ createOperationSelection,
3132
+ createRequestEvent,
3133
+ createResponseEvent,
3134
+ createSelectEvent,
3135
+ createSelectHistory,
3136
+ createSystemMessageHistory,
3137
+ createUserMessageEvent,
3138
+ createUserMessageHistory,
3139
+ createValidateEvent,
3140
+ decodeHistory,
3141
+ decodeUserMessageContent
3142
+ });
3143
+
3144
+ const getChatCompletionWithStreamingFunction = props => async (source, body) => {
3145
+ const event = createRequestEvent({
3146
+ source,
3147
+ body: {
3148
+ ...body,
3149
+ model: props.vendor.model,
3150
+ stream: true,
3151
+ stream_options: {
3152
+ include_usage: true
3153
+ }
3154
+ },
3155
+ options: {
3156
+ ...props.vendor.options,
3157
+ signal: props.abortSignal
3158
+ }
3159
+ });
3160
+ await props.dispatch(event);
3161
+ const backoffStrategy = props.config?.backoffStrategy ?? (props => {
3162
+ throw props.error;
3163
+ });
3164
+ const completion = await (async () => {
3165
+ let count = 0;
3166
+ while (true) {
3167
+ try {
3168
+ return await props.vendor.api.chat.completions.create(event.body, event.options);
3169
+ } catch (error) {
3170
+ const waiting = backoffStrategy({
3171
+ count,
3172
+ error
3173
+ });
3174
+ await new Promise((resolve => setTimeout(resolve, waiting)));
3175
+ count++;
3176
+ }
3177
+ }
3178
+ })();
3179
+ const [streamForEvent, temporaryStream] = StreamUtil.transform(completion.toReadableStream(), (value => ChatGptCompletionMessageUtil.transformCompletionChunk(value))).tee();
3180
+ const [streamForAggregate, streamForReturn] = temporaryStream.tee();
3181
+ (async () => {
3182
+ const reader = streamForAggregate.getReader();
3183
+ while (true) {
3184
+ const chunk = await reader.read();
3185
+ if (chunk.done) {
3186
+ break;
3187
+ }
3188
+ if (chunk.value.usage != null) {
3189
+ AgenticaTokenUsageAggregator.aggregate({
3190
+ kind: source,
3191
+ completionUsage: chunk.value.usage,
3192
+ usage: props.usage
3193
+ });
3194
+ }
3195
+ }
3196
+ })().catch((() => {}));
3197
+ const [streamForStream, streamForJoin] = streamForEvent.tee();
3198
+ void props.dispatch({
3199
+ id: v4(),
3200
+ type: "response",
3201
+ source,
3202
+ stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
3203
+ body: event.body,
3204
+ options: event.options,
3205
+ join: async () => {
3206
+ const chunks = await StreamUtil.readAll(streamForJoin);
3207
+ return ChatGptCompletionMessageUtil.merge(chunks);
3208
+ },
3209
+ created_at: (new Date).toISOString()
3210
+ }).catch((() => {}));
3211
+ return streamForReturn;
3212
+ };
3213
+
3116
3214
  class Agentica {
3117
3215
  constructor(props) {
3118
3216
  this.props = props;
@@ -3139,18 +3237,20 @@ class Agentica {
3139
3237
  }
3140
3238
  async conversate(content, options = {}) {
3141
3239
  const historyGetters = [];
3142
- const dispatch = event => {
3143
- this.dispatch(event).catch((() => {}));
3144
- if ("toHistory" in event) {
3145
- if ("join" in event) {
3146
- historyGetters.push((async () => {
3147
- await event.join();
3148
- return event.toHistory();
3149
- }));
3150
- } else {
3151
- historyGetters.push((async () => event.toHistory()));
3240
+ const dispatch = async event => {
3241
+ try {
3242
+ await this.dispatch(event);
3243
+ if ("toHistory" in event) {
3244
+ if ("join" in event) {
3245
+ historyGetters.push((async () => {
3246
+ await event.join();
3247
+ return event.toHistory();
3248
+ }));
3249
+ } else {
3250
+ historyGetters.push((async () => event.toHistory()));
3251
+ }
3152
3252
  }
3153
- }
3253
+ } catch {}
3154
3254
  };
3155
3255
  const prompt = createUserMessageEvent({
3156
3256
  contents: Array.isArray(content) ? content : typeof content === "string" ? [ {
@@ -3158,7 +3258,7 @@ class Agentica {
3158
3258
  text: content
3159
3259
  } ] : [ content ]
3160
3260
  });
3161
- dispatch(prompt);
3261
+ void dispatch(prompt).catch((() => {}));
3162
3262
  await this.executor_(this.getContext({
3163
3263
  dispatch,
3164
3264
  prompt: prompt.toHistory(),
@@ -3188,75 +3288,13 @@ class Agentica {
3188
3288
  return this.token_usage_;
3189
3289
  }
3190
3290
  getContext(props) {
3191
- const request = async (source, body) => {
3192
- const event = createRequestEvent({
3193
- source,
3194
- body: {
3195
- ...body,
3196
- model: this.props.vendor.model,
3197
- stream: true,
3198
- stream_options: {
3199
- include_usage: true
3200
- }
3201
- },
3202
- options: {
3203
- ...this.props.vendor.options,
3204
- signal: props.abortSignal
3205
- }
3206
- });
3207
- props.dispatch(event);
3208
- const backoffStrategy = this.props.config?.backoffStrategy ?? (props => {
3209
- throw props.error;
3210
- });
3211
- const completion = await (async () => {
3212
- let count = 0;
3213
- while (true) {
3214
- try {
3215
- return await this.props.vendor.api.chat.completions.create(event.body, event.options);
3216
- } catch (error) {
3217
- const waiting = backoffStrategy({
3218
- count,
3219
- error
3220
- });
3221
- await new Promise((resolve => setTimeout(resolve, waiting)));
3222
- count++;
3223
- }
3224
- }
3225
- })();
3226
- const [streamForEvent, temporaryStream] = StreamUtil.transform(completion.toReadableStream(), (value => ChatGptCompletionMessageUtil.transformCompletionChunk(value))).tee();
3227
- const [streamForAggregate, streamForReturn] = temporaryStream.tee();
3228
- (async () => {
3229
- const reader = streamForAggregate.getReader();
3230
- while (true) {
3231
- const chunk = await reader.read();
3232
- if (chunk.done) {
3233
- break;
3234
- }
3235
- if (chunk.value.usage != null) {
3236
- AgenticaTokenUsageAggregator.aggregate({
3237
- kind: source,
3238
- completionUsage: chunk.value.usage,
3239
- usage: props.usage
3240
- });
3241
- }
3242
- }
3243
- })().catch((() => {}));
3244
- const [streamForStream, streamForJoin] = streamForEvent.tee();
3245
- props.dispatch({
3246
- id: v4(),
3247
- type: "response",
3248
- source,
3249
- stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
3250
- body: event.body,
3251
- options: event.options,
3252
- join: async () => {
3253
- const chunks = await StreamUtil.readAll(streamForJoin);
3254
- return ChatGptCompletionMessageUtil.merge(chunks);
3255
- },
3256
- created_at: (new Date).toISOString()
3257
- });
3258
- return streamForReturn;
3259
- };
3291
+ const request = getChatCompletionWithStreamingFunction({
3292
+ vendor: this.props.vendor,
3293
+ config: this.props.config,
3294
+ dispatch: props.dispatch,
3295
+ abortSignal: props.abortSignal,
3296
+ usage: this.token_usage_
3297
+ });
3260
3298
  return {
3261
3299
  operations: this.operations_,
3262
3300
  config: this.props.config,
@@ -3276,7 +3314,7 @@ class Agentica {
3276
3314
  },
3277
3315
  initialize: async () => {
3278
3316
  this.ready_ = true;
3279
- props.dispatch(createInitializeEvent());
3317
+ void props.dispatch(createInitializeEvent()).catch((() => {}));
3280
3318
  }
3281
3319
  };
3282
3320
  }
@@ -3306,32 +3344,6 @@ class Agentica {
3306
3344
  }
3307
3345
  }
3308
3346
 
3309
- var index$1 = Object.freeze({
3310
- __proto__: null,
3311
- createAssistantMessageEvent,
3312
- createAssistantMessageHistory,
3313
- createCallEvent,
3314
- createCancelEvent,
3315
- createCancelHistory,
3316
- createDescribeEvent,
3317
- createDescribeHistory,
3318
- createExecuteEvent,
3319
- createExecuteHistory,
3320
- createInitializeEvent,
3321
- createJsonParseErrorEvent,
3322
- createOperationSelection,
3323
- createRequestEvent,
3324
- createResponseEvent,
3325
- createSelectEvent,
3326
- createSelectHistory,
3327
- createSystemMessageHistory,
3328
- createUserMessageEvent,
3329
- createUserMessageHistory,
3330
- createValidateEvent,
3331
- decodeHistory,
3332
- decodeUserMessageContent
3333
- });
3334
-
3335
3347
  function assertHttpController(props) {
3336
3348
  const document = OpenApi.convert((() => {
3337
3349
  const _io0 = input => null !== input.swagger && undefined !== input.swagger && ("2.0" === input.swagger || "string" === typeof input.swagger && RegExp(/^2\.0\.[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/).test(input.swagger)) && (undefined === input.info || "object" === typeof input.info && null !== input.info && _io1(input.info)) && (undefined === input.host || "string" === typeof input.host) && (undefined === input.basePath || "string" === typeof input.basePath) && (undefined === input.consumes || Array.isArray(input.consumes) && input.consumes.every((elem => "string" === typeof elem))) && (undefined === input.produces || Array.isArray(input.produces) && input.produces.every((elem => "string" === typeof elem))) && (undefined === input.definitions || "object" === typeof input.definitions && null !== input.definitions && false === Array.isArray(input.definitions) && _io4(input.definitions)) && (undefined === input.parameters || "object" === typeof input.parameters && null !== input.parameters && false === Array.isArray(input.parameters) && _io16(input.parameters)) && (undefined === input.responses || "object" === typeof input.responses && null !== input.responses && false === Array.isArray(input.responses) && _io29(input.responses)) && (undefined === input.securityDefinitions || "object" === typeof input.securityDefinitions && null !== input.securityDefinitions && false === Array.isArray(input.securityDefinitions) && _io31(input.securityDefinitions)) && (undefined === input.security || Array.isArray(input.security) && input.security.every((elem => "object" === typeof elem && null !== elem && false === Array.isArray(elem) && _io39(elem)))) && (undefined === input.paths || "object" === typeof input.paths && null !== input.paths && false === Array.isArray(input.paths) && _io40(input.paths)) && (undefined === input.tags || Array.isArray(input.tags) && input.tags.every((elem => "object" === typeof elem && null !== elem && _io47(elem))));
@@ -42016,20 +42028,22 @@ class MicroAgentica {
42016
42028
  histories: this.props.histories?.slice()
42017
42029
  });
42018
42030
  }
42019
- async conversate(content) {
42031
+ async conversate(content, options = {}) {
42020
42032
  const histories = [];
42021
- const dispatch = event => {
42022
- this.dispatch(event).catch((() => {}));
42023
- if ("toHistory" in event) {
42024
- if ("join" in event) {
42025
- histories.push((async () => {
42026
- await event.join();
42027
- return event.toHistory();
42028
- }));
42029
- } else {
42030
- histories.push((async () => event.toHistory()));
42033
+ const dispatch = async event => {
42034
+ try {
42035
+ await this.dispatch(event);
42036
+ if ("toHistory" in event) {
42037
+ if ("join" in event) {
42038
+ histories.push((async () => {
42039
+ await event.join();
42040
+ return event.toHistory();
42041
+ }));
42042
+ } else {
42043
+ histories.push((async () => event.toHistory()));
42044
+ }
42031
42045
  }
42032
- }
42046
+ } catch {}
42033
42047
  };
42034
42048
  const prompt = createUserMessageEvent({
42035
42049
  contents: Array.isArray(content) ? content : typeof content === "string" ? [ {
@@ -42037,11 +42051,12 @@ class MicroAgentica {
42037
42051
  text: content
42038
42052
  } ] : [ content ]
42039
42053
  });
42040
- dispatch(prompt);
42054
+ void dispatch(prompt).catch((() => {}));
42041
42055
  const ctx = this.getContext({
42042
42056
  prompt,
42043
42057
  dispatch,
42044
- usage: this.token_usage_
42058
+ usage: this.token_usage_,
42059
+ abortSignal: options.abortSignal
42045
42060
  });
42046
42061
  const executes = await call(ctx, this.operations_.array);
42047
42062
  if (executes.length && this.props.config?.executor?.describe !== null && this.props.config?.executor?.describe !== false) {
@@ -42070,72 +42085,13 @@ class MicroAgentica {
42070
42085
  return this.token_usage_;
42071
42086
  }
42072
42087
  getContext(props) {
42073
- const request = async (source, body) => {
42074
- const event = createRequestEvent({
42075
- source,
42076
- body: {
42077
- ...body,
42078
- model: this.props.vendor.model,
42079
- stream: true,
42080
- stream_options: {
42081
- include_usage: true
42082
- }
42083
- },
42084
- options: this.props.vendor.options
42085
- });
42086
- props.dispatch(event);
42087
- const backoffStrategy = this.props.config?.backoffStrategy ?? (props => {
42088
- throw props.error;
42089
- });
42090
- const completion = await (async () => {
42091
- let count = 0;
42092
- while (true) {
42093
- try {
42094
- return await this.props.vendor.api.chat.completions.create(event.body, event.options);
42095
- } catch (error) {
42096
- const waiting = backoffStrategy({
42097
- count,
42098
- error
42099
- });
42100
- await new Promise((resolve => setTimeout(resolve, waiting)));
42101
- count++;
42102
- }
42103
- }
42104
- })();
42105
- const [streamForEvent, temporaryStream] = StreamUtil.transform(completion.toReadableStream(), (value => ChatGptCompletionMessageUtil.transformCompletionChunk(value))).tee();
42106
- const [streamForAggregate, streamForReturn] = temporaryStream.tee();
42107
- void (async () => {
42108
- const reader = streamForAggregate.getReader();
42109
- while (true) {
42110
- const chunk = await reader.read();
42111
- if (chunk.done) {
42112
- break;
42113
- }
42114
- if (chunk.value.usage != null) {
42115
- AgenticaTokenUsageAggregator.aggregate({
42116
- kind: source,
42117
- completionUsage: chunk.value.usage,
42118
- usage: props.usage
42119
- });
42120
- }
42121
- }
42122
- })().catch((() => {}));
42123
- const [streamForStream, streamForJoin] = streamForEvent.tee();
42124
- props.dispatch({
42125
- id: v4(),
42126
- type: "response",
42127
- source,
42128
- stream: streamDefaultReaderToAsyncGenerator(streamForStream.getReader()),
42129
- body: event.body,
42130
- options: event.options,
42131
- join: async () => {
42132
- const chunks = await StreamUtil.readAll(streamForJoin);
42133
- return ChatGptCompletionMessageUtil.merge(chunks);
42134
- },
42135
- created_at: (new Date).toISOString()
42136
- });
42137
- return streamForReturn;
42138
- };
42088
+ const request = getChatCompletionWithStreamingFunction({
42089
+ vendor: this.props.vendor,
42090
+ config: this.props.config,
42091
+ dispatch: props.dispatch,
42092
+ abortSignal: props.abortSignal,
42093
+ usage: this.token_usage_
42094
+ });
42139
42095
  return {
42140
42096
  operations: this.operations_,
42141
42097
  config: this.props.config,