pulse-coder-engine 0.0.1-alpha.10 → 0.0.1-alpha.11

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.
@@ -504,25 +504,6 @@ var BuiltInPlanModeService = class {
504
504
  }
505
505
  return lines.join("\n");
506
506
  }
507
- applyHooks(baseHooks) {
508
- return {
509
- onBeforeToolCall: async (name, input) => {
510
- this.observePotentialPolicyViolation(name, input);
511
- if (baseHooks?.onBeforeToolCall) {
512
- const nextInput = await baseHooks.onBeforeToolCall(name, input);
513
- return nextInput ?? input;
514
- }
515
- return input;
516
- },
517
- onAfterToolCall: async (name, input, output) => {
518
- if (baseHooks?.onAfterToolCall) {
519
- const nextOutput = await baseHooks.onAfterToolCall(name, input, output);
520
- return nextOutput ?? output;
521
- }
522
- return output;
523
- }
524
- };
525
- }
526
507
  getEvents(limit = 50) {
527
508
  return this.events.slice(-Math.max(0, limit));
528
509
  }
@@ -659,21 +640,18 @@ var builtInPlanModePlugin = {
659
640
  version: "1.0.0",
660
641
  async initialize(context) {
661
642
  const service = new BuiltInPlanModeService(context.logger, context.events, "executing");
662
- context.registerRunHook("plan-mode", ({ context: runContext, tools, systemPrompt, hooks }) => {
643
+ context.registerHook("beforeLLMCall", ({ context: runContext, tools, systemPrompt }) => {
663
644
  const mode = service.getMode();
664
645
  if (mode === "executing") {
665
- return {
666
- systemPrompt,
667
- hooks
668
- };
646
+ return;
669
647
  }
670
648
  const transition = service.processContextMessages(runContext.messages);
671
649
  const append = service.buildPromptAppend(Object.keys(tools), transition);
672
650
  const finalSystemPrompt = appendSystemPrompt(systemPrompt, append);
673
- return {
674
- systemPrompt: finalSystemPrompt,
675
- hooks: service.applyHooks(hooks)
676
- };
651
+ return { systemPrompt: finalSystemPrompt };
652
+ });
653
+ context.registerHook("beforeToolCall", ({ name, input }) => {
654
+ service.observePotentialPolicyViolation(name, input);
677
655
  });
678
656
  context.registerService("planMode", service);
679
657
  context.registerService("planModeService", service);
@@ -1047,15 +1025,28 @@ var maybeCompactContext = async (context, options) => {
1047
1025
  };
1048
1026
 
1049
1027
  // src/core/loop.ts
1050
- function applyToolHooks(tools, hooks) {
1028
+ function wrapToolsWithHooks(tools, beforeHooks, afterHooks) {
1051
1029
  const wrapped = {};
1052
1030
  for (const [name, t] of Object.entries(tools)) {
1053
1031
  wrapped[name] = {
1054
1032
  ...t,
1055
1033
  execute: async (input, ctx) => {
1056
- const finalInput = hooks.onBeforeToolCall ? await hooks.onBeforeToolCall(name, input) ?? input : input;
1034
+ let finalInput = input;
1035
+ for (const hook of beforeHooks) {
1036
+ const result = await hook({ name, input: finalInput });
1037
+ if (result && "input" in result) {
1038
+ finalInput = result.input;
1039
+ }
1040
+ }
1057
1041
  const output = await t.execute(finalInput, ctx);
1058
- return hooks.onAfterToolCall ? await hooks.onAfterToolCall(name, finalInput, output) ?? output : output;
1042
+ let finalOutput = output;
1043
+ for (const hook of afterHooks) {
1044
+ const result = await hook({ name, input: finalInput, output: finalOutput });
1045
+ if (result && "output" in result) {
1046
+ finalOutput = result.output;
1047
+ }
1048
+ }
1049
+ return finalOutput;
1059
1050
  }
1060
1051
  };
1061
1052
  }
@@ -1065,6 +1056,7 @@ async function loop(context, options) {
1065
1056
  let errorCount = 0;
1066
1057
  let totalSteps = 0;
1067
1058
  let compactionAttempts = 0;
1059
+ const loopHooks = options?.hooks ?? {};
1068
1060
  while (true) {
1069
1061
  try {
1070
1062
  if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
@@ -1081,8 +1073,24 @@ async function loop(context, options) {
1081
1073
  }
1082
1074
  }
1083
1075
  let tools = options?.tools || {};
1084
- if (options?.hooks) {
1085
- tools = applyToolHooks(tools, options.hooks);
1076
+ let systemPrompt = options?.systemPrompt;
1077
+ if (loopHooks.beforeLLMCall?.length) {
1078
+ for (const hook of loopHooks.beforeLLMCall) {
1079
+ const result2 = await hook({ context, systemPrompt, tools });
1080
+ if (result2) {
1081
+ if ("systemPrompt" in result2 && result2.systemPrompt !== void 0) {
1082
+ systemPrompt = result2.systemPrompt;
1083
+ }
1084
+ if ("tools" in result2 && result2.tools !== void 0) {
1085
+ tools = result2.tools;
1086
+ }
1087
+ }
1088
+ }
1089
+ }
1090
+ const beforeToolHooks = loopHooks.beforeToolCall ?? [];
1091
+ const afterToolHooks = loopHooks.afterToolCall ?? [];
1092
+ if (beforeToolHooks.length || afterToolHooks.length) {
1093
+ tools = wrapToolsWithHooks(tools, beforeToolHooks, afterToolHooks);
1086
1094
  }
1087
1095
  const toolExecutionContext = {
1088
1096
  onClarificationRequest: options?.onClarificationRequest,
@@ -1093,7 +1101,7 @@ async function loop(context, options) {
1093
1101
  toolExecutionContext,
1094
1102
  provider: options?.provider,
1095
1103
  model: options?.model,
1096
- systemPrompt: options?.systemPrompt,
1104
+ systemPrompt,
1097
1105
  onStepFinish: (step) => {
1098
1106
  options?.onStepFinish?.(step);
1099
1107
  },
@@ -1121,6 +1129,11 @@ async function loop(context, options) {
1121
1129
  options?.onResponse?.(messages);
1122
1130
  }
1123
1131
  }
1132
+ if (loopHooks.afterLLMCall?.length) {
1133
+ for (const hook of loopHooks.afterLLMCall) {
1134
+ await hook({ context, finishReason, text });
1135
+ }
1136
+ }
1124
1137
  if (finishReason === "stop") {
1125
1138
  return text || "Task completed.";
1126
1139
  }