File size: 29,044 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
/**
 * Probe the `select_tools` (progressive tool disclosure) capability of the
 * kimi-type providers, based on the real providers/auth in
 * `~/.kimi-code/config.toml`.
 *
 * Feature recap (from the Tool Select guide): with the `tool-select`
 * experimental flag + `tool_use` + `dynamically_loaded_tools` capabilities,
 * MCP tool schemas no longer ride the top-level `tools[]`; the model calls
 * the builtin `select_tools` with exact names (announced in
 * `<tools_added>`/`<tools_removed>` system reminders), the engine injects
 * the loaded schemas as a `role: 'system'` message whose `tools` field
 * carries full definitions, and the model then calls the loaded tool.
 *
 * The wire fact that makes this kimi-specific: only the kimi vendor trait
 * serializes `message.tools` into the outbound request
 * (`kimiOpenAITrait.convertMessage`); every other base SKIPS
 * tool-declaration-only messages entirely. So the whole mechanism can only
 * work on `(kimi, openai)` β€” this example proves that on a stub, then
 * behaviorally tests each real kimi model with a simulated "kimi computer
 * use" tool pool.
 *
 * Part A β€” deterministic, local stub (OpenAI Chat Completions wire):
 *   1. kimi providerType: the tool-declaration message reaches the wire with
 *      an embedded `tools` array.
 *   2. no providerType (plain openai): the same message is dropped β€” the
 *      boundary non-kimi providers cannot cross.
 *   3. `deferred: true` top-level tools are filtered out of the wire
 *      `tools[]` by the contract's generate() (their schema travels via the
 *      declaration message instead).
 *
 * Part B β€” live, per kimi model from the real config: a two-step flow that
 * mirrors exactly what the agent loop produces.
 *   step 1: announcement + `select_tools` available, user asks for a
 *           screenshot β€” does the model call `select_tools`, with names from
 *           the announced list?
 *   step 2: history += the select_tools call, its "Loaded: ..." result, and
 *           the schema-injection system message β€” does the model now call
 *           the loaded computer-use tool?
 *
 * Run:
 *   pnpm -C packages/klient exec tsx --tsconfig ./tsconfig.examples.json \
 *     --import ../../build/register-raw-text-loader.mjs examples/kimi-select-tools.ts
 *
 * Env:
 *   KIMI_CODE_HOME              β€” default `~/.kimi-code`
 *   KIMI_SELECT_TOOLS_MODELS    β€” comma-separated model ids for the live parts (default: all kimi-type)
 *   KIMI_SELECT_TOOLS_SKIP_LIVE β€” set to `1` to skip part B (no real API calls)
 *   KIMI_SELECT_TOOLS_TAP       β€” set to `1` to run part C instead of B: route
 *                                 the flow through a logging proxy and dump
 *                                 the actual wire Context of each request.
 */
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { homedir } from 'node:os';
import { join } from 'node:path';

import { EXAMPLE_CLIENT_IDENTITY } from './identity.js';

import type { AddressInfo } from 'node:net';

import { bootstrap, logSeed, resolveLoggingConfig } from '@moonshot-ai/agent-core-v2';
import { IConfigService } from '@moonshot-ai/agent-core-v2/app/config/config';
import { renderLoadableToolsAnnouncement } from '@moonshot-ai/agent-core-v2/agent/toolSelect/dynamicTools';
import { UNKNOWN_CAPABILITY } from '@moonshot-ai/agent-core-v2/llm-adapter/contract/capability';
import type { Message } from '@moonshot-ai/agent-core-v2/llm-adapter/contract/message';
import type { ToolDescription as Tool } from '@moonshot-ai/agent-core-v2/human/llm/message';
import { createStaticCredentialProvider } from '@moonshot-ai/agent-core-v2/human/credentials/credentials';
import type { Model } from '@moonshot-ai/agent-core-v2/llm-adapter/model/catalog';
import { IModelCatalog } from '@moonshot-ai/agent-core-v2/llm-adapter/model/catalog';
import type {
  ModelRequestInput,
  ModelRequester,
} from '@moonshot-ai/agent-core-v2/llm-adapter/model/model-requester';
import { ModelRequesterImpl } from '@moonshot-ai/agent-core-v2/llm-adapter/model/model-requester-impl';
import { IProtocolAdapterRegistry } from '@moonshot-ai/agent-core-v2/llm-adapter/protocol/protocol';
import { ProtocolAdapterRegistry } from '@moonshot-ai/agent-core-v2/llm-adapter/protocol/protocolAdapterRegistry';

