File size: 26,951 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
import type { SwarmModeTrigger } from '@moonshot-ai/agent-core-v2/features/swarm/agent/swarm';

import type { AgentContextData } from '#/context';
import { ErrorCodes, KimiError, type KimiErrorCode } from '#/errors';

import { type ApprovalHandler, type Event, type QuestionHandler } from '#/events';
import type { SDKRpcClientBase } from '#/rpc';
import type {
  AddAdditionalDirOptions,
  AddAdditionalDirResult,
  AgentCommandInfo,
  AgentRuntimeBinding,
  BackgroundTaskInfo,
  CapabilityStatus,
  CompactOptions,
  CreateGoalInput,
  GetCronTasksResult,
  GoalSnapshot,
  GoalToolResult,
  JsonObject,
  McpServerConfig,
  McpServerInfo,
  McpStartupMetrics,
  PermissionMode,
  PluginInfo,
  PluginSummary,
  PromptInput,
  PromptSkillActivation,
  ReloadSessionOptions,
  ReloadSummary,
  ResumedSessionState,
  ResumedSessionSummary,
  SessionPlan,
  SessionStatus,
  SessionSummary,
  SessionTodoItem,
  SessionUsage,
  SkillSummary,
  PluginCommandDef,
  ThinkingEffort,
  Unsubscribe,
} from '#/types';

const MAIN_AGENT_ID = 'main';

export interface SessionOptions {
  readonly id: string;
  readonly workDir: string;
  readonly summary?: SessionSummary | undefined;
  readonly resumeState?: ResumedSessionState | undefined;
  readonly rpc: SDKRpcClientBase;
  readonly onClose?: (() => void | Promise<void>) | undefined;
}

/**
 * The capability surface (built-in product capabilities: kimi-cu,
 * kimi-webbridge) exists only on the v2 engine — v1 has no capability
 * domain. Feature-detect structurally so a Session backed by v1 fails with
 * a clear message instead of a confusing missing-method error.
 */
interface CapabilityRpcSurface {
  listCapabilities(): Promise<readonly CapabilityStatus[]>;
  getCapability(id: string): Promise<CapabilityStatus>;
  installCapability(id: string): Promise<CapabilityStatus>;
}

export function capabilityRpc(rpc: SDKRpcClientBase): CapabilityRpcSurface {
  const candidate = rpc as Partial<CapabilityRpcSurface>;
  if (
    typeof candidate.listCapabilities !== 'function' ||
    typeof candidate.getCapability !== 'function' ||
    typeof candidate.installCapability !== 'function'
  ) {
    throw new TypeError('The capability surface is unavailable on this engine (requires v2).');
  }
  return candidate as CapabilityRpcSurface;
}

export class Session {
  readonly id: string;
  readonly workDir: string;
  summary?: SessionSummary | undefined;
  private resumeState: ResumedSessionState | undefined;

  private readonly rpc: SDKRpcClientBase;
  private readonly onClose?: (() => void | Promise<void>) | undefined;
  private closed = false;

  constructor(options: SessionOptions) {
    this.id = options.id;
    this.workDir = options.workDir;
    this.summary = options.summary;
    this.resumeState = options.resumeState ?? resumeStateFromSummary(options.summary);
    this.rpc = options.rpc;
    this.onClose = options.onClose;
  }

  /** True once {@link close} began — the session may still be closing in the engine. */
  get isClosed(): boolean {
    return this.closed;
  }

  getResumeState(): ResumedSessionState | undefined {
    this.ensureOpen();
    return this.resumeState;
  }

  async reloadSession(options?: ReloadSessionOptions): Promise<ResumedSessionSummary> {
    this.ensureOpen();
    const summary = await this.rpc.reloadSession({
      sessionId: this.id,
      forcePluginSessionStartReminder: options?.forcePluginSessionStartReminder,
    });
    this.summary = summary;
    this.resumeState = resumeStateFromSummary(summary);
    return summary;
  }

  onEvent(listener: (event: Event) => void): Unsubscribe {
    this.ensureOpen();
    return this.rpc.onEvent((event) => {
      if (event.sessionId === this.id) {
        listener(event);
      }
    });
  }

