Files
magpie/internal/sessions/kimi_work.go
T
yetone 18b46f5c25 sessions: the Kimi desktop app's Work calls reach Usage
CNSA0422 on X: magpie didn't count local usage from the Kimi desktop
app. Its Work agent is Daimon, which runs the Kimi Code runtime it
vendors with its own home under the app's share folder, so its calls
are written as Kimi Code's usage.record lines in a folder magpie never
looked in.

Read from Kimi 3.2.27's own bundle (com.moonshot.kimichat, Daimon
1.0.10, Kimi Code runtime 0.5.1): the app passes KIMI_SHARE_DIR, which
is <userData>/daimon-share or the absolute shareDir in
<userData>/daimon-storage.json; Daimon alone falls back to ~/.kimi.
The sessions are <share>/daimon/runtime/kimi-code/home/sessions/<slug>/
<id>/agents/<agent>/wire.jsonl. userData is kimi-desktop under
~/Library/Application Support, %APPDATA% or ~/.config.

- kimi_work.go finds the shares and adds their wire files to Kimi
  Code's ("kimi"), as MiniMax Code Desktop's count as MiniMax Code's.
  Found() lists kimi when only Work sessions exist.
- A record's model is Daimon's alias (daimon-kimi-code). It is read
  as the alias's model in the runtime's config.toml (k2p6-agent).
- magpie's kimi-desktop plugin points the alias's provider at a bridge
  on 127.0.0.1 that forwards to the gateway, which logs those calls.
  A call whose alias's base_url is a loopback address is skipped, so
  it is not counted twice. config.toml is the current setting only,
  so history read for the first time is judged by it.
- docs/subsystems/gateway-sessions.md says so.

Run (mote): go test ./internal/sessions -count=1, ok;
go vet ./internal/sessions for darwin, windows, linux -tags nogui, ok.
Windows (xiaomi-gpu, win-run): go test ./internal/sessions
-run 'KimiWork|Loopback|KimiCode', ok. gofmt clean.

Broken by hand: kimiCallFiles without the Work files made
TestKimiWorkCalls red (no calls), and so did dropping the loopback skip
(the bridged call counted). Restored, green.

Not tried: a real Kimi Work session. This computer has no Kimi desktop
data, so the fixture's records are the Kimi Code format the bundle's
UsageRecord writes and its config.toml is what modelToToml builds. The
Windows installer was not unpacked: the Windows path is Electron's
userData as the bundle's code reads it. k2p6-agent has no price in the
catalog, so these calls show tokens and no cost.
2026-11-13 15:12:46 +08:00

175 lines
5.5 KiB
Go