function assert(cond: boolean, message: string): asserts cond {
  if (!cond) throw new Error(`assertion failed: ${message}`);
}

// ---------------------------------------------------------------------------
// The simulated "kimi computer use" MCP tool pool.
// ---------------------------------------------------------------------------

const COMPUTER_USE_TOOLS: readonly Tool[] = [
  {
    name: 'computer_screenshot',
    description: 'Take a screenshot of the current screen and return it as an image.',
    parameters: { type: 'object', properties: {}, additionalProperties: false },
  },
  {
    name: 'computer_click',
    description: 'Click at a screen coordinate.',
    parameters: {
      type: 'object',
      properties: {
        x: { type: 'number', description: 'Screen x coordinate.' },
        y: { type: 'number', description: 'Screen y coordinate.' },
        button: { type: 'string', enum: ['left', 'right', 'middle'] },
      },
      required: ['x', 'y'],
      additionalProperties: false,
    },
  },
  {
    name: 'computer_move',
    description: 'Move the mouse to a screen coordinate.',
    parameters: {
      type: 'object',
      properties: {
        x: { type: 'number' },
        y: { type: 'number' },
      },
      required: ['x', 'y'],
      additionalProperties: false,
    },
  },
  {
    name: 'computer_type',
    description: 'Type text at the current focus.',
    parameters: {
      type: 'object',
      properties: { text: { type: 'string' } },
      required: ['text'],
      additionalProperties: false,
    },
  },
  {
    name: 'computer_scroll',
    description: 'Scroll at a screen coordinate.',
    parameters: {
      type: 'object',
      properties: {
        x: { type: 'number' },
        y: { type: 'number' },
        delta_y: { type: 'number' },
      },
      required: ['x', 'y', 'delta_y'],
      additionalProperties: false,
    },
  },
];

const COMPUTER_USE_NAMES = COMPUTER_USE_TOOLS.map((t) => t.name);

/** The builtin select_tools schema, mirroring `SelectToolsInputSchema`. */
const SELECT_TOOLS: Tool = {
  name: 'select_tools',
  description:
    'Load one or more tools by name so you can call them. ' +
    'All available tool names are listed in the <tools_added>/<tools_removed> announcements ' +
    'in the system context β€” fold them in order to get the current list. ' +
    'Pass the exact name(s) you need; their full definitions become available immediately, ' +
    'so you can call them directly in your next tool call.',
  parameters: {
    type: 'object',
    properties: {
      names: {
        type: 'array',
        items: { type: 'string' },
        minItems: 1,
        description: 'Exact tool names to load, taken from the latest announced tool list.',
      },
    },
    required: ['names'],
    additionalProperties: false,
  },
};

const SYSTEM_PROMPT =
  'You are a computer-use agent operating the user\u2019s computer through tool calls. ' +
  'Only a core tool set is available up front; additional tools are announced by name in ' +
  '<tools_added> blocks in the system context. To use an announced tool, first call ' +
  'select_tools with its exact name to load its definition, then call the tool itself.';

const userMessage = (text: string): Message => ({
  role: 'user',
  content: [{ type: 'text', text }],
  toolCalls: [],
});

/** The schema-injection message the engine appends after a successful load. */
const toolDeclarationMessage = (tools: readonly Tool[]): Message => ({
  role: 'system',
  content: [],
  toolCalls: [],
  tools,
});

const announcementMessage = (names: readonly string[]): Message => ({
  role: 'system',
  content: [
    { type: 'text', text: renderLoadableToolsAnnouncement(names, []) },
  ],
  toolCalls: [],
});

// ---------------------------------------------------------------------------
// Shared request draining.
// ---------------------------------------------------------------------------

interface Collected {
  readonly text: string;
  readonly toolCalls: readonly { id: string; name: string; arguments: string | null }[];
  readonly finishReason?: string | undefined;
}