  setApprovalHandler(handler: ApprovalHandler | undefined): void {
    this.ensureOpen();
    this.rpc.setApprovalHandler(this.id, handler);
  }

  setQuestionHandler(handler: QuestionHandler | undefined): void {
    this.ensureOpen();
    this.rpc.setQuestionHandler(this.id, handler);
  }

  async prompt(input: string | PromptInput, options?: { promptId?: string }): Promise<void> {
    this.ensureOpen();
    if (options?.promptId !== undefined && options.promptId.length === 0) {
      throw new TypeError('promptId must not be empty');
    }
    await this.rpc.prompt({
      sessionId: this.id,
      input: normalizePromptInput(input),
      promptId: options?.promptId,
    });
  }

  /**
   * Submit one prompt with one or more skill activations bundled into the
   * same user message: the skills are validated up front (an unknown name
   * rejects the whole submission), rendered ahead of the prompt in the same
   * turn, and the bundle undoes as a single anchor. Requires the
   * agent-core-v2 engine.
   */
  async promptWithSkills(
    input: string | PromptInput,
    skills: readonly PromptSkillActivation[],
  ): Promise<void> {
    this.ensureOpen();
    await this.rpc.promptWithSkills({
      sessionId: this.id,
      input: normalizePromptInput(input),
      skills,
    });
  }

  /** Execute a user-initiated `!` shell command (silent — does not prompt the
   *  model). Resolves with the command's stdout/stderr for immediate display.
   *  Pass `commandId` to receive live `shell.output` events for this command. */
  async runShellCommand(
    command: string,
    options?: { commandId?: string },
  ): Promise<{ stdout: string; stderr: string; isError?: boolean; backgrounded?: boolean }> {
    this.ensureOpen();
    return this.rpc.runShellCommand({
      sessionId: this.id,
      command,
      commandId: options?.commandId,
    });
  }

  /** Cancel a running `!` shell command by its commandId (e.g. on Esc / Ctrl+C). */
  async cancelShellCommand(commandId: string): Promise<void> {
    this.ensureOpen();
    return this.rpc.cancelShellCommand({ sessionId: this.id, commandId });
  }

  async steer(input: string | PromptInput): Promise<void> {
    this.ensureOpen();
    await this.rpc.steer({
      sessionId: this.id,
      input: normalizePromptInput(input),
    });
  }

  async swarm(input: string | PromptInput): Promise<void> {
    this.ensureOpen();
    await this.rpc.swarm({
      sessionId: this.id,
      input: normalizePromptInput(input),
    });
  }

  async init(): Promise<void> {
    this.ensureOpen();
    await this.rpc.generateAgentsMd({ sessionId: this.id });
  }

  async getSessionWarnings() {
    this.ensureOpen();
    return this.rpc.getSessionWarnings({ sessionId: this.id });
  }

  async addAdditionalDir(
    path: string,
    options?: AddAdditionalDirOptions,
  ): Promise<AddAdditionalDirResult> {
    this.ensureOpen();
    const normalized = normalizeRequiredString(
      path,
      'Additional directory cannot be empty',
      ErrorCodes.REQUEST_INVALID,
    );
    const result = await this.rpc.addAdditionalDir({
      id: this.id,
      path: normalized,
      persist: options?.persist ?? true,
    });
    this.summary = { ...this.requireSummary(), additionalDirs: result.additionalDirs };
    return result;
  }

  async startBtw(): Promise<string> {
    this.ensureOpen();
    return this.rpc.startBtw({ sessionId: this.id });
  }

  async cancel(): Promise<void> {
    this.ensureOpen();
    await this.rpc.cancel({ sessionId: this.id });
  }

  async setModel(model: string): Promise<void> {
    this.ensureOpen();
    const normalized = normalizeRequiredString(
      model,
      'Session model cannot be empty',
      ErrorCodes.SESSION_MODEL_EMPTY,
    );
    await this.rpc.setModel({ sessionId: this.id, model: normalized });
  }

