@leviyuan/lodestar 0.21.7 → 0.21.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.
@@ -35,6 +35,8 @@ function apply(ctx) {
35
35
  let turnFailure;
36
36
  let settledSeq = -1;
37
37
  let settling;
38
+ let inputGeneration = 0;
39
+ const steeringInputs = new Map;
38
40
  const questions = new Map;
39
41
  const notify = (method, params) => {
40
42
  process.stdout.write(JSON.stringify({ jsonrpc: "2.0", method, params }) + `
@@ -68,8 +70,13 @@ function apply(ctx) {
68
70
  if (settling || !lastEnd || !root || root.agent.status !== "idle" || lastEnd.seq <= settledSeq)
69
71
  return;
70
72
  const end = lastEnd;
73
+ for (const messageId of steeringInputs.keys())
74
+ root.agent.inbox.remove(messageId);
75
+ steeringInputs.clear();
71
76
  settling = ctx.sessionPersistence.flush().then(() => {
72
77
  settledSeq = end.seq;
78
+ if (root?.agent.status !== "idle" || lastEnd !== end || activeTurn !== end.data.turn)
79
+ return;
73
80
  notify("settled", { sessionId: rootId, event: end });
74
81
  }).catch((error) => {
75
82
  settledSeq = end.seq;
@@ -80,7 +87,10 @@ function apply(ctx) {
80
87
  });
81
88
  };
82
89
  ctx.on("session/event", (session, event) => {
83
- notify("session.event", { sessionId: session.id, event });
90
+ const inputId = session.id === rootId && event.type === "user/message" ? steeringInputs.get(event.data.id) : undefined;
91
+ if (inputId && event.type === "user/message")
92
+ steeringInputs.delete(event.data.id);
93
+ notify("session.event", { sessionId: session.id, event, ...inputId ? { inputId } : {} });
84
94
  if (session.id === rootId && event.type === "turn/start") {
85
95
  turnRoute = selected;
86
96
  if (root)
@@ -312,27 +322,49 @@ function apply(ctx) {
312
322
  }
313
323
  case "session/prompt": {
314
324
  const agent = live();
325
+ const generation = inputGeneration;
326
+ const isSteering = params.mode === "steer";
327
+ if (isSteering && (typeof params.inputId !== "string" || !params.inputId || typeof params.expectedTurnId !== "string"))
328
+ throw new Error("invalid DSH steering input identity");
329
+ const acceptsSteering = () => agent.status === "running" && String(activeTurn) === params.expectedTurnId && lastEnd?.data.turn !== activeTurn;
330
+ if (isSteering && !acceptsSteering())
331
+ return { accepted: false };
315
332
  if (!Array.isArray(params.content) || !params.content.length)
316
333
  throw new Error("empty DSH prompt");
317
334
  if (params.content.some((part) => part.type === "image")) {
318
- const routeInfo = await ctx.llm.resolveModelInfo(PROVIDER, selected.model);
335
+ const model = (isSteering ? turnRoute : selected).model;
336
+ const routeInfo = await ctx.llm.resolveModelInfo(PROVIDER, model);
319
337
  if (!routeInfo.inputModalities?.includes("image"))
320
- throw new Error(`DSH model ${selected.model} does not accept images`);
338
+ throw new Error(`DSH model ${model} does not accept images`);
321
339
  }
322
340
  const content = await ctx.attachments.admitPromptContent(params.content);
341
+ if (closing || generation !== inputGeneration)
342
+ throw new Error("DSH prompt cancelled before submission");
343
+ if (isSteering && !acceptsSteering())
344
+ return { accepted: false };
323
345
  const message = createUserMessage({ content, source: { kind: "user" } });
324
346
  const delivery = params.mode === "auto" ? agent.status === "running" ? "steer" : "queue" : params.mode;
325
- if (delivery === "steer")
326
- agent.steer(message);
327
- else if (delivery === "queue")
347
+ if (delivery === "steer") {
348
+ if (isSteering)
349
+ steeringInputs.set(message.id, params.inputId);
350
+ try {
351
+ agent.steer(message);
352
+ } catch (error) {
353
+ steeringInputs.delete(message.id);
354
+ throw error;
355
+ }
356
+ } else if (delivery === "queue")
328
357
  agent.followup(message);
329
358
  else
330
359
  throw new Error("invalid DSH input mode");
331
- return { messageId: message.id };
360
+ return { messageId: message.id, ...isSteering ? { accepted: true, inputId: params.inputId } : {} };
332
361
  }
333
- case "session/cancel":
334
- live().cancel(Object.freeze({ kind: "user" }), { keepInbox: true });
362
+ case "session/cancel": {
363
+ inputGeneration++;
364
+ steeringInputs.clear();
365
+ live().cancel(Object.freeze({ kind: "user" }));
335
366
  return {};
367
+ }
336
368
  case "question/answer": {
337
369
  const pending = questions.get(params.id);
338
370
  if (!pending)
@@ -354,6 +386,8 @@ function apply(ctx) {
354
386
  }
355
387
  case "shutdown": {
356
388
  closing = true;
389
+ inputGeneration++;
390
+ steeringInputs.clear();
357
391
  lifecycle.abort(Object.freeze({ kind: "disposed" }));
358
392
  if (opening) {
359
393
  try {