async function collect(
  requester: ModelRequester,
  input: ModelRequestInput,
  signal?: AbortSignal,
): Promise<Collected> {
  let text = '';
  let toolCalls: Collected['toolCalls'] = [];
  let finishReason: string | undefined;
  for await (const event of requester.request(input, signal)) {
    if (event.type === 'part' && event.part.type === 'text') text += event.part.text;
    if (event.type === 'finish') {
      finishReason = event.providerFinishReason ?? event.rawFinishReason;
      toolCalls = event.message.toolCalls.map((t) => ({
        id: t.id,
        name: t.name,
        arguments: t.arguments,
      }));
    }
  }
  return { text, toolCalls, finishReason };
}

// ---------------------------------------------------------------------------
// Part A β€” deterministic wire-encoding probes against a local stub.
// ---------------------------------------------------------------------------

const sseChunk = (delta: object, finishReason: string | null): string =>
  JSON.stringify({
    id: 'chatcmpl-probe',
    object: 'chat.completion.chunk',
    created: 0,
    model: 'probe-model',
    choices: [{ index: 0, delta, finish_reason: finishReason }],
  });

function writePong(res: ServerResponse): void {
  res.writeHead(200, { 'content-type': 'text/event-stream' });
  res.write(`data: ${sseChunk({ role: 'assistant' }, null)}\n\n`);
  res.write(`data: ${sseChunk({ content: 'pong' }, null)}\n\n`);
  res.write(`data: ${sseChunk({}, 'stop')}\n\n`);
  res.end('data: [DONE]\n\n');
}

interface WireMessage {
  readonly role?: string;
  readonly tools?: readonly { function?: { name?: string } }[];
}

interface WireBody {
  readonly messages?: readonly WireMessage[];
  readonly tools?: readonly { function?: { name?: string } }[];
}

async function probeWireEncoding(): Promise<void> {
  console.log('\n=== part A: wire-encoding boundary (local stub) ===');
  let handler: (req: IncomingMessage, res: ServerResponse) => void = (_req, res) =>
    writePong(res);
  let lastBody: WireBody | undefined;

  const server = createServer((req, res) => {
    const chunks: Buffer[] = [];
    req.on('data', (chunk: Buffer) => chunks.push(chunk));
    req.on('end', () => {
      try {
        lastBody = JSON.parse(Buffer.concat(chunks).toString('utf8')) as WireBody;
      } catch {
        lastBody = undefined;
      }
      handler(req, res);
    });
  });
  await new Promise<void>((resolve) => {
    server.listen(0, '127.0.0.1', () => resolve());
  });
  const port = (server.address() as AddressInfo).port;

  const registry = new ProtocolAdapterRegistry();
  const makeRequester = (providerType?: string): ModelRequester => {
    const model: Model = {
      id: 'probe',
      name: 'probe-model',
      aliases: [],
      protocol: 'openai',
      baseUrl: `http://127.0.0.1:${String(port)}`,
      headers: {},
      capabilities: { ...UNKNOWN_CAPABILITY, max_context_tokens: 8192 },
      maxContextSize: 8192,
      alwaysThinking: false,
      providerType,
      providerName: providerType ?? 'probe',
      credentialProvider: createStaticCredentialProvider('sk-probe'),
    };
    return new ModelRequesterImpl(model, registry);
  };

  const input: ModelRequestInput = {
    systemPrompt: SYSTEM_PROMPT,
    tools: [SELECT_TOOLS, { ...COMPUTER_USE_TOOLS[0]!, deferred: true }],
    messages: [
      announcementMessage(COMPUTER_USE_NAMES),
      userMessage('take a screenshot'),
      toolDeclarationMessage([COMPUTER_USE_TOOLS[0]!]),
    ],
  };

  try {
    // 1) kimi providerType: the declaration message reaches the wire, with the
    // loaded schema embedded in the message β€” and the deferred top-level tool
    // does NOT duplicate into the wire tools[].
    await collect(makeRequester('kimi'), input);
    const kimiBody = lastBody;
    const declaration = kimiBody?.messages?.find((m) => Array.isArray(m.tools));
    assert(declaration !== undefined, 'kimi: a wire message carries the loaded tool schemas');
    assert(
      declaration.tools?.some((t) => t.function?.name === 'computer_screenshot') === true,
      'kimi: declaration embeds computer_screenshot',
    );
    assert(
      kimiBody?.tools?.some((t) => t.function?.name === 'select_tools') === true,
      'kimi: top-level tools[] keeps select_tools',
    );
    assert(
      kimiBody?.tools?.some((t) => t.function?.name === 'computer_screenshot') !== true,
      'kimi: deferred tool stays OUT of top-level tools[]',
    );
    console.log(
      `[ok] kimi: declaration on wire (role=${String(declaration.role)}), ` +
        `top-level tools=[${(kimiBody?.tools ?? []).map((t) => t.function?.name ?? '?').join(', ')}]`,
    );

    // 2) plain openai: the same declaration message is dropped outright β€” the
    // dynamic-tool schema can never reach a non-kimi wire. (3 wire messages =
    // system prompt + announcement + user; the declaration is gone.)
    await collect(makeRequester(), input);
    const plainBody = lastBody;
    assert(
      plainBody?.messages?.every((m) => !Array.isArray(m.tools)) === true,
      'openai: declaration message dropped (no wire message carries tools)',
    );
    assert(
      plainBody?.messages?.length === 3,
      `openai: declaration removed from history (got ${String(plainBody?.messages?.length)} messages)`,
    );
    console.log(
      '[ok] openai: declaration message dropped β€” dynamic tools cannot cross a non-kimi wire',
    );
  } finally {
    server.closeAllConnections();
    await new Promise<void>((resolve) => {
      server.close(() => resolve());
    });
  }
}