  async getRuntime(): Promise<AgentRuntimeBinding> {
    this.ensureOpen();
    return this.rpc.getRuntime({ sessionId: this.id });
  }

  async switchRuntime(runtimeId: string): Promise<AgentRuntimeBinding> {
    this.ensureOpen();
    const normalized = normalizeRequiredString(
      runtimeId,
      'Session runtime cannot be empty',
      ErrorCodes.REQUEST_INVALID,
    );
    return this.rpc.switchRuntime({ sessionId: this.id, runtimeId: normalized });
  }

  async setThinking(effort: ThinkingEffort): Promise<void> {
    this.ensureOpen();
    const normalized = normalizeRequiredString(
      effort,
      'Session thinking effort cannot be empty',
      ErrorCodes.SESSION_THINKING_EMPTY,
    );
    await this.rpc.setThinking({ sessionId: this.id, effort: normalized });
  }

  async setPermission(mode: PermissionMode): Promise<void> {
    this.ensureOpen();
    if (!isPermissionMode(mode)) {
      throw new KimiError(
        ErrorCodes.SESSION_PERMISSION_MODE_INVALID,
        'Session permission mode must be yolo, manual, or auto',
      );
    }
    await this.rpc.setPermission({ sessionId: this.id, mode });
  }

  /** Shallow-merge host-owned fields into this session's persisted custom metadata. */
  async updateMetadata(patch: JsonObject): Promise<void> {
    this.ensureOpen();
    if (Object.hasOwn(patch, 'goal')) {
      throw new KimiError(
        ErrorCodes.GOAL_METADATA_RESERVED,
        'Session metadata key "goal" is reserved for the goal lifecycle',
      );
    }
    const summary = this.requireSummary();
    await this.rpc.updateSessionMetadata({ sessionId: this.id, metadata: patch });
    const metadata = { ...summary.metadata, ...patch };
    this.summary = { ...summary, metadata };
    if (this.resumeState !== undefined) {
      this.resumeState = {
        ...this.resumeState,
        sessionMetadata: {
          ...this.resumeState.sessionMetadata,
          custom: { ...this.resumeState.sessionMetadata.custom, ...patch },
        },
      };
    }
  }

  async setPlanMode(enabled: boolean): Promise<void> {
    this.ensureOpen();
    if (typeof enabled !== 'boolean') {
      throw new KimiError(
        ErrorCodes.SESSION_PLAN_MODE_INVALID,
        'Session plan mode must be a boolean',
      );
    }
    await this.rpc.setPlanMode({ sessionId: this.id, enabled });
  }

  async setSwarmMode(enabled: boolean, trigger: SwarmModeTrigger): Promise<void> {
    this.ensureOpen();
    if (typeof enabled !== 'boolean') {
      throw new KimiError(
        ErrorCodes.REQUEST_INVALID,
        'Session swarm mode must be a boolean',
      );
    }
    if (enabled) {
      await this.rpc.setSwarmMode({ sessionId: this.id, enabled: true, trigger });
    } else {
      await this.rpc.setSwarmMode({ sessionId: this.id, enabled: false });
    }
  }

  async setTowerMode(enabled: boolean, base?: string): Promise<void> {
    this.ensureOpen();
    if (typeof enabled !== 'boolean') {
      throw new KimiError(
        ErrorCodes.REQUEST_INVALID,
        'Session tower mode must be a boolean',
      );
    }
    if (base !== undefined && typeof base !== 'string') {
      throw new KimiError(
        ErrorCodes.REQUEST_INVALID,
        'Session tower mode base must be a string',
      );
    }
    await this.rpc.setTowerMode({ sessionId: this.id, enabled, base });
  }

  async getPlan(): Promise<SessionPlan> {
    this.ensureOpen();
    return this.rpc.getPlan({ sessionId: this.id });
  }

  async clearPlan(): Promise<void> {
    this.ensureOpen();
    await this.rpc.clearPlan({ sessionId: this.id });
  }

  async compact(options: CompactOptions = {}): Promise<void> {
    this.ensureOpen();
    const instruction = normalizeOptionalString(options.instruction);
    await this.rpc.compact({
      sessionId: this.id,
      ...(instruction !== undefined ? { instruction } : {}),
    });
  }

