File size: 2,771 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
/**
 * Minimal end-to-end example driving an in-process engine with klient's
 * `global` facade over the memory transport (calls and events never leave
 * the process — same facade either way).
 *
 * Run it (the engine sources need the decorators tsconfig + raw-text loader):
 *   pnpm -C packages/klient exec tsx --tsconfig ./tsconfig.examples.json \
 *     --import ../../build/register-raw-text-loader.mjs examples/basic.ts
 */
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

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


import { bootstrap, logSeed, resolveLoggingConfig } from '@moonshot-ai/agent-core-v2';
import { createKlient } from '@moonshot-ai/klient/memory';

async function main(): Promise<void> {
  const homeDir = await mkdtemp(join(tmpdir(), 'klient-basic-'));
  const { app } = bootstrap({ homeDir, clientIdentity: EXAMPLE_CLIENT_IDENTITY }, [
    ...logSeed(resolveLoggingConfig({ homeDir, env: process.env })),
  ]);
  try {
    const klient = createKlient({ scope: app });

    // 1) Aggregated host snapshot.
    const env = await klient.global.env();
    console.log('[env]      platform/homeDir   ->', env.platform, env.homeDir);

    // 2) Read models.
    const sessions = await klient.global.sessions.list({});
    console.log('[sessions] list               ->', sessions.items.length, 'sessions');
    const workspaces = await klient.global.workspaces.list();
    console.log('[workspaces] list             ->', workspaces.length, 'workspaces');
    const providers = await klient.global.kosong.listProviders();
    console.log('[providers] list              ->', providers.length, 'providers');

    // 3) Events — klient-level forwarding (no onDid*/onWill* in sight).
    const sub = klient.events.on('kosong.providers.changed', (event) => {
      console.log(
        '[event]    kosong.providers.changed  -> +%s -%s ~%s',
        event.added,
        event.removed,
        event.changed,
      );
    });
    await klient.global.kosong.addProvider('__klient_example__', {
      type: 'openai',
      auth: { method: 'api-key', apiKey: 'example-key' },
    });
    await klient.global.kosong.removeProvider('__klient_example__');
    sub.dispose();

    // 4) Error path — a missing plugin surfaces an error.
    try {
      await klient.global.plugins.info('__definitely_missing__');
    } catch (error) {
      const e = error as { name: string; code?: number };
      console.log('[error]    plugins.info        ->', e.name, e.code);
    }

    await klient.close();
  } finally {
    app.dispose();
    await rm(homeDir, { recursive: true, force: true });
  }
}

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