// ---------------------------------------------------------------------------
// Part B β€” live two-step select_tools flow per real kimi model.
// ---------------------------------------------------------------------------

type Step1Outcome =
  | { readonly kind: 'selected'; readonly names: readonly string[]; readonly callId: string; readonly argumentsJson: string }
  | { readonly kind: 'no-call'; readonly text: string }
  | { readonly kind: 'error'; readonly message: string };

type Step2Outcome =
  | { readonly kind: 'called'; readonly name: string; readonly argumentsJson: string | null }
  | { readonly kind: 'other'; readonly description: string }
  | { readonly kind: 'error'; readonly message: string };

async function withTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {
  let timer: ReturnType<typeof setTimeout> | undefined;
  try {
    return await Promise.race([
      promise,
      new Promise<never>((_resolve, reject) => {
        timer = setTimeout(() => reject(new Error(`${label} timed out after ${String(ms)}ms`)), ms);
      }),
    ]);
  } finally {
    if (timer !== undefined) clearTimeout(timer);
  }
}

interface Scenario {
  readonly prompt: string;
  /** The tool a correct model is expected to select and call. */
  readonly expectTool: string;
}

const SCENARIOS: readonly Scenario[] = [
  { prompt: 'Take a screenshot of my screen right now.', expectTool: 'computer_screenshot' },
  {
    prompt: 'Click at the exact center of my screen. The screen resolution is 1920x1080.',
    expectTool: 'computer_click',
  },
];

async function step1Select(requester: ModelRequester, scenario: Scenario): Promise<Step1Outcome> {
  try {
    const result = await withTimeout(
      collect(requester, {
        systemPrompt: SYSTEM_PROMPT,
        tools: [SELECT_TOOLS],
        messages: [
          announcementMessage(COMPUTER_USE_NAMES),
          userMessage(scenario.prompt),
        ],
      }),
      60_000,
      'step1',
    );
    const call = result.toolCalls.find((t) => t.name === 'select_tools');
    if (call === undefined) {
      const other = result.toolCalls.map((t) => t.name).join(', ');
      return {
        kind: 'no-call',
        text:
          result.toolCalls.length > 0
            ? `called [${other}] instead`
            : `answered text: ${result.text.slice(0, 80)}`,
      };
    }
    let names: string[] = [];
    try {
      const parsed = JSON.parse(call.arguments ?? '{}') as { names?: unknown };
      if (Array.isArray(parsed.names)) names = parsed.names.filter((n): n is string => typeof n === 'string');
    } catch {
      // keep names empty β€” reported below
    }
    return { kind: 'selected', names, callId: call.id, argumentsJson: call.arguments ?? '' };
  } catch (error) {
    return { kind: 'error', message: error instanceof Error ? error.message.split('\n')[0] ?? '' : String(error) };
  }
}

