@lichao0223/lobehub-cli 0.0.82 → 0.0.84

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/dist/index.js CHANGED
@@ -9790,8 +9790,7 @@ var init_fetch = __esmMin((() => {
9790
9790
  WorkspaceFrozenByAdmin: "WorkspaceFrozenByAdmin",
9791
9791
  WorkspaceFrozenByRiskControl: "WorkspaceFrozenByRiskControl",
9792
9792
  WorkspaceSubscriptionInactive: "WorkspaceSubscriptionInactive",
9793
- ShareTurnLimitExceeded: "ShareTurnLimitExceeded",
9794
- ShareTopicLimitExceeded: "ShareTopicLimitExceeded",
9793
+ ShareMessageLimitExceeded: "ShareMessageLimitExceeded",
9795
9794
  ShareSpendLimitExceeded: "ShareSpendLimitExceeded",
9796
9795
  ShareHeterogeneousAgentUnsupported: "ShareHeterogeneousAgentUnsupported",
9797
9796
  ShareHeteroDeviceUnavailable: "ShareHeteroDeviceUnavailable",
@@ -62780,16 +62779,20 @@ var CodexAppServerAdapter = class {
62780
62779
  started = false;
62781
62780
  /** A steered message was read; the next visible output opens a new step. */
62782
62781
  steerStepPending = false;
62783
- stepIndex = 0;
62782
+ stepIndex;
62784
62783
  terminal = false;
62785
62784
  constructor(options = {}) {
62786
62785
  this.currentModel = options.initialModel;
62787
62786
  this.lastCumulativeUsage = options.initialCumulativeUsage;
62788
62787
  this.sessionId = options.sessionId;
62788
+ this.stepIndex = options.initialStepIndex ?? 0;
62789
62789
  }
62790
62790
  get cumulativeUsage() {
62791
62791
  return this.latestCumulativeUsage ?? this.lastCumulativeUsage;
62792
62792
  }
62793
+ get nextStepIndex() {
62794
+ return this.stepIndex + 1;
62795
+ }
62793
62796
  startUserInput(toolCallId, args) {
62794
62797
  const payload = {
62795
62798
  apiName: "askUserQuestion",
@@ -62855,7 +62858,10 @@ var CodexAppServerAdapter = class {
62855
62858
  if (this.started) return [];
62856
62859
  this.started = true;
62857
62860
  this.currentTurnId = params.turn.id;
62858
- return [this.makeEvent("stream_start", this.streamStartData({ messageId: params.turn.id }))];
62861
+ return [this.makeEvent("stream_start", this.streamStartData({
62862
+ messageId: params.turn.id,
62863
+ newStep: this.stepIndex > 0 || void 0
62864
+ }))];
62859
62865
  }
62860
62866
  handleItemStarted({ item }) {
62861
62867
  if (item.type === "userMessage") return this.handleSteeredUserMessage(item);
@@ -63233,6 +63239,61 @@ const codexUserInputResponse = (request, answer) => {
63233
63239
  return { answers: Object.fromEntries(entries) };
63234
63240
  };
63235
63241
 
63242
+ //#endregion
63243
+ //#region ../../packages/heterogeneous-agents/src/codex/asyncUserInput.ts
63244
+ init_zod$1();
63245
+ const nonBlank = string().refine((value) => value.trim().length > 0);
63246
+ const notificationSchema = object$1({ item: object$1({
63247
+ delivery: literal("async"),
63248
+ id: nonBlank,
63249
+ questions: array(object$1({
63250
+ options: array(nonBlank).nullish(),
63251
+ title: nonBlank
63252
+ })).min(1),
63253
+ type: literal("agentMessage")
63254
+ }) });
63255
+ const readCodexAsyncQuestion = (params) => {
63256
+ const item = notificationSchema.safeParse(params).data?.item;
63257
+ if (!item) return;
63258
+ const questionNames = new Set(["__freeform__", "__supplement__"]);
63259
+ const questions = item.questions.map(({ options, title }, index) => {
63260
+ let question = title;
63261
+ for (let suffix = 2; questionNames.has(question); suffix++) question = `${title} (${suffix})`;
63262
+ questionNames.add(question);
63263
+ return {
63264
+ header: "",
63265
+ id: JSON.stringify([
63266
+ "request_user_input_async",
63267
+ item.id,
63268
+ index
63269
+ ]),
63270
+ isSecret: false,
63271
+ options: [...new Set(options)].map((label) => ({
63272
+ description: "",
63273
+ label
63274
+ })),
63275
+ question,
63276
+ title
63277
+ };
63278
+ });
63279
+ return {
63280
+ itemId: item.id,
63281
+ questions
63282
+ };
63283
+ };
63284
+ const codexAsyncAnswers = (request, answer) => {
63285
+ const { answers } = codexUserInputResponse(request, answer);
63286
+ return request.questions.flatMap(({ id, title }) => {
63287
+ const response = answers[id]?.answers.join("\n");
63288
+ return response ? [{
63289
+ answer: response,
63290
+ question: title,
63291
+ questionItemId: id
63292
+ }] : [];
63293
+ });
63294
+ };
63295
+ const formatCodexAsyncReplies = (replies) => `<send_user_message_question_reply>\n${JSON.stringify(replies)}\n</send_user_message_question_reply>`;
63296
+
63236
63297
  //#endregion
63237
63298
  //#region ../../packages/heterogeneous-agents/src/codex/CodexThreadSession.ts
63238
63299
  init_object$1();
@@ -63302,49 +63363,14 @@ var CodexThreadSession = class {
63302
63363
  const threadId = this.threadId;
63303
63364
  if (!threadId) throw new Error("Codex thread is not attached");
63304
63365
  traceUnsubscribers.push(this.options.client.onRawMessage(threadId, options.onRawMessage));
63305
- const adapter = new CodexAppServerAdapter({
63306
- initialCumulativeUsage: this.cumulativeUsage,
63307
- initialModel: this.model,
63308
- sessionId: this.options.threadParams.ephemeral ? void 0 : threadId
63309
- });
63310
- let resolveTurn;
63311
- const completion = new Promise((resolve) => {
63312
- resolveTurn = resolve;
63313
- });
63314
- const activeTurn = {
63315
- adapter,
63316
- askUserBridge: options.askUserBridge,
63317
- completion,
63318
- interruptRequested: this.interruptRequested,
63319
- notificationQueue: Promise.resolve(),
63320
- operationId: options.operationId,
63321
- resolve: resolveTurn,
63322
- terminalNotificationReceived: false,
63323
- transportInterrupted: false
63324
- };
63325
- this.activeTurn = activeTurn;
63326
- const turnParams = {
63327
- input: options.input,
63328
- threadId
63329
- };
63330
- this.canFallback = false;
63331
- activeTurn.turnId = (await this.options.client.request("turn/start", turnParams))?.turn?.id;
63332
- if (!activeTurn.turnId) throw new Error("Codex app-server returned no turn id");
63333
- if (activeTurn.interruptRequested || this.closedByHost) await this.requestInterrupt(activeTurn);
63334
- if (this.closedByHost) return;
63335
- this.emitStatus("running", options.operationId);
63336
- await activeTurn.completion;
63337
- await activeTurn.notificationQueue;
63338
- if (this.closedByHost) return;
63339
- if (activeTurn.notificationError) throw activeTurn.notificationError;
63340
- if (activeTurn.transportInterrupted) {
63341
- this.cumulativeUsage = adapter.cumulativeUsage;
63342
- this.emitStatus("idle", options.operationId);
63343
- return;
63344
- }
63345
- await this.emitEvents(adapter.flush(), options.operationId);
63346
- this.cumulativeUsage = adapter.cumulativeUsage;
63347
- this.emitStatus("idle", options.operationId);
63366
+ let input = options.input;
63367
+ let stepIndex = 0;
63368
+ while (input && !this.closedByHost) {
63369
+ const result = await this.runNativeTurn(options, threadId, input, stepIndex);
63370
+ input = result?.input;
63371
+ stepIndex = result?.stepIndex ?? 0;
63372
+ }
63373
+ if (!this.closedByHost) this.emitStatus("idle", options.operationId);
63348
63374
  } catch (error) {
63349
63375
  if (this.closedByHost) return;
63350
63376
  if (this.activeTurn?.transportInterrupted) {
@@ -63367,6 +63393,57 @@ var CodexThreadSession = class {
63367
63393
  this.running = false;
63368
63394
  }
63369
63395
  }
63396
+ async runNativeTurn(options, threadId, input, stepIndex) {
63397
+ const adapter = new CodexAppServerAdapter({
63398
+ initialCumulativeUsage: this.cumulativeUsage,
63399
+ initialModel: this.model,
63400
+ initialStepIndex: stepIndex,
63401
+ sessionId: this.options.threadParams.ephemeral ? void 0 : threadId
63402
+ });
63403
+ let resolveTurn;
63404
+ const completion = new Promise((resolve) => {
63405
+ resolveTurn = resolve;
63406
+ });
63407
+ const activeTurn = {
63408
+ adapter,
63409
+ askUserBridge: options.askUserBridge,
63410
+ asyncQuestions: /* @__PURE__ */ new Map(),
63411
+ completion,
63412
+ interruptRequested: this.interruptRequested,
63413
+ notificationQueue: Promise.resolve(),
63414
+ operationId: options.operationId,
63415
+ resolve: resolveTurn,
63416
+ synchronousQuestions: /* @__PURE__ */ new Set(),
63417
+ terminalNotificationReceived: false,
63418
+ transportInterrupted: false
63419
+ };
63420
+ this.activeTurn = activeTurn;
63421
+ const turnParams = {
63422
+ input,
63423
+ threadId
63424
+ };
63425
+ this.canFallback = false;
63426
+ activeTurn.turnId = (await this.options.client.request("turn/start", turnParams))?.turn?.id;
63427
+ if (!activeTurn.turnId) throw new Error("Codex app-server returned no turn id");
63428
+ if (activeTurn.interruptRequested || this.closedByHost) await this.requestInterrupt(activeTurn);
63429
+ if (this.closedByHost) return;
63430
+ if (stepIndex === 0) this.emitStatus("running", options.operationId);
63431
+ await activeTurn.completion;
63432
+ await activeTurn.notificationQueue;
63433
+ if (this.closedByHost) return;
63434
+ if (activeTurn.notificationError) throw activeTurn.notificationError;
63435
+ const continuation = activeTurn.deferredCompletion ? await this.finishAsyncQuestions(activeTurn, activeTurn.deferredCompletion) : void 0;
63436
+ if (!activeTurn.transportInterrupted) await this.emitEvents(adapter.flush(), options.operationId);
63437
+ this.cumulativeUsage = adapter.cumulativeUsage;
63438
+ return continuation ? {
63439
+ input: [{
63440
+ text: continuation,
63441
+ text_elements: [],
63442
+ type: "text"
63443
+ }],
63444
+ stepIndex: adapter.nextStepIndex
63445
+ } : void 0;
63446
+ }
63370
63447
  /**
63371
63448
  * Add a user message to the running turn (`turn/steer`). The model reads it
63372
63449
  * at its next step; the app-server reports it back as a `userMessage` item
@@ -63377,7 +63454,7 @@ var CodexThreadSession = class {
63377
63454
  async steer(input, clientUserMessageId) {
63378
63455
  const activeTurn = this.activeTurn;
63379
63456
  const threadId = this.threadId;
63380
- if (!activeTurn?.turnId || !threadId || activeTurn.interruptRequested || this.closedByHost) return "rejected";
63457
+ if (!activeTurn?.turnId || !threadId || activeTurn.terminalNotificationReceived || activeTurn.interruptRequested || this.closedByHost) return "rejected";
63381
63458
  try {
63382
63459
  const params = {
63383
63460
  clientUserMessageId,
@@ -63399,7 +63476,7 @@ var CodexThreadSession = class {
63399
63476
  if (!activeTurn) return;
63400
63477
  activeTurn.interruptRequested = true;
63401
63478
  activeTurn.askUserBridge?.cancelAll("session_ended");
63402
- if (!activeTurn.turnId) return;
63479
+ if (!activeTurn.turnId || activeTurn.terminalNotificationReceived) return;
63403
63480
  try {
63404
63481
  await this.requestInterrupt(activeTurn);
63405
63482
  } catch (error) {
@@ -63413,7 +63490,7 @@ var CodexThreadSession = class {
63413
63490
  this.interruptRequested = true;
63414
63491
  if (this.activeTurn) {
63415
63492
  this.activeTurn.interruptRequested = true;
63416
- if (this.activeTurn.turnId) this.requestInterrupt(this.activeTurn).catch((error) => {
63493
+ if (this.activeTurn.turnId && !this.activeTurn.terminalNotificationReceived) this.requestInterrupt(this.activeTurn).catch((error) => {
63417
63494
  console.error("Failed to interrupt Codex turn while closing the session:", error);
63418
63495
  });
63419
63496
  this.interruptActiveTurn();
@@ -63516,10 +63593,11 @@ var CodexThreadSession = class {
63516
63593
  activeTurn.notificationQueue = activeTurn.notificationQueue.then(() => this.emitEvents(activeTurn.adapter.startUserInput(request.itemId, args), activeTurn.operationId));
63517
63594
  await activeTurn.notificationQueue;
63518
63595
  if (activeTurn.interruptRequested || activeTurn.terminalNotificationReceived || activeTurn.transportInterrupted) return { answers: {} };
63596
+ activeTurn.synchronousQuestions.add(request.itemId);
63519
63597
  const answer = await bridge.pending({
63520
63598
  arguments: args,
63521
63599
  toolCallId: request.itemId
63522
- }, request.autoResolutionMs ? { timeoutMs: request.autoResolutionMs } : {});
63600
+ }, request.autoResolutionMs ? { timeoutMs: request.autoResolutionMs } : {}).finally(() => activeTurn.synchronousQuestions.delete(request.itemId));
63523
63601
  activeTurn.notificationQueue = activeTurn.notificationQueue.then(() => this.emitEvents(activeTurn.adapter.completeUserInput(request.itemId, answer), activeTurn.operationId));
63524
63602
  await activeTurn.notificationQueue;
63525
63603
  return codexUserInputResponse(request, answer);
@@ -63527,12 +63605,12 @@ var CodexThreadSession = class {
63527
63605
  handleDisconnect() {
63528
63606
  if (this.closedByHost) return;
63529
63607
  this.attached = false;
63530
- if (this.activeTurn && !this.activeTurn.terminalNotificationReceived) this.interruptActiveTurn();
63608
+ if (this.activeTurn && (!this.activeTurn.terminalNotificationReceived || this.activeTurn.asyncQuestions.size > 0)) this.interruptActiveTurn();
63531
63609
  }
63532
63610
  requestInterrupt(activeTurn) {
63533
63611
  const threadId = this.threadId;
63534
63612
  const turnId = activeTurn.turnId;
63535
- if (!threadId || !turnId) return Promise.resolve();
63613
+ if (!threadId || !turnId || activeTurn.terminalNotificationReceived) return Promise.resolve();
63536
63614
  if (activeTurn.interruptRequest) return activeTurn.interruptRequest;
63537
63615
  const params = {
63538
63616
  threadId,
@@ -63554,11 +63632,21 @@ var CodexThreadSession = class {
63554
63632
  if (method === "turn/completed") {
63555
63633
  if (params.turn.status !== "inProgress") {
63556
63634
  activeTurn.terminalNotificationReceived = true;
63557
- activeTurn.askUserBridge?.cancelAll("session_ended");
63635
+ for (const id of activeTurn.synchronousQuestions) activeTurn.askUserBridge?.cancel(id, "session_ended");
63558
63636
  }
63559
63637
  }
63560
63638
  activeTurn.notificationQueue = activeTurn.notificationQueue.then(async () => {
63561
63639
  if (activeTurn.notificationError) return;
63640
+ if (method === "item/completed" && await this.startAsyncQuestion(activeTurn, params)) return;
63641
+ if (method === "turn/completed") {
63642
+ const notification = params;
63643
+ if (notification.turn.status === "completed" && activeTurn.asyncQuestions.size > 0 && !activeTurn.transportInterrupted) {
63644
+ activeTurn.deferredCompletion = notification;
63645
+ activeTurn.resolve();
63646
+ return;
63647
+ }
63648
+ if (notification.turn.status !== "inProgress") activeTurn.askUserBridge?.cancelAll("session_ended");
63649
+ }
63562
63650
  await this.emitEvents(activeTurn.adapter.adapt(method, params), activeTurn.operationId);
63563
63651
  if (method !== "turn/completed") return;
63564
63652
  const notification = params;
@@ -63570,6 +63658,52 @@ var CodexThreadSession = class {
63570
63658
  });
63571
63659
  return activeTurn.notificationQueue;
63572
63660
  }
63661
+ async startAsyncQuestion(activeTurn, params) {
63662
+ const bridge = activeTurn.askUserBridge;
63663
+ if (!bridge || bridge.operationId !== activeTurn.operationId) return false;
63664
+ const request = readCodexAsyncQuestion(params);
63665
+ if (!request) return false;
63666
+ if (activeTurn.asyncQuestions.has(request.itemId)) return true;
63667
+ if (activeTurn.interruptRequested || activeTurn.transportInterrupted) return false;
63668
+ const { itemId } = request;
63669
+ const args = codexUserInputArguments(request);
63670
+ await this.emitEvents(activeTurn.adapter.startUserInput(itemId, args), activeTurn.operationId);
63671
+ if (activeTurn.interruptRequested || activeTurn.transportInterrupted) return true;
63672
+ const answer = bridge.pending({
63673
+ arguments: args,
63674
+ toolCallId: itemId
63675
+ }).catch((error) => {
63676
+ this.recordNotificationError(activeTurn, error);
63677
+ return { cancelled: true };
63678
+ });
63679
+ activeTurn.asyncQuestions.set(itemId, {
63680
+ answer,
63681
+ request
63682
+ });
63683
+ return true;
63684
+ }
63685
+ async finishAsyncQuestions(activeTurn, notification) {
63686
+ const { adapter, asyncQuestions, operationId } = activeTurn;
63687
+ const answers = await Promise.all([...asyncQuestions.values()].map(async ({ answer, request }) => ({
63688
+ answer: await answer,
63689
+ request
63690
+ })));
63691
+ if (activeTurn.notificationError) throw activeTurn.notificationError;
63692
+ if (this.closedByHost || activeTurn.transportInterrupted) {
63693
+ await activeTurn.notificationQueue;
63694
+ return;
63695
+ }
63696
+ const replies = [];
63697
+ for (const { answer, request } of answers) {
63698
+ await this.emitEvents(adapter.completeUserInput(request.itemId, answer), operationId);
63699
+ replies.push(...codexAsyncAnswers(request, answer));
63700
+ }
63701
+ const turnEnded = ({ type }) => type === "agent_runtime_end" || type === "visible_output_end";
63702
+ const events = adapter.adapt("turn/completed", notification);
63703
+ await this.emitEvents(events.filter((event) => !turnEnded(event)), operationId);
63704
+ if (!activeTurn.interruptRequested && !activeTurn.transportInterrupted && replies.length > 0) return formatCodexAsyncReplies(replies);
63705
+ await this.emitEvents(events.filter(turnEnded), operationId);
63706
+ }
63573
63707
  isCurrentTurnNotification(method, params, activeTurn) {
63574
63708
  if (!isRecord$7(params)) return false;
63575
63709
  const notificationTurnId = method === "turn/started" || method === "turn/completed" ? isRecord$7(params.turn) ? pickString$1(params.turn.id) : void 0 : pickString$1(params.turnId);
package/man/man1/lh.1 CHANGED
@@ -1,6 +1,6 @@
1
1
  .\" Code generated by `npm run man:generate`; DO NOT EDIT.
2
2
  .\" Manual command details come from the Commander command tree.
3
- .TH LH 1 "" "@lichao0223/lobehub\-cli 0.0.82" "User Commands"
3
+ .TH LH 1 "" "@lichao0223/lobehub\-cli 0.0.84" "User Commands"
4
4
  .SH NAME
5
5
  lh \- LobeHub CLI \- manage and connect to LobeHub services
6
6
  .SH SYNOPSIS
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lichao0223/lobehub-cli",
3
- "version": "0.0.82",
3
+ "version": "0.0.84",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/lichao0223/lobehub",
@@ -83,5 +83,5 @@
83
83
  "access": "public",
84
84
  "registry": "https://registry.npmjs.org"
85
85
  },
86
- "gitHead": "1cf8a95396bef78410893415c6e0d41863c802ed"
86
+ "gitHead": "4dec4662fb0ccaaf95a9dd3dcc0fffc520a79134"
87
87
  }