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.
@@ -461,25 +461,6 @@ var BuiltInPlanModeService = class {
461
461
  }
462
462
  return lines.join("\n");
463
463
  }
464
- applyHooks(baseHooks) {
465
- return {
466
- onBeforeToolCall: async (name, input) => {
467
- this.observePotentialPolicyViolation(name, input);
468
- if (baseHooks?.onBeforeToolCall) {
469
- const nextInput = await baseHooks.onBeforeToolCall(name, input);
470
- return nextInput ?? input;
471
- }
472
- return input;
473
- },
474
- onAfterToolCall: async (name, input, output) => {
475
- if (baseHooks?.onAfterToolCall) {
476
- const nextOutput = await baseHooks.onAfterToolCall(name, input, output);
477
- return nextOutput ?? output;
478
- }
479
- return output;
480
- }
481
- };
482
- }
483
464
  getEvents(limit = 50) {
484
465
  return this.events.slice(-Math.max(0, limit));
485
466
  }
@@ -616,21 +597,18 @@ var builtInPlanModePlugin = {
616
597
  version: "1.0.0",
617
598
  async initialize(context) {
618
599
  const service = new BuiltInPlanModeService(context.logger, context.events, "executing");
619
- context.registerRunHook("plan-mode", ({ context: runContext, tools, systemPrompt, hooks }) => {
600
+ context.registerHook("beforeLLMCall", ({ context: runContext, tools, systemPrompt }) => {
620
601
  const mode = service.getMode();
621
602
  if (mode === "executing") {
622
- return {
623
- systemPrompt,
624
- hooks
625
- };
603
+ return;
626
604
  }
627
605
  const transition = service.processContextMessages(runContext.messages);
628
606
  const append = service.buildPromptAppend(Object.keys(tools), transition);
629
607
  const finalSystemPrompt = appendSystemPrompt(systemPrompt, append);
630
- return {
631
- systemPrompt: finalSystemPrompt,
632
- hooks: service.applyHooks(hooks)
633
- };
608
+ return { systemPrompt: finalSystemPrompt };
609
+ });
610
+ context.registerHook("beforeToolCall", ({ name, input }) => {
611
+ service.observePotentialPolicyViolation(name, input);
634
612
  });
635
613
  context.registerService("planMode", service);
636
614
  context.registerService("planModeService", service);
@@ -1004,15 +982,28 @@ var maybeCompactContext = async (context, options) => {
1004
982
  };
1005
983
 
1006
984
  // src/core/loop.ts
1007
- function applyToolHooks(tools, hooks) {
985
+ function wrapToolsWithHooks(tools, beforeHooks, afterHooks) {
1008
986
  const wrapped = {};
1009
987
  for (const [name, t] of Object.entries(tools)) {
1010
988
  wrapped[name] = {
1011
989
  ...t,
1012
990
  execute: async (input, ctx) => {
1013
- const finalInput = hooks.onBeforeToolCall ? await hooks.onBeforeToolCall(name, input) ?? input : input;
991
+ let finalInput = input;
992
+ for (const hook of beforeHooks) {
993
+ const result = await hook({ name, input: finalInput });
994
+ if (result && "input" in result) {
995
+ finalInput = result.input;
996
+ }
997
+ }
1014
998
  const output = await t.execute(finalInput, ctx);
1015
- return hooks.onAfterToolCall ? await hooks.onAfterToolCall(name, finalInput, output) ?? output : output;
999
+ let finalOutput = output;
1000
+ for (const hook of afterHooks) {
1001
+ const result = await hook({ name, input: finalInput, output: finalOutput });
1002
+ if (result && "output" in result) {
1003
+ finalOutput = result.output;
1004
+ }
1005
+ }
1006
+ return finalOutput;
1016
1007
  }
1017
1008
  };
1018
1009
  }
@@ -1022,6 +1013,7 @@ async function loop(context, options) {
1022
1013
  let errorCount = 0;
1023
1014
  let totalSteps = 0;
1024
1015
  let compactionAttempts = 0;
1016
+ const loopHooks = options?.hooks ?? {};
1025
1017
  while (true) {
1026
1018
  try {
1027
1019
  if (compactionAttempts < MAX_COMPACTION_ATTEMPTS) {
@@ -1038,8 +1030,24 @@ async function loop(context, options) {
1038
1030
  }
1039
1031
  }
1040
1032
  let tools = options?.tools || {};
1041
- if (options?.hooks) {
1042
- tools = applyToolHooks(tools, options.hooks);
1033
+ let systemPrompt = options?.systemPrompt;
1034
+ if (loopHooks.beforeLLMCall?.length) {
1035
+ for (const hook of loopHooks.beforeLLMCall) {
1036
+ const result2 = await hook({ context, systemPrompt, tools });
1037
+ if (result2) {
1038
+ if ("systemPrompt" in result2 && result2.systemPrompt !== void 0) {
1039
+ systemPrompt = result2.systemPrompt;
1040
+ }
1041
+ if ("tools" in result2 && result2.tools !== void 0) {
1042
+ tools = result2.tools;
1043
+ }
1044
+ }
1045
+ }
1046
+ }
1047
+ const beforeToolHooks = loopHooks.beforeToolCall ?? [];
1048
+ const afterToolHooks = loopHooks.afterToolCall ?? [];
1049
+ if (beforeToolHooks.length || afterToolHooks.length) {
1050
+ tools = wrapToolsWithHooks(tools, beforeToolHooks, afterToolHooks);
1043
1051
  }
1044
1052
  const toolExecutionContext = {
1045
1053
  onClarificationRequest: options?.onClarificationRequest,
@@ -1050,7 +1058,7 @@ async function loop(context, options) {
1050
1058
  toolExecutionContext,
1051
1059
  provider: options?.provider,
1052
1060
  model: options?.model,
1053
- systemPrompt: options?.systemPrompt,
1061
+ systemPrompt,
1054
1062
  onStepFinish: (step) => {
1055
1063
  options?.onStepFinish?.(step);
1056
1064
  },
@@ -1078,6 +1086,11 @@ async function loop(context, options) {
1078
1086
  options?.onResponse?.(messages);
1079
1087
  }
1080
1088
  }
1089
+ if (loopHooks.afterLLMCall?.length) {
1090
+ for (const hook of loopHooks.afterLLMCall) {
1091
+ await hook({ context, finishReason, text });
1092
+ }
1093
+ }
1081
1094
  if (finishReason === "stop") {
1082
1095
  return text || "Task completed.";
1083
1096
  }