async function step2UseLoadedTool(
  requester: ModelRequester,
  step1: Extract<Step1Outcome, { kind: 'selected' }>,
  scenario: Scenario,
): Promise<Step2Outcome> {
  const validNames = step1.names.filter((n) => COMPUTER_USE_NAMES.includes(n));
  const loadName = validNames.includes(scenario.expectTool)
    ? scenario.expectTool
    : validNames[0];
  if (loadName === undefined) {
    return { kind: 'other', description: `selected names not in announced list: [${step1.names.join(', ')}]` };
  }
  const loadedTool = COMPUTER_USE_TOOLS.find((t) => t.name === loadName)!;
  const messages = (withDeclaration: boolean): ModelRequestInput => ({
    systemPrompt: SYSTEM_PROMPT,
    tools: [SELECT_TOOLS, { ...loadedTool, deferred: true }],
    messages: [
      announcementMessage(COMPUTER_USE_NAMES),
      userMessage(scenario.prompt),
      {
        role: 'assistant',
        content: [],
        toolCalls: [
          {
            type: 'function',
            id: step1.callId,
            name: 'select_tools',
            arguments: JSON.stringify({ names: [loadName] }),
          },
        ],
      },
      {
        role: 'tool',
        toolCallId: step1.callId,
        content: [{ type: 'text', text: `Loaded: ${loadName}` }],
        toolCalls: [],
      },
      // The schema-injection message β€” the exact wire shape whose acceptance
      // this probe measures. Dropped entirely in the isolation retry below.
      ...(withDeclaration ? [toolDeclarationMessage([loadedTool])] : []),
    ],
  });
  try {
    const result = await withTimeout(collect(requester, messages(true)), 60_000, 'step2');
    const call = result.toolCalls.find((t) => t.name === loadName);
    if (call !== undefined) {
      return { kind: 'called', name: call.name, argumentsJson: call.arguments };
    }
    const other = result.toolCalls.map((t) => t.name).join(', ');
    return {
      kind: 'other',
      description:
        result.toolCalls.length > 0
          ? `called [${other}] instead of ${loadName}`
          : `answered text: ${result.text.slice(0, 80)}`,
    };
  } catch (error) {
    const message = error instanceof Error ? error.message.split('\n')[0] ?? '' : String(error);
    // Differential: retry the SAME request minus the schema-injection message.
    // If it then succeeds, the backend rejects the dynamic-schema wire shape
    // specifically; if it fails too, the problem is elsewhere in the flow.
    try {
      await withTimeout(collect(requester, messages(false)), 60_000, 'step2-isolation');
      return {
        kind: 'error',
        message: `${message} (isolation: same request WITHOUT the tools-in-message injection succeeds β€” backend rejects the dynamic-schema wire shape)`,
      };
    } catch {
      return { kind: 'error', message: `${message} (isolation: also fails without the injection β€” not caused by the declaration message)` };
    }
  }
}

