@toolforge-js/sdk 0.8.6 → 0.8.8

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.
@@ -1,8 +1,9 @@
1
1
  import { createRequire } from "node:module";
2
2
  import * as z$1 from "zod";
3
- import { nanoid } from "nanoid";
4
3
  import { P, match } from "ts-pattern";
5
4
  import { setTimeout } from "node:timers/promises";
5
+ import { nanoid } from "nanoid";
6
+ import { AsyncLocalStorage } from "node:async_hooks";
6
7
 
7
8
  //#region rolldown:runtime
8
9
  var __create = Object.create;
@@ -30,22 +31,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
31
  }) : target, mod));
31
32
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
32
33
 
33
- //#endregion
34
- //#region src/lib/utils.ts
35
- function convertToWords(input) {
36
- if (input.includes("-") || input.includes("_")) return input.split(/[-_]+/).filter((word) => word.length > 0).map((word) => word.toLowerCase());
37
- return input.split(/(?=[A-Z])/).filter((word) => word.length > 0).map((word) => word.toLowerCase());
38
- }
39
- function invariant(cond, message) {
40
- if (!cond) throw new Error(message);
41
- }
42
- function getErrorMessage(error, defaultMessage = "Something went wrong. Please try again") {
43
- let errorMessage = defaultMessage;
44
- if (error instanceof z$1.ZodError) errorMessage = error.issues.length ? error.issues.map((e) => e.message).join(", ") : error.message;
45
- else if (error instanceof Error) errorMessage = error.message;
46
- return errorMessage;
47
- }
48
-
49
34
  //#endregion
50
35
  //#region ../../node_modules/.bun/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js