  async cancelCompaction(): Promise<void> {
    this.ensureOpen();
    await this.rpc.cancelCompaction({ sessionId: this.id });
  }

  async undoHistory(count: number = 1): Promise<void> {
    this.ensureOpen();
    await this.rpc.undoHistory({ sessionId: this.id, count });
  }

  async getTodos(): Promise<readonly SessionTodoItem[]> {
    this.ensureOpen();
    return this.rpc.getTodos({ sessionId: this.id });
  }

  /** Clear this session's model context without creating a new session. */
  async clearContext(): Promise<void> {
    this.ensureOpen();
    await this.rpc.clearContext({ sessionId: this.id });
  }

  /** Append imported text to this session's context without prompting the model. */
  async importContext(content: string, source: string): Promise<void> {
    this.ensureOpen();
    await this.rpc.importContext({ sessionId: this.id, content, source });
  }

  async getContext(): Promise<AgentContextData> {
    this.ensureOpen();
    return this.rpc.getContext({ sessionId: this.id });
  }

  async getUsage(): Promise<SessionUsage> {
    this.ensureOpen();
    return this.rpc.getUsage({ sessionId: this.id });
  }

  async getStatus(): Promise<SessionStatus> {
    this.ensureOpen();
    return this.rpc.getStatus({ sessionId: this.id });
  }

  async listSkills(): Promise<readonly SkillSummary[]> {
    this.ensureOpen();
    return this.rpc.listSkills({ sessionId: this.id });
  }

  async listPluginCommands(): Promise<readonly PluginCommandDef[]> {
    this.ensureOpen();
    return this.rpc.listPluginCommands({ sessionId: this.id });
  }

  /**
   * Contributed commands registered with this session's interactive agent
   * (agent-core-v2 only — a v1-backed session reports the empty set).
   */
  async listCommands(): Promise<readonly AgentCommandInfo[]> {
    this.ensureOpen();
    return this.rpc.listCommands({ sessionId: this.id });
  }

  /**
   * List background tasks for this session's interactive agent.
   *
   * Defaults to all tasks (including terminal/lost). Pass
   * `{ activeOnly: true }` to filter to non-terminal entries.
   */
  async listBackgroundTasks(
    options: { activeOnly?: boolean; limit?: number } = {},
  ): Promise<readonly BackgroundTaskInfo[]> {
    this.ensureOpen();
    return this.rpc.listBackgroundTasks({
      sessionId: this.id,
      activeOnly: options.activeOnly,
      limit: options.limit,
    });
  }

  /**
   * Read a background task's captured output. Returns the in-memory
   * ring buffer if available, otherwise falls back to the persisted
   * `<sessionDir>/tasks/<taskId>/output.log`. `tail` caps the returned
   * string to that many trailing characters.
   */
  async getBackgroundTaskOutput(
    taskId: string,
    options: { tail?: number } = {},
  ): Promise<string> {
    this.ensureOpen();
    const trimmedTaskId = normalizeRequiredString(
      taskId,
      'Task id cannot be empty',
      ErrorCodes.BACKGROUND_TASK_ID_EMPTY,
    );
    return this.rpc.getBackgroundTaskOutput({
      sessionId: this.id,
      taskId: trimmedTaskId,
      tail: options.tail,
    });
  }

  /**
   * Request a running background task to stop. Sends SIGTERM with a
   * grace period (handled by the core BPM); subscribers receive a
   * `background.task.terminated` event when the kill settles. Calls
   * for unknown or already-terminal task ids are no-ops at the core
   * level — this method does not throw in those cases.
   */
  async stopBackgroundTask(
    taskId: string,
    options: { reason?: string } = {},
  ): Promise<void> {
    this.ensureOpen();
    const trimmedTaskId = normalizeRequiredString(
      taskId,
      'Task id cannot be empty',
      ErrorCodes.BACKGROUND_TASK_ID_EMPTY,
    );
    await this.rpc.stopBackgroundTask({
      sessionId: this.id,
      taskId: trimmedTaskId,
      reason: options.reason,
    });
  }