async function probeLiveKimiProviders(): Promise<void> {
  const homeDir = process.env['KIMI_CODE_HOME'] ?? join(homedir(), '.kimi-code');
  console.log(`\n=== part B: live select_tools flow on real kimi providers (${homeDir}) ===`);
  const { app } = bootstrap({ homeDir, clientIdentity: EXAMPLE_CLIENT_IDENTITY }, [
    ...logSeed(resolveLoggingConfig({ homeDir, env: process.env })),
  ]);
  try {
    await app.accessor.get(IConfigService).ready;
    const catalog = app.accessor.get(IModelCatalog);

    const filter = process.env['KIMI_SELECT_TOOLS_MODELS']?.split(',').map((s) => s.trim());
    const models = await catalog.listModels();
    const targets = models.filter((m) => {
      if (filter !== undefined && !filter.includes(m.model)) return false;
      try {
        return catalog.get(m.model).providerType === 'kimi';
      } catch {
        return false;
      }
    });
    assert(targets.length > 0, 'at least one kimi-type model configured');

    const summary: string[] = [];
    for (const m of targets) {
      const model = catalog.get(m.model);
      const declared = model.capabilities.dynamically_loaded_tools === true;
      const requester = catalog.getRequester(m.model);

      for (const scenario of SCENARIOS) {
        const startedAt = Date.now();
        const step1 = await step1Select(requester, scenario);
        let row: string;
        if (step1.kind === 'selected') {
          const step2 = await step2UseLoadedTool(requester, step1, scenario);
          const elapsed = `${String(Date.now() - startedAt)}ms`;
          if (step2.kind === 'called') {
            row = `PASS  select=[${step1.names.join(', ')}] then called ${step2.name}(${step2.argumentsJson ?? ''})`;
          } else if (step2.kind === 'other') {
            row = `PARTIAL  select=[${step1.names.join(', ')}] but step2: ${step2.description}`;
          } else {
            row = `PARTIAL  select=[${step1.names.join(', ')}] but step2 error: ${step2.message}`;
          }
          console.log(`[${m.model}] ${scenario.expectTool}  ${elapsed}  declared=${String(declared)}  ${row}`);
        } else if (step1.kind === 'no-call') {
          row = `FAIL  no select_tools call: ${step1.text}`;
          console.log(`[${m.model}] ${scenario.expectTool}  ${String(Date.now() - startedAt)}ms  declared=${String(declared)}  ${row}`);
        } else {
          row = `ERROR  ${step1.message}`;
          console.log(`[${m.model}] ${scenario.expectTool}  ${String(Date.now() - startedAt)}ms  declared=${String(declared)}  ${row}`);
        }
        summary.push(`${m.model.padEnd(40)} ${scenario.expectTool.padEnd(22)} ${row}`);
      }
    }

    console.log('\n=== live summary (declared = config declares dynamically_loaded_tools) ===');
    for (const line of summary) console.log(line);
  } finally {
    app.dispose();
  }
}

// ---------------------------------------------------------------------------
// Part C β€” TAP mode: sit a logging proxy between the requester and the real
// endpoint, and dump the actual wire Context (structure digest only β€” auth
// headers are forwarded untouched, never printed).
// ---------------------------------------------------------------------------

interface TapWireTool {
  readonly type?: string;
  readonly function?: { readonly name?: string };
}

interface TapWireMessage {
  readonly role?: string;
  readonly content?: unknown;
  readonly tools?: readonly TapWireTool[];
  readonly tool_calls?: readonly { readonly function?: { readonly name?: string } }[];
  readonly tool_call_id?: string;
}

function describeWireBody(raw: Buffer): string[] {
  const lines: string[] = [];
  let body: { tools?: TapWireTool[]; messages?: TapWireMessage[]; model?: string };
  try {
    body = JSON.parse(raw.toString('utf8')) as typeof body;
  } catch {
    return ['    (unparseable body)'];
  }
  const toolName = (t: TapWireTool): string => t.function?.name ?? t.type ?? '?';
  lines.push(`    model=${String(body.model)}`);
  lines.push(`    top-level tools[] = [${(body.tools ?? []).map(toolName).join(', ')}]`);
  for (const [i, m] of (body.messages ?? []).entries()) {
    const parts: string[] = [`#${String(i)} role=${String(m.role)}`];
    if (Array.isArray(m.tools)) {
      parts.push(`>>> tools=[${m.tools.map(toolName).join(', ')}] (dynamic schema injected HERE)`);
    }
    if (Array.isArray(m.tool_calls)) {
      parts.push(`tool_calls=[${m.tool_calls.map((tc) => tc.function?.name ?? '?').join(', ')}]`);
    }
    if (m.tool_call_id !== undefined) parts.push(`tool_call_id=${m.tool_call_id}`);
    const content =
      typeof m.content === 'string' ? m.content : m.content === undefined ? '' : JSON.stringify(m.content);
    if (content.length > 0) parts.push(`content=${content.slice(0, 60).replaceAll('\n', ' ')}…`);
    lines.push(`    ${parts.join('  ')}`);
  }
  return lines;
}

