File size: 2,381 Bytes
68d7816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { basename, join, relative } from 'pathe';

import type { KimiHostIdentity } from '@moonshot-ai/kimi-code-oauth';
import { LifecycleScope } from '#/app/scopes';
import { ScopeActivation, registerScopedService } from '#/_base/di/scope';

import {
  IBootstrapOptions,
  IBootstrapService,
  type HostArgs,
  type PersistenceScopeName,
} from './bootstrap';

export class BootstrapService implements IBootstrapService {
  declare readonly _serviceBrand: undefined;

  readonly platform: NodeJS.Platform;
  readonly arch: string;
  readonly cwd: string;
  readonly osHomeDir: string;
  readonly homeDir: string;
  readonly configPath: string;
  readonly clientIdentity: KimiHostIdentity;
  readonly args: HostArgs;
  readonly sessionsDir: string;
  readonly blobsDir: string;
  readonly storeDir: string;
  readonly cacheDir: string;
  readonly logsDir: string;
  readonly configKey: string;

  private readonly env: NodeJS.ProcessEnv;
  private readonly scopes: Readonly<Record<PersistenceScopeName, string>>;

  constructor(@IBootstrapOptions options: IBootstrapOptions) {
    this.platform = options.platform;
    this.arch = options.arch;
    this.cwd = options.cwd;
    this.osHomeDir = options.osHomeDir;
    this.env = options.env;
    this.homeDir = options.homeDir;
    this.configPath = options.configPath;
    this.clientIdentity = options.clientIdentity;
    this.args = options.args;
    this.sessionsDir = join(options.homeDir, 'sessions');
    this.blobsDir = join(options.homeDir, 'blobs');
    this.storeDir = join(options.homeDir, 'store');
    this.cacheDir = join(options.homeDir, 'cache');
    this.logsDir = join(options.homeDir, 'logs');
    this.configKey = basename(options.configPath);
    this.scopes = {
      config: '',
      sessions: relative(options.homeDir, join(options.homeDir, 'sessions')),
      blobs: relative(options.homeDir, this.blobsDir),
      store: relative(options.homeDir, this.storeDir),
      logs: relative(options.homeDir, this.logsDir),
      cache: relative(options.homeDir, this.cacheDir),
      credentials: 'credentials',
    };
  }

  getEnv(name: string): string | undefined {
    return this.env[name];
  }

  scope(name: PersistenceScopeName): string {
    return this.scopes[name];
  }
}

registerScopedService(LifecycleScope.App, IBootstrapService, BootstrapService, ScopeActivation.OnScopeCreated, 'bootstrap');