  /**
   * Detach a running foreground task so the current tool call can return while
   * the task continues under background-task management.
   */
  async detachBackgroundTask(taskId: string): Promise<BackgroundTaskInfo | undefined> {
    this.ensureOpen();
    const trimmedTaskId = normalizeRequiredString(
      taskId,
      'Task id cannot be empty',
      ErrorCodes.BACKGROUND_TASK_ID_EMPTY,
    );
    return this.rpc.detachBackgroundTask({
      sessionId: this.id,
      taskId: trimmedTaskId,
    });
  }

  /**
   * Block until every still-running background task (across all agents in this
   * session) reaches a terminal state. Used by `kimi -p` after the main agent's
   * turn finishes when the resolved print background mode is `'drain'`
   * (`print_background_mode = "drain"`, or the legacy `keep_alive_on_exit = true`
   * fallback), so background subagents get a chance to complete before the process
   * exits. No-op in other modes. Bounded by `background.print_wait_ceiling_s`.
   */
  async waitForBackgroundTasksOnPrint(): Promise<void> {
    this.ensureOpen();
    await this.rpc.waitForBackgroundTasksOnPrint({ sessionId: this.id });
  }

  /**
   * Used by `kimi -p` after the main agent's turn ends with `reason ===
   * 'completed'`. Returns `'finish'` when the run may exit, or `'continue'` when
   * the caller must keep the session alive so a background-task completion can
   * steer the main agent into a new turn. Policy is selected by
   * `background.print_background_mode` (`'exit' | 'drain' | 'steer'`); when unset
   * it falls back to the legacy `keep_alive_on_exit` mapping (`true ⇒ 'drain'`).
   */
  async handlePrintMainTurnCompleted(): Promise<'finish' | 'continue'> {
    this.ensureOpen();
    return this.rpc.handlePrintMainTurnCompleted({ sessionId: this.id });
  }

  // --- Goal lifecycle ---------------------------------------------------
  // Deterministic user/host control surface. There is intentionally no
  // `updateGoal`: the goal's terminal status is decided by the model via the
  // in-conversation UpdateGoal tool (or the goal driver on budget/error), not
  // by the host.

  async createGoal(input: CreateGoalInput): Promise<GoalSnapshot> {
    this.ensureOpen();
    return this.rpc.createGoal({ sessionId: this.id, ...input });
  }

  async getGoal(): Promise<GoalToolResult> {
    this.ensureOpen();
    return this.rpc.getGoal({ sessionId: this.id });
  }

  async pauseGoal(): Promise<GoalSnapshot> {
    this.ensureOpen();
    return this.rpc.pauseGoal({ sessionId: this.id });
  }

  async resumeGoal(): Promise<GoalSnapshot> {
    this.ensureOpen();
    return this.rpc.resumeGoal({ sessionId: this.id });
  }

  async cancelGoal(): Promise<GoalSnapshot> {
    this.ensureOpen();
    return this.rpc.cancelGoal({ sessionId: this.id });
  }

  /**
   * Enumerate the cron tasks scheduled in this session. Hosts running a
   * bounded session lifetime (e.g. `kimi -p`) poll this to decide whether
   * pending scheduled work still needs the process alive.
   */
  async getCronTasks(): Promise<GetCronTasksResult> {
    this.ensureOpen();
    return this.rpc.getCronTasks({ sessionId: this.id });
  }

  async listMcpServers(): Promise<readonly McpServerInfo[]> {
    this.ensureOpen();
    return this.rpc.listMcpServers({ sessionId: this.id });
  }

  async getMcpStartupMetrics(): Promise<McpStartupMetrics> {
    this.ensureOpen();
    return this.rpc.getMcpStartupMetrics({ sessionId: this.id });
  }

  /**
   * Connect an MCP server in this live session. `persist: true` also writes
   * the user-level `mcp.json` (the entry becomes a mutable `global` one);
   * otherwise it stays a session-local `caller` entry.
   */
  async addMcpServer(
    server: McpServerConfig,
    options: { readonly persist?: boolean } = {},
  ): Promise<McpServerInfo> {
    this.ensureOpen();
    return this.rpc.addSessionMcpServer({
      sessionId: this.id,
      server,
      persist: options.persist,
    });
  }