async function probeTappedContext(): Promise<void> {
  const homeDir = process.env['KIMI_CODE_HOME'] ?? join(homedir(), '.kimi-code');
  console.log(`\n=== part C: tapped wire context (${homeDir}) ===`);
  const { app } = bootstrap({ homeDir, clientIdentity: EXAMPLE_CLIENT_IDENTITY }, [
    ...logSeed(resolveLoggingConfig({ homeDir, env: process.env })),
  ]);
  try {
    await app.accessor.get(IConfigService).ready;
    const catalog = app.accessor.get(IModelCatalog);
    const registry = app.accessor.get(IProtocolAdapterRegistry);

    const filter = process.env['KIMI_SELECT_TOOLS_MODELS']?.split(',').map((s) => s.trim());
    const models = await catalog.listModels();
    const targets = models.filter((m) => {
      if (filter !== undefined && !filter.includes(m.model)) return false;
      try {
        return catalog.get(m.model).providerType === 'kimi';
      } catch {
        return false;
      }
    });
    assert(targets.length > 0, 'at least one kimi-type model configured');

    for (const m of targets) {
      const model = catalog.get(m.model);
      if (model.baseUrl === undefined) {
        console.log(`[${m.model}] skip tap: model has no resolved baseUrl`);
        continue;
      }
      const upstream = model.baseUrl;
      let tapCount = 0;
      const server = createServer((req, res) => {
        const chunks: Buffer[] = [];
        req.on('data', (chunk: Buffer) => chunks.push(chunk));
        req.on('end', () => {
          void (async () => {
            const raw = Buffer.concat(chunks);
            tapCount += 1;
            console.log(`\n[${m.model}] ── request #${String(tapCount)} β†’ ${upstream}${req.url ?? ''}`);
            for (const line of describeWireBody(raw)) console.log(line);
            const headers = { ...(req.headers as Record<string, string>) };
            delete headers['host'];
            delete headers['content-length'];
            delete headers['connection'];
            const response = await fetch(`${upstream}${req.url ?? ''}`, {
              method: req.method,
              headers,
              body: raw.length > 0 ? raw : undefined,
            });
            const passthrough: Record<string, string> = {};
            response.headers.forEach((value, key) => {
              if (key !== 'content-length' && key !== 'content-encoding' && key !== 'transfer-encoding') {
                passthrough[key] = value;
              }
            });
            res.writeHead(response.status, passthrough);
            res.end(Buffer.from(await response.arrayBuffer()));
          })().catch((error: unknown) => {
            res.writeHead(502);
            res.end(String(error));
          });
        });
      });
      await new Promise<void>((resolve) => {
        server.listen(0, '127.0.0.1', () => resolve());
      });
      const port = (server.address() as AddressInfo).port;

      try {
        const tapped = new ModelRequesterImpl(
          { ...model, baseUrl: `http://127.0.0.1:${String(port)}` },
          registry,
        );
        // One scenario is enough to show the mechanism: click (has arguments).
        const scenario = SCENARIOS[1]!;
        const step1 = await step1Select(tapped, scenario);
        if (step1.kind === 'selected') {
          const step2 = await step2UseLoadedTool(tapped, step1, scenario);
          console.log(
            `\n[${m.model}] outcome: select=[${step1.names.join(', ')}] -> ${step2.kind === 'called' ? `called ${step2.name}(${step2.argumentsJson ?? ''})` : JSON.stringify(step2)}`,
          );
        } else {
          console.log(`\n[${m.model}] step1 outcome: ${JSON.stringify(step1)}`);
        }
      } finally {
        server.closeAllConnections();
        await new Promise<void>((resolve) => {
          server.close(() => resolve());
        });
      }
    }
  } finally {
    app.dispose();
  }
}

// ---------------------------------------------------------------------------

async function main(): Promise<void> {
  await probeWireEncoding();
  if (process.env['KIMI_SELECT_TOOLS_TAP'] === '1') {
    await probeTappedContext();
  } else if (process.env['KIMI_SELECT_TOOLS_SKIP_LIVE'] !== '1') {
    await probeLiveKimiProviders();
  }
  console.log('\nselect-tools: OK');
}

try {
  await main();
} catch (error) {
  console.error(error);
  process.exit(1);
}