@deathbeam/pi-mistral 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +21 -2
- package/index.ts +197 -0
- package/package.json +26 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tomas Slusny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# [@deathbeam/pi-mistral](https://www.npmjs.com/package/@deathbeam/pi-mistral)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Mistral models through the built-in `mistral` provider.
|
|
4
|
+
Set `MISTRAL_API_KEY`.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
# Choose one:
|
|
10
|
+
pi install npm:@deathbeam/pi-mistral # this package
|
|
11
|
+
pi install git:github.com/deathbeam/pi-packages # all my packages
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Development
|
|
15
|
+
|
|
16
|
+
From the repo root:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install
|
|
20
|
+
npm test -- packages/pi-mistral
|
|
21
|
+
pi install ./packages/pi-mistral
|
|
22
|
+
```
|
package/index.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { arch, platform, release, version } from "node:os";
|
|
5
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
|
|
7
|
+
const CLI_NAME = "vibe_rs";
|
|
8
|
+
const CLI_VERSION = "3.2.0";
|
|
9
|
+
const CALL_SOURCE = "vibe_code";
|
|
10
|
+
const HOST_KIND = "local";
|
|
11
|
+
const MAIN_CALL = "main_call";
|
|
12
|
+
const SECONDARY_CALL = "secondary_call";
|
|
13
|
+
const PLATFORM_IDS: Record<string, string> = {
|
|
14
|
+
win32: "windows",
|
|
15
|
+
darwin: "darwin",
|
|
16
|
+
linux: "linux",
|
|
17
|
+
freebsd: "freebsd",
|
|
18
|
+
openbsd: "openbsd",
|
|
19
|
+
netbsd: "netbsd",
|
|
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"];
|
|
41
|
+
|
|
42
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
43
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// HTTP header names are case-insensitive, but a duplicate key would send both values.
|
|
47
|
+
function setHeader(headers: Record<string, string | null>, name: string, value: string): void {
|
|
48
|
+
const lower = name.toLowerCase();
|
|
49
|
+
for (const key of Object.keys(headers)) {
|
|
50
|
+
if (key !== name && key.toLowerCase() === lower) delete headers[key];
|
|
51
|
+
}
|
|
52
|
+
headers[name] = value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function newSessionId(): string {
|
|
56
|
+
const head = randomBytes(10).toString("hex");
|
|
57
|
+
const tail = randomBytes(6).toString("hex");
|
|
58
|
+
return `${head.slice(0, 8)}-${head.slice(8, 12)}-${head.slice(12, 16)}-${head.slice(16, 20)}-${tail}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function machine(): string {
|
|
62
|
+
switch (arch()) {
|
|
63
|
+
case "x64":
|
|
64
|
+
return platform() === "win32" ? "amd64" : "x86_64";
|
|
65
|
+
case "arm64":
|
|
66
|
+
return platform() === "linux" ? "aarch64" : "arm64";
|
|
67
|
+
default:
|
|
68
|
+
return arch();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
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> {
|
|
151
|
+
return {
|
|
152
|
+
os: PLATFORM_IDS[platform()] ?? platform(),
|
|
153
|
+
arch: machine(),
|
|
154
|
+
os_version: OS_VERSION,
|
|
155
|
+
version: CLI_VERSION,
|
|
156
|
+
session_id,
|
|
157
|
+
message_id: randomUUID(),
|
|
158
|
+
agent_entrypoint: "cli",
|
|
159
|
+
agent_version: CLI_VERSION,
|
|
160
|
+
client_name: CLI_NAME,
|
|
161
|
+
client_version: CLI_VERSION,
|
|
162
|
+
terminal_emulator: TERMINAL_EMULATOR,
|
|
163
|
+
call_type,
|
|
164
|
+
call_source: CALL_SOURCE,
|
|
165
|
+
host_kind: HOST_KIND,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export default function (pi: ExtensionAPI): void {
|
|
170
|
+
const sessions = new Map<string, string>();
|
|
171
|
+
|
|
172
|
+
const affinity = (ctx: ExtensionContext): string => {
|
|
173
|
+
const piSession = ctx.sessionManager.getSessionId();
|
|
174
|
+
let session = sessions.get(piSession);
|
|
175
|
+
if (!session) {
|
|
176
|
+
session = newSessionId();
|
|
177
|
+
sessions.set(piSession, session);
|
|
178
|
+
}
|
|
179
|
+
return session;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
pi.on("before_provider_headers", (event, ctx) => {
|
|
183
|
+
const model = ctx.model;
|
|
184
|
+
if (!model || model.provider !== "mistral") return;
|
|
185
|
+
|
|
186
|
+
setHeader(event.headers, "User-Agent", `mistral-client-python/Mistral-Vibe/${CLI_VERSION}`);
|
|
187
|
+
setHeader(event.headers, "x-affinity", affinity(ctx));
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
pi.on("before_provider_request", (event, ctx) => {
|
|
191
|
+
const model = ctx.model;
|
|
192
|
+
if (!model || model.provider !== "mistral") return;
|
|
193
|
+
if (!isObject(event.payload)) return;
|
|
194
|
+
|
|
195
|
+
return { ...event.payload, metadata: requestMetadata(affinity(ctx), callType(event.payload)) };
|
|
196
|
+
});
|
|
197
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deathbeam/pi-mistral",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Mistral models through the built-in mistral provider for Pi",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/deathbeam/pi-packages.git",
|
|
10
|
+
"directory": "packages/pi-mistral"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=22.19.0"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.ts",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"pi": {
|
|
21
|
+
"extensions": [
|
|
22
|
+
"./index.ts"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
27
|
+
}
|
|
28
|
+
}
|