  /**
   * Reconnect a server. Without `config` the session re-resolves the current
   * effective config from the unified registry (file edits and plugin
   * enable/disable land here). With `config`, the entry is replaced with the
   * given full config — a plugin-contributed server rejects this because its
   * config is read-only, owned by the plugin manifest.
   */
  async reconnectMcpServer(name: string, config?: McpServerConfig): Promise<void> {
    this.ensureOpen();
    await this.rpc.reconnectMcpServer({ sessionId: this.id, name, config });
  }

  async listPlugins(): Promise<readonly PluginSummary[]> {
    this.ensureOpen();
    return this.rpc.listPlugins();
  }

  async installPlugin(source: string): Promise<PluginSummary> {
    this.ensureOpen();
    return this.rpc.installPlugin(source);
  }

  async setPluginEnabled(id: string, enabled: boolean): Promise<void> {
    this.ensureOpen();
    await this.rpc.setPluginEnabled(id, enabled);
  }

  /** Built-in capabilities with layered readiness (v2 engine only). */
  async listCapabilities(): Promise<readonly CapabilityStatus[]> {
    this.ensureOpen();
    return capabilityRpc(this.rpc).listCapabilities();
  }

  /** One capability's layered readiness + live install progress. */
  async getCapability(id: string): Promise<CapabilityStatus> {
    this.ensureOpen();
    return capabilityRpc(this.rpc).getCapability(id);
  }

  /**
   * Start an idempotent capability install (binary runtime + wiring) in the
   * background; poll `getCapability` for progress.
   */
  async installCapability(id: string): Promise<CapabilityStatus> {
    this.ensureOpen();
    return capabilityRpc(this.rpc).installCapability(id);
  }

  async setPluginMcpServerEnabled(
    id: string,
    server: string,
    enabled: boolean,
  ): Promise<void> {
    this.ensureOpen();
    await this.rpc.setPluginMcpServerEnabled(id, server, enabled);
  }

  async removePlugin(id: string): Promise<void> {
    this.ensureOpen();
    await this.rpc.removePlugin(id);
  }

  async reloadPlugins(): Promise<ReloadSummary> {
    this.ensureOpen();
    return this.rpc.reloadPlugins();
  }

  async getPluginInfo(id: string): Promise<PluginInfo> {
    this.ensureOpen();
    return this.rpc.getPluginInfo(id);
  }

  async activateSkill(name: string, args?: string | undefined): Promise<void> {
    this.ensureOpen();
    const skillName = normalizeRequiredString(
      name,
      'Skill name cannot be empty',
      ErrorCodes.SKILL_NAME_EMPTY,
    );
    const skillArgs = normalizeOptionalString(args);
    await this.rpc.activateSkill({
      sessionId: this.id,
      name: skillName,
      ...(skillArgs !== undefined ? { args: skillArgs } : {}),
    });
  }

  async activatePluginCommand(
    pluginId: string,
    commandName: string,
    args?: string | undefined,
  ): Promise<void> {
    this.ensureOpen();
    const normalizedPluginId = pluginId.trim();
    const normalizedCommandName = commandName.trim();
    if (normalizedPluginId.length === 0 || normalizedCommandName.length === 0) {
      throw new KimiError(
        ErrorCodes.REQUEST_INVALID,
        'Plugin id and command name cannot be empty',
      );
    }
    const commandArgs = normalizeOptionalString(args);
    await this.rpc.activatePluginCommand({
      sessionId: this.id,
      pluginId: normalizedPluginId,
      commandName: normalizedCommandName,
      ...(commandArgs !== undefined ? { args: commandArgs } : {}),
    });
  }