package sessions
import (
"encoding/json"
"net"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/pelletier/go-toml/v2"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/filememo"
)
// Kimi's desktop app's Work (CNSA0422 on X: its calls never reached
// Usage). Read from Kimi 3.2.27's own bundle (com.moonshot.kimichat), whose
// Work agent is Daimon 1.0.10 running the Kimi Code runtime 0.5.1 it
// vendors. The app starts Daimon with KIMI_SHARE_DIR set to its share
// folder: <userData>/daimon-share, or the absolute shareDir the app keeps
// in <userData>/daimon-storage.json when Settings moved it. userData is
// Electron's, kimi-desktop under ~/Library/Application Support on a Mac and
// %APPDATA% on Windows (and ~/.config on Linux). Daimon run on its own,
// without the variable, shares ~/.kimi.
//
// Daimon keeps its state in <share>/daimon and gives Kimi Code its own
// home, <share>/daimon/runtime/kimi-code/home, so a Work session is a Kimi
// Code session there: sessions/<slug>/<id>/agents/<agent>/wire.jsonl with
// the same usage.record lines calls_kimi.go reads. Its calls are counted as
// Kimi Code's ("kimi"), as MiniMax Code Desktop's are as MiniMax Code's.
//
// Two things differ. A record's model is Daimon's alias ("daimon-kimi-code"
// by default), the key of a [models."<alias>"] table in the config.toml
// Daimon writes for the runtime beside home; its model (k2p6-agent on Kimi's
// coding endpoint) is the call's model. And magpie's kimi-desktop
// agent plugin points the alias's provider at a bridge on this computer
// that forwards to the gateway, which logs those calls itself: a call made
// while that config.toml's base_url is a loopback address is the
// gateway's. config.toml is only the current setting, so history read for
// the first time is judged by it.
// kimiWorkShares are the share folders Kimi's Work may keep sessions in,
// each once.
func kimiWorkShares() []string {
home, _ := os.UserHomeDir()
return kimiWorkSharesIn(home, runtime.GOOS)
}
func kimiWorkSharesIn(home, goos string) []string {
var out []string
add := func(d string) {
if d == "" {
return
}
d = filepath.Clean(d)
for _, x := range out {
if x == d {
return
}
}
out = append(out, d)
}
if ud := kimiDesktopUserData(home, goos); ud != "" {
var s struct {
ShareDir string `json:"shareDir"`
}
if b, err := os.ReadFile(filepath.Join(ud, "daimon-storage.json")); err == nil && json.Unmarshal(b, &s) == nil && filepath.IsAbs(s.ShareDir) {
add(s.ShareDir)
}
add(filepath.Join(ud, "daimon-share"))
}
add(filepath.Join(home, ".kimi"))
return out
}
// kimiDesktopUserData is the Kimi desktop app's Electron userData.
func kimiDesktopUserData(home, goos string) string {
switch goos {
case "darwin":
return filepath.Join(home, "Library", "Application Support", "kimi-desktop")
case "windows":
base := appdir.Getenv("APPDATA")
if base == "" {
base = filepath.Join(home, "AppData", "Roaming")
}
return filepath.Join(base, "kimi-desktop")
default:
base := appdir.Getenv("XDG_CONFIG_HOME")
if base == "" {
base = filepath.Join(home, ".config")
}
return filepath.Join(base, "kimi-desktop")
}
}
// kimiWorkRuntime is the Kimi Code runtime folder of a share.
func kimiWorkRuntime(share string) string {
return filepath.Join(share, "daimon", "runtime", "kimi-code")
}
// kimiWorkCallFiles are the wire files of every Work session.
func kimiWorkCallFiles() []file {
var out []file
for _, s := range kimiWorkShares() {
out = append(out, kimiFilesIn(filepath.Join(kimiWorkRuntime(s), "home", "sessions"))...)
}
return out
}
// kimiWorkRoot is the runtime folder of a Work session's wire file
// (<root>/home/sessions/<slug>/<id>/agents/<agent>/wire.jsonl), or "" for
// a Kimi Code session of its own.
func kimiWorkRoot(path string) string {
d := path
for range 5 {
d = filepath.Dir(d)
}
if filepath.Base(d) != "sessions" || filepath.Base(filepath.Dir(d)) != "home" {
return ""
}
root := filepath.Dir(filepath.Dir(d))
if filepath.Base(root) != "kimi-code" || filepath.Base(filepath.Dir(root)) != "runtime" ||
filepath.Base(filepath.Dir(filepath.Dir(root))) != "daimon" {
return ""
}
return root
}
// kimiWorkModel is one alias of Daimon's config.toml.
type kimiWorkModel struct {
model string
gateway bool // its provider's base_url is on this computer
}
// kimiWorkModels are the aliases of a runtime's config.toml, read again
// only when it changes. What it gives is shared and must not be changed.
func kimiWorkModels(root string) map[string]kimiWorkModel {
m, _ := filememo.Read("sessions kimi work config", filepath.Join(root, "config.toml"), func(b []byte) (map[string]kimiWorkModel, error) {
var c struct {
Providers map[string]struct {
BaseURL string `toml:"base_url"`
} `toml:"providers"`
Models map[string]struct {
Provider string `toml:"provider"`
Model string `toml:"model"`
} `toml:"models"`
}
if err := toml.Unmarshal(b, &c); err != nil {
return nil, err
}
out := map[string]kimiWorkModel{}
for alias, t := range c.Models {
out[alias] = kimiWorkModel{model: t.Model, gateway: loopbackURL(c.Providers[t.Provider].BaseURL)}
}
return out, nil
})
return m
}
// loopbackURL tells whether a base URL is on this computer.
func loopbackURL(s string) bool {
u, err := url.Parse(strings.TrimSpace(s))
if err != nil {
return false
}
h := u.Hostname()
if strings.EqualFold(h, "localhost") {
return true
}
ip := net.ParseIP(h)
return ip != nil && ip.IsLoopback()
}