| index 869786f..72f40f7 100644 | | index 869786f..72f40f7 100644 |
| --- a/src/plugin_sglang.c | | --- a/src/plugin_sglang.c |
| +++ b/src/plugin_sglang.c | | +++ b/src/plugin_sglang.c |
| @@ -47,7 +47,7 @@ | | @@ -47,7 +47,7 @@ |
| 47 | | 47 | |
| 48 | #include "bot_theme_plugin.h" | 48 | #include "bot_theme_plugin.h" |
| 49 | | 49 | |
| 50 | -#define PLUGIN_VERSION "1.0.0" | 50 | +#define PLUGIN_VERSION "1.0.1" |
| 51 | #define CACHE_SECS 4 | 51 | #define CACHE_SECS 4 |
| 52 | #define CACHE_NAME "sglang_metrics.json" | 52 | #define CACHE_NAME "sglang_metrics.json" |
| 53 | #define MAX_SESSIONS 128 | 53 | #define MAX_SESSIONS 128 |
| @@ -415,6 +415,55 @@ static int snapshot_get(const char *path, const char *key, char *out, size_t out | | @@ -415,6 +415,55 @@ static int snapshot_get(const char *path, const char *key, char *out, size_t out |
| 415 | return found; | 415 | return found; |
| 416 | } | 416 | } |
| 417 | | 417 | |
| | 418 | +static int resolve_session_url(const char *sdir, char *out, size_t outsz) |
| | 419 | +{ |
| | 420 | + char snap[1400], ibpath[1400], prof[160] = {0}; |
| | 421 | + char *ib; |
| | 422 | + size_t iblen = 0; |
| | 423 | + if (out && outsz) out[0] = '\0'; |
| | 424 | + if (!sdir || !*sdir) return 0; |
| | 425 | + |
| | 426 | + snprintf(snap, sizeof snap, "%.1279s%cconfig-snapshot.txt", sdir, SX_SEP); |
| | 427 | + if (snapshot_get(snap, "OPENAI_BASE_URL", out, outsz) && out[0]) return 1; |
| | 428 | + |
| | 429 | + /* Fallback: named profile recorded in infobox.json or config-snapshot.txt */ |
| | 430 | + snprintf(ibpath, sizeof ibpath, "%.1279s%cinfobox.json", sdir, SX_SEP); |
| | 431 | + ib = read_file_all(ibpath, &iblen); |
| | 432 | + if (ib && iblen > 2) { |
| | 433 | + json_extract_str_after(ib, ib + iblen, "profile", prof, sizeof prof); |
| | 434 | + } |
| | 435 | + free(ib); |
| | 436 | + if (!prof[0]) snapshot_get(snap, "ACTIVE_PROFILE", prof, sizeof prof); |
| | 437 | + if (!prof[0] || !safe_id(prof)) return 0; |
| | 438 | + |
| | 439 | + { |
| | 440 | + char ppath[1024]; |
| | 441 | +#ifdef _WIN32 |
| | 442 | + const char *up = getenv("USERPROFILE"); |
| | 443 | + const char *lad = getenv("LOCALAPPDATA"); |
| | 444 | + if (up && *up) { |
| | 445 | + snprintf(ppath, sizeof ppath, "%.500s\\.claude\\scorpiox-env\\%.159s.txt", up, prof); |
| | 446 | + if (snapshot_get(ppath, "OPENAI_BASE_URL", out, outsz) && out[0]) return 1; |
| | 447 | + } |
| | 448 | + if (lad && *lad) { |
| | 449 | + snprintf(ppath, sizeof ppath, "%.500s\\scorpiox\\scorpiox-env\\%.159s.txt", lad, prof); |
| | 450 | + if (snapshot_get(ppath, "OPENAI_BASE_URL", out, outsz) && out[0]) return 1; |
| | 451 | + } |
| | 452 | +#else |
| | 453 | + const char *home = getenv("HOME"); |
| | 454 | + if (home && *home) { |
| | 455 | + snprintf(ppath, sizeof ppath, "%.500s/.claude/scorpiox-env/%.159s.txt", home, prof); |
| | 456 | + if (snapshot_get(ppath, "OPENAI_BASE_URL", out, outsz) && out[0]) return 1; |
| | 457 | + snprintf(ppath, sizeof ppath, "%.500s/.scorpiox/scorpiox-env/%.159s.txt", home, prof); |
| | 458 | + if (snapshot_get(ppath, "OPENAI_BASE_URL", out, outsz) && out[0]) return 1; |
| | 459 | + } |
| | 460 | + snprintf(ppath, sizeof ppath, "/usr/local/bin/scorpiox-env/%.159s.txt", prof); |
| | 461 | + if (snapshot_get(ppath, "OPENAI_BASE_URL", out, outsz) && out[0]) return 1; |
| | 462 | +#endif |
| | 463 | + } |
| | 464 | + return 0; |
| | 465 | +} |
| | 466 | + |
| 418 | static int pick_newest_session(const char *sessions_dir, char *sid_out, size_t sidsz) | 467 | static int pick_newest_session(const char *sessions_dir, char *sid_out, size_t sidsz) |
| 419 | { | 468 | { |
| 420 | time_t best_t = 0; | 469 | time_t best_t = 0; |
| @@ -423,18 +472,21 @@ static int pick_newest_session(const char *sessions_dir, char *sid_out, size_t s | | @@ -423,18 +472,21 @@ static int pick_newest_session(const char *sessions_dir, char *sid_out, size_t s |
| 423 | char pat[1200]; | 472 | char pat[1200]; |
| 424 | WIN32_FIND_DATAA fd; | 473 | WIN32_FIND_DATAA fd; |
| 425 | HANDLE h; | 474 | HANDLE h; |
| 426 | - snprintf(pat, sizeof pat, "%s\\*", sessions_dir); | 475 | + snprintf(pat, sizeof pat, "%.950s\\*", sessions_dir); |
| 427 | h = FindFirstFileA(pat, &fd); | 476 | h = FindFirstFileA(pat, &fd); |
| 428 | if (h == INVALID_HANDLE_VALUE) return 0; | 477 | if (h == INVALID_HANDLE_VALUE) return 0; |
| 429 | do { | 478 | do { |
| 430 | - char probe[1600]; | 479 | + char probe[1400]; |
| 431 | struct stat st; | 480 | struct stat st; |
| 432 | if (fd.cFileName[0] == '.') continue; | 481 | if (fd.cFileName[0] == '.') continue; |
| 433 | if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; | 482 | if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; |
| 434 | - snprintf(probe, sizeof probe, "%s\\%s\\config-snapshot.txt", sessions_dir, fd.cFileName); | 483 | + snprintf(probe, sizeof probe, "%.950s\\%.200s\\infobox.json", sessions_dir, fd.cFileName); |
| 435 | if (stat(probe, &st) != 0) { | 484 | if (stat(probe, &st) != 0) { |
| 436 | - snprintf(probe, sizeof probe, "%s\\%s\\meta.json", sessions_dir, fd.cFileName); | 485 | + snprintf(probe, sizeof probe, "%.950s\\%.200s\\config-snapshot.txt", sessions_dir, fd.cFileName); |
| 437 | - if (stat(probe, &st) != 0) continue; | 486 | + if (stat(probe, &st) != 0) { |
| | 487 | + snprintf(probe, sizeof probe, "%.950s\\%.200s\\meta.json", sessions_dir, fd.cFileName); |
| | 488 | + if (stat(probe, &st) != 0) continue; |
| | 489 | + } |
| 438 | } | 490 | } |
| 439 | if (st.st_mtime >= best_t) { | 491 | if (st.st_mtime >= best_t) { |
| 440 | best_t = st.st_mtime; | 492 | best_t = st.st_mtime; |
| @@ -447,13 +499,16 @@ static int pick_newest_session(const char *sessions_dir, char *sid_out, size_t s | | @@ -447,13 +499,16 @@ static int pick_newest_session(const char *sessions_dir, char *sid_out, size_t s |
| 447 | struct dirent *de; | 499 | struct dirent *de; |
| 448 | if (!d) return 0; | 500 | if (!d) return 0; |
| 449 | while ((de = readdir(d)) != NULL) { | 501 | while ((de = readdir(d)) != NULL) { |
| 450 | - char probe[1600]; | 502 | + char probe[1400]; |
| 451 | struct stat st; | 503 | struct stat st; |
| 452 | if (de->d_name[0] == '.') continue; | 504 | if (de->d_name[0] == '.') continue; |
| 453 | - snprintf(probe, sizeof probe, "%s/%s/config-snapshot.txt", sessions_dir, de->d_name); | 505 | + snprintf(probe, sizeof probe, "%.950s/%.200s/infobox.json", sessions_dir, de->d_name); |
| 454 | if (stat(probe, &st) != 0) { | 506 | if (stat(probe, &st) != 0) { |
| 455 | - snprintf(probe, sizeof probe, "%s/%s/meta.json", sessions_dir, de->d_name); | 507 | + snprintf(probe, sizeof probe, "%.950s/%.200s/config-snapshot.txt", sessions_dir, de->d_name); |
| 456 | - if (stat(probe, &st) != 0) continue; | 508 | + if (stat(probe, &st) != 0) { |
| | 509 | + snprintf(probe, sizeof probe, "%.950s/%.200s/meta.json", sessions_dir, de->d_name); |
| | 510 | + if (stat(probe, &st) != 0) continue; |
| | 511 | + } |
| 457 | } | 512 | } |
| 458 | if (st.st_mtime >= best_t) { | 513 | if (st.st_mtime >= best_t) { |
| 459 | best_t = st.st_mtime; | 514 | best_t = st.st_mtime; |
| @@ -532,10 +587,8 @@ static int list_tmux_sessions(sess_entry_t *out, int max_n) | | @@ -532,10 +587,8 @@ static int list_tmux_sessions(sess_entry_t *out, int max_n) |
| 532 | char sroot[960]; | 587 | char sroot[960]; |
| 533 | snprintf(sroot, sizeof sroot, "%s%c.scorpiox%csessions", e->worktree, SX_SEP, SX_SEP); | 588 | snprintf(sroot, sizeof sroot, "%s%c.scorpiox%csessions", e->worktree, SX_SEP, SX_SEP); |
| 534 | if (pick_newest_session(sroot, e->sid, sizeof e->sid)) { | 589 | if (pick_newest_session(sroot, e->sid, sizeof e->sid)) { |
| 535 | - char snap[1400]; | | |
| 536 | snprintf(e->session_dir, sizeof e->session_dir, "%.959s%c%.159s", sroot, SX_SEP, e->sid); | 590 | snprintf(e->session_dir, sizeof e->session_dir, "%.959s%c%.159s", sroot, SX_SEP, e->sid); |
| 537 | - snprintf(snap, sizeof snap, "%.1279s%cconfig-snapshot.txt", e->session_dir, SX_SEP); | 591 | + resolve_session_url(e->session_dir, e->base_url, sizeof e->base_url); |
| 538 | - snapshot_get(snap, "OPENAI_BASE_URL", e->base_url, sizeof e->base_url); | | |
| 539 | } | 592 | } |
| 540 | } | 593 | } |
| 541 | count++; | 594 | count++; |
| @@ -701,13 +754,11 @@ static void handle_sessions(void) | | @@ -701,13 +754,11 @@ static void handle_sessions(void) |
| 701 | struct dirent *de; | 754 | struct dirent *de; |
| 702 | if (d) { | 755 | if (d) { |
| 703 | while ((de = readdir(d)) != NULL && emitted < MAX_SESSIONS) { | 756 | while ((de = readdir(d)) != NULL && emitted < MAX_SESSIONS) { |
| 704 | - char cand[1024], snap[1200], burl[512] = {0}; | 757 | + char cand[1024], burl[512] = {0}; |
| 705 | if (de->d_name[0] == '.') continue; | 758 | if (de->d_name[0] == '.') continue; |
| 706 | - snprintf(cand, sizeof cand, "%s/%s", roots[0], de->d_name); | 759 | + snprintf(cand, sizeof cand, "%.511s/%.200s", roots[0], de->d_name); |
| 707 | if (!is_dir(cand)) continue; | 760 | if (!is_dir(cand)) continue; |
| 708 | - snprintf(snap, sizeof snap, "%s/config-snapshot.txt", cand); | 761 | + resolve_session_url(cand, burl, sizeof burl); |
| 709 | - snapshot_get(snap, "OPENAI_BASE_URL", burl, sizeof burl); | | |
| 710 | - if (emitted++) fputc(',', stdout); | | |
| 711 | fputs("{\"name\":", stdout); json_print_str(de->d_name); | 762 | fputs("{\"name\":", stdout); json_print_str(de->d_name); |
| 712 | fputs(",\"sid\":", stdout); json_print_str(de->d_name); | 763 | fputs(",\"sid\":", stdout); json_print_str(de->d_name); |
| 713 | fputs(",\"url\":", stdout); json_print_str(burl); | 764 | fputs(",\"url\":", stdout); json_print_str(burl); |
| @@ -773,7 +824,6 @@ static void handle_metrics(const char *qs) | | @@ -773,7 +824,6 @@ static void handle_metrics(const char *qs) |
| 773 | str_copy(base, sizeof base, url_q); | 824 | str_copy(base, sizeof base, url_q); |
| 774 | source = "query"; | 825 | source = "query"; |
| 775 | } else { | 826 | } else { |
| 776 | - char snap[1280]; | | |
| 777 | if (!id[0] || !safe_id(id)) { | 827 | if (!id[0] || !safe_id(id)) { |
| 778 | reply_json_err("400 Bad Request", "select a session or enter an SGLang URL", NULL); | 828 | reply_json_err("400 Bad Request", "select a session or enter an SGLang URL", NULL); |
| 779 | return; | 829 | return; |
| @@ -782,8 +832,7 @@ static void handle_metrics(const char *qs) | | @@ -782,8 +832,7 @@ static void handle_metrics(const char *qs) |
| 782 | reply_json_err("404 Not Found", "session not found on this node", NULL); | 832 | reply_json_err("404 Not Found", "session not found on this node", NULL); |
| 783 | return; | 833 | return; |
| 784 | } | 834 | } |
| 785 | - snprintf(snap, sizeof snap, "%s%cconfig-snapshot.txt", sdir, SX_SEP); | 835 | + if (!resolve_session_url(sdir, base, sizeof base)) { |
| 786 | - if (!snapshot_get(snap, "OPENAI_BASE_URL", base, sizeof base)) { | | |
| 787 | reply_json_err("404 Not Found", "session has no OPENAI_BASE_URL (not an OpenAI-compatible provider)", NULL); | 836 | reply_json_err("404 Not Found", "session has no OPENAI_BASE_URL (not an OpenAI-compatible provider)", NULL); |
| 788 | return; | 837 | return; |
| 789 | } | 838 | } |
| 790 | | 839 | |