  /**
   * Run a contributed command engine-side (agent-core-v2 only — a v1-backed
   * client rejects with `not_implemented`). Unknown names reject with the
   * engine's `request.invalid` error.
   */
  async runCommand(name: string, args?: string): Promise<void> {
    this.ensureOpen();
    const commandName = name.trim();
    if (commandName.length === 0) {
      throw new KimiError(ErrorCodes.REQUEST_INVALID, 'Command name cannot be empty');
    }
    await this.rpc.runCommand({
      sessionId: this.id,
      name: commandName,
      args: normalizeOptionalString(args),
    });
  }

  async close(): Promise<void> {
    if (this.closed) return;
    this.closed = true;
    try {
      await this.rpc.closeSession({ sessionId: this.id });
    } finally {
      this.rpc.clearSessionHandlers(this.id);
      await this.onClose?.();
    }
  }

  /** @internal */
  emitMetaUpdated(patch: { readonly title?: string; readonly isCustomTitle?: boolean }): void {
    this.emit({
      type: 'session.meta.updated',
      sessionId: this.id,
      agentId: MAIN_AGENT_ID,
      title: patch.title,
      patch,
    });
  }

  private emit(event: Event): void {
    this.rpc.receiveEvent(event);
  }

  private ensureOpen(): void {
    if (this.closed) {
      throw new KimiError(ErrorCodes.SESSION_CLOSED, 'Session is closed');
    }
  }

  private requireSummary(): SessionSummary {
    if (this.summary === undefined) {
      throw new KimiError(ErrorCodes.SESSION_STATE_INVALID, 'Session summary is unavailable');
    }
    return this.summary;
  }
}

function normalizePromptInput(input: string | PromptInput): PromptInput {
  if (typeof input === 'string') {
    if (input.trim().length === 0) {
      throw new KimiError(ErrorCodes.REQUEST_PROMPT_INPUT_EMPTY, 'Prompt input cannot be empty');
    }
    return [{ type: 'text', text: input }];
  }

  if (input.length === 0) {
    throw new KimiError(ErrorCodes.REQUEST_PROMPT_INPUT_EMPTY, 'Prompt input cannot be empty');
  }

  for (const part of input) {
    switch (part.type) {
      case 'text':
        if (part.text.trim().length === 0) {
          throw new KimiError(
            ErrorCodes.REQUEST_PROMPT_INPUT_EMPTY,
            'Prompt input cannot contain empty text parts',
          );
        }
        break;
      case 'image_url':
        if (part.imageUrl.url.trim().length === 0) {
          throw new KimiError(
            ErrorCodes.REQUEST_PROMPT_INPUT_EMPTY,
            'Prompt input cannot contain empty image URLs',
          );
        }
        break;
      case 'video_url':
        if (part.videoUrl.url.trim().length === 0) {
          throw new KimiError(
            ErrorCodes.REQUEST_PROMPT_INPUT_EMPTY,
            'Prompt input cannot contain empty video URLs',
          );
        }
        break;
    }
  }
  return input;
}

function normalizeRequiredString(
  value: string,
  message: string,
  code: KimiErrorCode,
): string {
  const normalized = value.trim();
  if (normalized.length === 0) {
    throw new KimiError(code, message);
  }
  return normalized;
}

function normalizeOptionalString(value: string | undefined): string | undefined {
  if (value === undefined) return undefined;
  const normalized = value.trim();
  return normalized.length > 0 ? normalized : undefined;
}

function isPermissionMode(value: unknown): value is PermissionMode {
  return value === 'yolo' || value === 'manual' || value === 'auto';
}

function resumeStateFromSummary(
  summary: SessionSummary | undefined,
): ResumedSessionState | undefined {
  if (!hasResumeState(summary)) return undefined;
  return {
    sessionMetadata: summary.sessionMetadata,
    agents: summary.agents,
    warning: summary.warning,
  };
}

function hasResumeState(
  summary: SessionSummary | undefined,
): summary is SessionSummary & ResumedSessionState {
  return (
    summary !== undefined &&
    typeof (summary as { readonly sessionMetadata?: unknown }).sessionMetadata === 'object' &&
    (summary as { readonly sessionMetadata?: unknown }).sessionMetadata !== null &&
    typeof (summary as { readonly agents?: unknown }).agents === 'object' &&
    (summary as { readonly agents?: unknown }).agents !== null
  );
}