@deathbeam/pi-mistral 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.ts +109 -8
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -1,11 +1,15 @@
1
+ import { execFileSync } from "node:child_process";
1
2
  import { randomBytes, randomUUID } from "node:crypto";
2
- import { arch, platform, release } from "node:os";
3
+ import { readFileSync } from "node:fs";
4
+ import { arch, platform, release, version } from "node:os";
3
5
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
4
6
 
5
7
  const CLI_NAME = "vibe_rs";
6
- const CLI_VERSION = "3.1.4";
8
+ const CLI_VERSION = "3.2.0";
7
9
  const CALL_SOURCE = "vibe_code";
8
10
  const HOST_KIND = "local";
11
+ const MAIN_CALL = "main_call";
12
+ const SECONDARY_CALL = "secondary_call";
9
13
  const PLATFORM_IDS: Record<string, string> = {
10
14
  win32: "windows",
11
15
  darwin: "darwin",
@@ -14,6 +18,26 @@ const PLATFORM_IDS: Record<string, string> = {
14
18
  openbsd: "openbsd",
15
19
  netbsd: "netbsd",
16
20
  };
21
+ const TERM_PROGRAM_TERMINALS: Record<string, string> = {
22
+ apple_terminal: "apple_terminal",
23
+ "iterm.app": "iterm2",
24
+ wezterm: "wezterm",
25
+ ghostty: "ghostty",
26
+ alacritty: "alacritty",
27
+ kitty: "kitty",
28
+ hyper: "hyper",
29
+ };
30
+ const ENV_MARKERS: Array<[string, string]> = [
31
+ ["WEZTERM_PANE", "wezterm"],
32
+ ["GHOSTTY_RESOURCES_DIR", "ghostty"],
33
+ ["KITTY_WINDOW_ID", "kitty"],
34
+ ["ALACRITTY_SOCKET", "alacritty"],
35
+ ["ALACRITTY_LOG", "alacritty"],
36
+ ["WT_SESSION", "windows_terminal"],
37
+ ["WT_PROFILE_ID", "windows_terminal"],
38
+ ];
39
+ const VSCODE_PATHS = ["VSCODE_GIT_ASKPASS_NODE", "VSCODE_GIT_ASKPASS_MAIN", "VSCODE_IPC_HOOK_CLI", "VSCODE_NLS_CONFIG"];
40
+ const OS_RELEASE_PATHS = ["/etc/os-release", "/usr/lib/os-release"];
17
41
 
18
42
  function isObject(value: unknown): value is Record<string, unknown> {
19
43
  return typeof value === "object" && value !== null && !Array.isArray(value);
@@ -45,12 +69,89 @@ function machine(): string {
45
69
  }
46
70
  }
47
71
 
48
- // shortcut: os_version is the kernel release, use the distro VERSION_ID if it ever matters.
49
- function requestMetadata(session_id: string): Record<string, string> {
72
+ function envValue(name: string): string {
73
+ return process.env[name] ?? "";
74
+ }
75
+
76
+ /** Vibe's `terminal_detect::detect_from`: same variables, same order, same labels. */
77
+ export function terminalEmulator(): string {
78
+ const termProgram = envValue("TERM_PROGRAM").toLowerCase();
79
+ if (termProgram === "vscode") {
80
+ if (VSCODE_PATHS.some((name) => envValue(name).toLowerCase().includes("cursor"))) return "cursor";
81
+ return envValue("TERM_PROGRAM_VERSION").toLowerCase().endsWith("-insider") ? "vscode_insiders" : "vscode";
82
+ }
83
+ const fromProgram = TERM_PROGRAM_TERMINALS[termProgram];
84
+ if (fromProgram) return fromProgram;
85
+ for (const [name, terminal] of ENV_MARKERS) {
86
+ if (envValue(name)) return terminal;
87
+ }
88
+ if (envValue("TERMINAL_EMULATOR").toLowerCase().includes("jetbrains")) return "jetbrains";
89
+ return "unknown";
90
+ }
91
+
92
+ /** Vibe's `get_platform_version` for Linux: `VERSION_ID`, then `VERSION`, from os-release. */
93
+ export function parseOsRelease(text: string): string | undefined {
94
+ const fields: Record<string, string> = {};
95
+ for (const line of text.split("\n")) {
96
+ const match = /^([A-Z0-9_]+)=(.*)$/.exec(line.trim());
97
+ if (match) fields[match[1]!] = match[2]!.replace(/^["']|["']$/g, "");
98
+ }
99
+ return fields["VERSION_ID"] || fields["VERSION"] || undefined;
100
+ }
101
+
102
+ function linuxOsVersion(): string | undefined {
103
+ for (const path of OS_RELEASE_PATHS) {
104
+ try {
105
+ return parseOsRelease(readFileSync(path, "utf8"));
106
+ } catch {
107
+ // Python tries the next os-release location too.
108
+ }
109
+ }
110
+ return undefined;
111
+ }
112
+
113
+ function macOsVersion(): string | undefined {
114
+ try {
115
+ return execFileSync("sw_vers", ["-productVersion"], { encoding: "utf8" }).trim() || undefined;
116
+ } catch {
117
+ return undefined;
118
+ }
119
+ }
120
+
121
+ function osVersion(): string {
122
+ switch (PLATFORM_IDS[platform()] ?? platform()) {
123
+ case "darwin":
124
+ return macOsVersion() || release();
125
+ case "windows":
126
+ return version() || release();
127
+ case "linux":
128
+ return linuxOsVersion() || release();
129
+ default:
130
+ return release() || version();
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Vibe marks a turn's first completion `main_call` and every later one `secondary_call`
136
+ * (`request_call_type`). Pi keeps no iteration counter, but a follow-up always carries the
137
+ * previous iteration's tool results.
138
+ */
139
+ // shortcut: compaction, branch and bug-report summaries pass no onPayload, so they skip `before_provider_request` and send no metadata (headers still apply); a call after injected feedback (steering, output-limit retry) reads as main_call.
140
+ export function callType(payload: Record<string, unknown>): string {
141
+ const messages = Array.isArray(payload["messages"]) ? payload["messages"] : [];
142
+ const last = messages[messages.length - 1];
143
+ return isObject(last) && last["role"] === "tool" ? SECONDARY_CALL : MAIN_CALL;
144
+ }
145
+
146
+ // The OS version and terminal do not change while pi runs.
147
+ const OS_VERSION = osVersion();
148
+ const TERMINAL_EMULATOR = terminalEmulator();
149
+
150
+ function requestMetadata(session_id: string, call_type: string): Record<string, string> {
50
151
  return {
51
152
  os: PLATFORM_IDS[platform()] ?? platform(),
52
153
  arch: machine(),
53
- os_version: release(),
154
+ os_version: OS_VERSION,
54
155
  version: CLI_VERSION,
55
156
  session_id,
56
157
  message_id: randomUUID(),
@@ -58,8 +159,8 @@ function requestMetadata(session_id: string): Record<string, string> {
58
159
  agent_version: CLI_VERSION,
59
160
  client_name: CLI_NAME,
60
161
  client_version: CLI_VERSION,
61
- terminal_emulator: "unknown",
62
- call_type: "main_call",
162
+ terminal_emulator: TERMINAL_EMULATOR,
163
+ call_type,
63
164
  call_source: CALL_SOURCE,
64
165
  host_kind: HOST_KIND,
65
166
  };
@@ -91,6 +192,6 @@ export default function (pi: ExtensionAPI): void {
91
192
  if (!model || model.provider !== "mistral") return;
92
193
  if (!isObject(event.payload)) return;
93
194
 
94
- return { ...event.payload, metadata: requestMetadata(affinity(ctx)) };
195
+ return { ...event.payload, metadata: requestMetadata(affinity(ctx), callType(event.payload)) };
95
196
  });
96
197
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deathbeam/pi-mistral",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Mistral models through the built-in mistral provider for Pi",
5
5
  "type": "module",
6
6
  "license": "MIT",