51
36
  var require_dayjs_min = /* @__PURE__ */ __commonJS({ "../../node_modules/.bun/dayjs@1.11.18/node_modules/dayjs/dayjs.min.js": ((exports, module) => {
@@ -2094,6 +2079,27 @@ function runWithRetries(fn, retries = 3, config = {}) {
2094
2079
  return execute();
2095
2080
  }
2096
2081
 
2082
+ //#endregion
2083
+ //#region src/lib/utils.ts
2084
+ function convertToWords(input) {
2085
+ if (input.includes("-") || input.includes("_")) return input.split(/[-_]+/).filter((word) => word.length > 0).map((word) => word.toLowerCase());
2086
+ return input.split(/(?=[A-Z])/).filter((word) => word.length > 0).map((word) => word.toLowerCase());
2087
+ }
2088
+ function invariant(cond, message) {
2089
+ if (!cond) throw new Error(message);
2090
+ }
2091
+ function getErrorMessage(error, defaultMessage = "Something went wrong. Please try again") {
2092
+ let errorMessage = defaultMessage;
2093
+ if (error instanceof z$1.ZodError) errorMessage = error.issues.length ? error.issues.map((e) => e.message).join(", ") : error.message;
2094
+ else if (error instanceof Error) errorMessage = error.message;
2095
+ return errorMessage;
2096
+ }
2097
+
2098
+ //#endregion
2099
+ //#region src/internal/store.ts
2100
+ const runnerIdStore = new AsyncLocalStorage();
2101
+ const sessionIdStore = new AsyncLocalStorage();
2102
+
2097
2103
  //#endregion
2098
2104
  //#region src/internal/loader.ts
2099
2105
  var Loader = class {
@@ -2105,7 +2111,9 @@ var Loader = class {
2105
2111
  #webSocketClient;
2106
2112
  #signal;
2107
2113
  constructor(options) {
2108
- this.#sessionId = options.sessionId;
2114
+ const sessionId = sessionIdStore.getStore();
2115
+ if (!sessionId) throw new Error("sessionId not found");
2116
+ this.#sessionId = sessionId;
2109
2117
  this.#executionContext = options.executionContext;
2110
2118
  this.#title = options.title;
2111
2119
  this.#description = options.description;
@@ -2159,7 +2167,9 @@ var ProgressLoader = class {
2159
2167
  this.#title = options.title;
2160
2168
  this.#description = options.description;
2161
2169
  this.#itemsInQueue = options.itemsInQueue;
2162
- this.#sessionId = options.sessionId;
2170
+ const sessionId = sessionIdStore.getStore();
2171
+ if (!sessionId) throw new Error("sessionId not found");
2172
+ this.#sessionId = sessionId;
2163
2173
  this.#executionContext = options.executionContext;
2164
2174
  this.#webSocketClient = options.webSocketClient;
2165
2175
  this.#signal = options.signal;
@@ -2247,10 +2257,15 @@ var IO = class {
2247
2257
  */
2248
2258
  #pendingIOs = /* @__PURE__ */ new Map();
2249
2259
  constructor(options, logger) {
2250
- this.#sessionId = options.sessionId;
2251
- this.#runnerId = options.runnerId;
2252
2260
  this.#executionContext = options.executionContext;
2261
+ const runnerId$1 = runnerIdStore.getStore();
2262
+ if (!runnerId$1) throw new Error("runnerId not found");
2263
+ this.#runnerId = runnerId$1;
2264
+ const sessionId = sessionIdStore.getStore();
2265
+ if (!sessionId) throw new Error("sessionId not found");
2266
+ this.#sessionId = sessionId;
2253
2267
  this.#logger = logger.child({
2268
+ runnerId: this.#runnerId,
2254
2269
  sessionId: this.#sessionId,
2255
2270
  component: "IO"
2256
2271
  });
@@ -2580,7 +2595,6 @@ var IO = class {
2580
2595
  return new Loader({
2581
2596
  title,
2582
2597
  description,
2583
- sessionId: this.#sessionId,
2584
2598
  webSocketClient: this.#webSocketClient,
2585
2599
  signal: this.#signal,
2586
2600
  executionContext: this.#executionContext
@@ -2596,7 +2610,6 @@ var IO = class {
2596
2610
  description,
2597
2611
  itemsInQueue,
2598
2612
  webSocketClient: this.#webSocketClient,
2599
- sessionId: this.#sessionId,
2600
2613
  signal: this.#signal,
2601
2614
  executionContext: this.#executionContext
2602
2615
  });
@@ -2677,6 +2690,88 @@ var IO = class {
2677
2690
  }
2678
2691
  };
2679
2692
 
2693
+ //#endregion
2694
+ //#region src/internal/tool.ts
2695
+ const TOOL_HANDLER_TAG_NAME = Symbol("TOOL_HANDLER");
2696
+ const TOOL_TAG_NAME = Symbol("TOOL");
2697
+ /**
2698
+ * Tool class is responsible for running the tool's logic.
2699
+ * It manages the tool lifecycle, communication with the worker, and handles sending messages back to the ForgeRunner.
2700
+ * It uses the IO component to facilitate input/output operations for the tool.
2701
+ */
2702
+ var Tool = class {
2703
+ #runnerId;
2704
+ #sessionId;
2705
+ #metadata;
2706
+ #abortController = new AbortController();
2707
+ #handler;
2708
+ #io;
2709
+ #block;
2710
+ #logger;
2711
+ constructor(options, logger) {
2712
+ this.#handler = options.handler;
2713
+ const runnerId$1 = runnerIdStore.getStore();
2714
+ if (!runnerId$1) throw new Error("runnerId not found");
2715
+ this.#runnerId = runnerId$1;
2716
+ const sessionId = sessionIdStore.getStore();
2717
+ if (!sessionId) throw new Error("sessionId not found");
2718
+ this.#sessionId = sessionId;
2719
+ this.#metadata = {
2720
+ toolId: options.toolId,
2721
+ toolName: options.toolName,
2722
+ toolDescription: options.toolDescription
2723
+ };
2724
+ this.#logger = logger.child({
2725
+ runnerId: this.#runnerId,
2726
+ sessionId: this.#sessionId,
2727
+ component: "Tool"
2728
+ });
2729
+ this.#io = new IO({
2730
+ webSocketClient: options.webSocketClient,
2731
+ signal: this.#abortController.signal,
2732
+ executionContext: { type: "TOOL" }
2733
+ }, this.#logger);
2734
+ this.#block = new Block({ signal: this.#abortController.signal });
2735
+ this.#logger.debug("initialized Tool instance");
2736
+ }
2737
+ async run() {
2738
+ return new Promise(async (resolve, reject) => {
2739
+ if (this.#abortController.signal.aborted) {
2740
+ this.#logger.info("tool execution aborted before start");
2741
+ return Promise.reject(/* @__PURE__ */ new Error("tool execution aborted"));
2742
+ }
2743
+ this.#abortController.signal.addEventListener("abort", () => {
2744
+ this.#logger.info("tool execution aborted");
2745
+ reject(/* @__PURE__ */ new Error("Tool execution aborted"));
2746
+ });
2747
+ try {
2748
+ this.#logger.info("running tool handler...");
2749
+ const output = await this.#handler({
2750
+ io: this.#io,
2751
+ signal: this.#abortController.signal,
2752
+ block: this.#block,
2753
+ metadata: {
2754
+ ...this.#metadata,
2755
+ sessionId: this.#sessionId,
2756
+ runnerId: this.#runnerId
2757
+ }
2758
+ });
2759
+ this.#logger.info("tool handler completed successfully");
2760
+ this.#io[STOP_SYMBOL]();
2761
+ resolve(output);
2762
+ } catch (error) {
2763
+ this.#logger.error(error, "error running tool handler");
2764
+ return reject(error);
2765
+ }
2766
+ });
2767
+ }
2768
+ stop() {
2769
+ this.#logger.info("stopping Tool...");
2770
+ this.#abortController.abort();
2771
+ this.#logger.info("Tool stopped");
2772
+ }
2773
+ };
2774
+
2680
2775
  //#endregion
2681
2776
  //#region src/internal/workflow-builder.ts
2682
2777
  var WorkflowBuilder = class {
@@ -2827,7 +2922,9 @@ var Agent = class {
2827
2922
  #logger;
2828
2923
  #webSocketClient;
2829
2924
  #abortController = new AbortController();
2830
- #metadata;
2925
+ #runnerId;
2926
+ #sessionId;
2927
+ #agentId;
2831
2928
  #stepDefinitions;
2832
2929
  #bootstrap;
2833
2930
  #workflowBuilder;
@@ -2838,13 +2935,20 @@ var Agent = class {
2838
2935
  #contextSchema;
2839
2936
  #context;
2840
2937
  constructor(definition, options, logger) {
2841
- this.#metadata = options.metadata;
2938
+ const runnerId$1 = runnerIdStore.getStore();
2939
+ if (!runnerId$1) throw new Error("runnerId not found");
2940
+ this.#runnerId = runnerId$1;
2941
+ const sessionId = sessionIdStore.getStore();
2942
+ if (!sessionId) throw new Error("sessionId not found");
2943
+ this.#sessionId = sessionId;
2944
+ this.#agentId = options.agentId;
2842
2945
  this.#stepDefinitions = definition.steps;
2843
2946
  this.#bootstrap = definition.bootstrap;
2844
2947
  this.#contextSchema = definition.contextSchema;
2845
2948
  this.#webSocketClient = options.webSocketClient;
2846
2949
  this.#logger = logger.child({
2847
- sessionId: this.#metadata.sessionId,
2950
+ runnerId: this.#runnerId,
2951
+ sessionId: this.#sessionId,
2848
2952
  component: "AGENT"
2849
2953
  });
2850
2954
  this.#workflowBuilder = new WorkflowBuilder(this.#logger);
@@ -2885,7 +2989,7 @@ var Agent = class {
2885
2989
  timestamp: Date.now(),
2886
2990
  type: "AGENT_START",
2887
2991
  data: {
2888
- sessionId: this.#metadata.sessionId,
2992
+ sessionId: this.#sessionId,
2889
2993
  agentWorkflow: this.#agentWorkflow,
2890
2994
  executionContext: { type: "AGENT" }
2891
2995
  }
@@ -2942,8 +3046,6 @@ var Agent = class {
2942
3046
  const io = new IO({
2943
3047
  webSocketClient: this.#webSocketClient,
2944
3048
  executionContext,
2945
- runnerId: this.#metadata.runnerId,
2946
- sessionId: this.#metadata.sessionId,
2947
3049
  signal: this.#abortController.signal
2948
3050
  }, this.#logger);
2949
3051
  const block = new Block({ signal: this.#abortController.signal });
@@ -2953,7 +3055,7 @@ var Agent = class {
2953
3055
  timestamp: Date.now(),
2954
3056
  type: "STEP_START",
2955
3057
  data: {
2956
- sessionId: this.#metadata.sessionId,
3058
+ sessionId: this.#sessionId,
2957
3059
  executionContext
2958
3060
  }
2959
3061
  });
@@ -2964,9 +3066,9 @@ var Agent = class {
2964
3066
  context: this.#context,
2965
3067
  updateContext: this.#updateContext.bind(this),
2966
3068
  metadata: {
2967
- agentId: this.#metadata.agentId,
2968
- runnerId: this.#metadata.runnerId,
2969
- sessionId: this.#metadata.sessionId,
3069
+ agentId: this.#agentId,
3070
+ runnerId: this.#runnerId,
3071
+ sessionId: this.#sessionId,
2970
3072
  stepName: stepDefinition.name,
2971
3073
  stepDescription: stepDefinition.description
2972
3074
  },
@@ -2982,7 +3084,7 @@ var Agent = class {
2982
3084
  type: "STEP_COMPLETE",
2983
3085
  data: {
2984
3086
  output,
2985
- sessionId: this.#metadata.sessionId,
3087
+ sessionId: this.#sessionId,
2986
3088
  executionContext
2987
3089
  }
2988
3090
  });
@@ -2998,7 +3100,7 @@ var Agent = class {
2998
3100
  stack: error instanceof Error ? error.stack : void 0,
2999
3101
  code: error instanceof Error && "code" in error ? String(error.code) : void 0
3000
3102
  },
3001
- sessionId: this.#metadata.sessionId
3103
+ sessionId: this.#sessionId
3002
3104
  }
3003
3105
  });
3004
3106
  this.#logger.error({ error }, "error executing step");
@@ -3018,8 +3120,6 @@ var Agent = class {
3018
3120
  const io = new IO({
3019
3121
  webSocketClient: this.#webSocketClient,
3020
3122
  executionContext,
3021
- runnerId: this.#metadata.runnerId,
3022
- sessionId: this.#metadata.sessionId,
3023
3123
  signal: this.#abortController.signal
3024
3124
  }, this.#logger);
3025
3125
  const block = new Block({ signal: this.#abortController.signal });
@@ -3029,7 +3129,7 @@ var Agent = class {
3029
3129
  timestamp: Date.now(),
3030
3130
  type: "STEP_START",
3031
3131
  data: {
3032
- sessionId: this.#metadata.sessionId,
3132
+ sessionId: this.#sessionId,
3033
3133
  executionContext
3034
3134
  }
3035
3135
  });
@@ -3044,7 +3144,7 @@ var Agent = class {
3044
3144
  type: "STEP_COMPLETE",
3045
3145
  data: {
3046
3146
  output: "Bootstrap completed successfully",
3047
- sessionId: this.#metadata.sessionId,
3147
+ sessionId: this.#sessionId,
3048
3148
  executionContext
3049
3149
  }
3050
3150
  });
@@ -3061,7 +3161,7 @@ var Agent = class {
3061
3161
  stack: error instanceof Error ? error.stack : void 0,
3062
3162
  code: error instanceof Error && "code" in error ? String(error.code) : void 0
3063
3163
  },
3064
- sessionId: this.#metadata.sessionId
3164
+ sessionId: this.#sessionId
3065
3165
  }
3066
3166
  });
3067
3167
  throw error;
@@ -3081,71 +3181,4 @@ var Agent = class {
3081
3181
  };
3082
3182
 
3083
3183
  //#endregion
3084
- //#region src/internal/tool.ts
3085
- const TOOL_HANDLER_TAG_NAME = Symbol("TOOL_HANDLER");
3086
- const TOOL_TAG_NAME = Symbol("TOOL");
3087
- /**
3088
- * Tool class is responsible for running the tool's logic.
3089
- * It manages the tool lifecycle, communication with the worker, and handles sending messages back to the ForgeRunner.
3090
- * It uses the IO component to facilitate input/output operations for the tool.
3091
- */
3092
- var Tool = class {
3093
- #metadata;
3094
- #abortController = new AbortController();
3095
- #handler;
3096
- #io;
3097
- #block;
3098
- #logger;
3099
- constructor(options, logger) {
3100
- this.#metadata = options.metadata;
3101
- this.#handler = options.handler;
3102
- this.#logger = logger.child({
3103
- sessionId: this.#metadata.sessionId,
3104
- component: "TOOL"
3105
- });
3106
- this.#io = new IO({
3107
- sessionId: this.#metadata.sessionId,
3108
- runnerId: this.#metadata.runnerId,
3109
- webSocketClient: options.webSocketClient,
3110
- signal: this.#abortController.signal,
3111
- executionContext: { type: "TOOL" }
3112
- }, this.#logger);
3113
- this.#block = new Block({ signal: this.#abortController.signal });
3114
- this.#logger.debug("initialized Tool instance");
3115
- }
3116
- async run() {
3117
- return new Promise(async (resolve, reject) => {
3118
- if (this.#abortController.signal.aborted) {
3119
- this.#logger.info("tool execution aborted before start");
3120
- return Promise.reject(/* @__PURE__ */ new Error("tool execution aborted"));
3121
- }
3122
- this.#abortController.signal.addEventListener("abort", () => {
3123
- this.#logger.info("tool execution aborted");
3124
- reject(/* @__PURE__ */ new Error("Tool execution aborted"));
3125
- });
3126
- try {
3127
- this.#logger.info("running tool handler...");
3128
- const output = await this.#handler({
3129
- io: this.#io,
3130
- signal: this.#abortController.signal,
3131
- block: this.#block,
3132
- metadata: this.#metadata
3133
- });
3134
- this.#logger.info("tool handler completed successfully");
3135
- this.#io[STOP_SYMBOL]();
3136
- resolve(output);
3137
- } catch (error) {
3138
- this.#logger.error(error, "error running tool handler");
3139
- return reject(error);
3140
- }
3141
- });
3142
- }
3143
- stop() {
3144
- this.#logger.info("stopping Tool...");
3145
- this.#abortController.abort();
3146
- this.#logger.info("Tool stopped");
3147
- }
3148
- };
3149
-
3150
- //#endregion
3151
- export { __toESM as C, __require as S, stopToolMessageSchema as _, AGENT_TAG_NAME as a, invariant as b, runWithRetries as c, heartbeatAckMessageSchema as d, initCommunicationMessageSchema as f, stopAgentMessageSchema as g, startToolMessageSchema as h, AGENT_STEP_TAG_NAME as i, ackMessageSchema as l, startAgentMessageSchema as m, TOOL_TAG_NAME as n, Agent as o, runnerId as p, Tool as r, exponentialBackoff as s, TOOL_HANDLER_TAG_NAME as t, baseMessageSchema as u, convertToWords as v, __commonJS as x, getErrorMessage as y };
3184
+ export { __require as C, __commonJS as S, initCommunicationMessageSchema as _, TOOL_TAG_NAME as a, stopAgentMessageSchema as b, sessionIdStore as c, invariant as d, exponentialBackoff as f, heartbeatAckMessageSchema as g, baseMessageSchema as h, TOOL_HANDLER_TAG_NAME as i, convertToWords as l, ackMessageSchema as m, AGENT_TAG_NAME as n, Tool as o, runWithRetries as p, Agent as r, runnerIdStore as s, AGENT_STEP_TAG_NAME as t, getErrorMessage as u, startAgentMessageSchema as v, __toESM as w, stopToolMessageSchema as x, startToolMessageSchema as y };