kimi-code/packages/klient/examples/inspect-init.ts
haozhe.yang 2c5a4a3799 feat(api-v2): add klient SDK and reflection-based channel registry
- replace per-method actionMap (resource:action) with a channel registry: each Service registers once by decorator id and all methods are invoked by reflection
- routes move from /api/v2/:sa to /api/v2/:service/:method across HTTP routes and the WS protocol
- add @moonshot-ai/klient: typed core/session/agent client over the HTTP channel that reuses agent-core-v2 service interfaces
- register klient in flake.nix and pnpm-lock; refresh kap-server tests, e2e, and the apiSurface snapshot
2026-07-11 12:33:48 +08:00

50 lines
2.2 KiB
TypeScript

/**
* Diagnostic: inspect a session after a `/init` attempt — session agents, and
* for each agent the permission mode / active turn / context tail. Uses only
* Services exposed on the wire. Usage: `pnpm exec tsx examples/inspect-init.ts <sessionId>`
*/
import { readFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { Klient } from '@moonshot-ai/klient';
import { IAgentContextMemoryService } from '@moonshot-ai/agent-core-v2/agent/contextMemory/contextMemory';
import { IAgentPermissionModeService } from '@moonshot-ai/agent-core-v2/agent/permissionMode/permissionMode';
import { ISessionMetadata } from '@moonshot-ai/agent-core-v2/session/sessionMetadata/sessionMetadata';
const home = process.env['KIMI_CODE_HOME'] ?? join(homedir(), '.kimi-code');
const baseUrl = (process.env['KIMI_SERVER_URL'] ?? 'http://127.0.0.1:58627').replace(/\/$/, '');
const sid = process.argv[2];
if (sid === undefined) {
console.error('usage: inspect-init.ts <sessionId>');
process.exit(1);
}
const token = (await readFile(join(home, 'server.token'), 'utf8')).trim();
const client = new Klient({ url: baseUrl, token });
const meta = await client.session(sid).service(ISessionMetadata).read();
const agentIds = Object.keys(meta.agents ?? {});
console.log('agents in metadata:', agentIds);
for (const agentId of ['main', ...agentIds.filter((id) => id !== 'main')]) {
const agent = client.session(sid).agent(agentId);
try {
const mode = await agent.service(IAgentPermissionModeService).mode();
const context = await agent.service(IAgentContextMemoryService).get();
console.log(`\n== agent ${agentId} == mode=${mode} messages=${context.length}`);
for (const last of context.slice(-2)) {
const text = last.content
.map((part) => (part.type === 'text' ? part.text : `[${part.type}]`))
.join(' ')
.slice(0, 300);
const calls = (last.toolCalls ?? []).map((c) => c.name).join(',');
console.log(
` - role=${last.role} origin=${JSON.stringify(last.origin ?? null)}${calls.length > 0 ? ` toolCalls=[${calls}]` : ''} :: ${text}`,
);
}
} catch (error) {
console.log(`\n== agent ${agentId} == not reachable (${error instanceof Error ? error.message : error})`);
}
}