@theopenbee/engine-pi 0.1.1 → 0.2.1

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/README.md CHANGED
@@ -48,6 +48,10 @@ Then enable the plugin, and pick `pi` as the default engine or per worker.
48
48
 
49
49
  The plugin checks the setting when it is enabled: it runs `pi --version` and shows the version, or the reason it failed (for example `pi not found on PATH`).
50
50
 
51
+ ## Model
52
+
53
+ The worker's **Model** field is passed to pi as `--model <model>`, before the extra args. pi accepts `provider/id`, for example `deepseek/deepseek-flash`. The suggested models come from `pi --list-models`, as `provider/model`. pi only lists providers it has a login or key for, so set the provider key in **Env** first. If the engine args already set a model with `--model`, they win and the Model field is ignored. Without a model, pi uses its own default.
54
+
51
55
  ## Sessions
52
56
 
53
57
  Every conversation maps to one pi session file:
@@ -61,7 +65,7 @@ This is the plugin's `state_dir/sessions`. openbee never deletes it, so sessions
61
65
  ## How it runs pi
62
66
 
63
67
  ```
64
- pi --mode json --session <state_dir>/sessions/<session_id>.jsonl -p <prompt> [-e <package>/assets/openbee-mcp.js] [extra args]
68
+ pi --mode json --session <state_dir>/sessions/<session_id>.jsonl -p <prompt> [-e <package>/assets/openbee-mcp.js] [--model <model>] [extra args]
65
69
  ```
66
70
 
67
71
  - When openbee provides its MCP server, the plugin loads the bundled `assets/openbee-mcp.js` extension and sets `OPENBEE_MCP_URL`. The API key reaches pi through the `OPENBEE_API_KEY` environment variable, never through the command line.
package/dist/pi.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { Engine, TokenUsage } from "@theopenbee/engine-sdk";
1
+ import type { Engine, ModelInfo, TokenUsage } from "@theopenbee/engine-sdk";
2
+ export declare function modelArgs(model: string | undefined, extraArgs: string[]): string[];
2
3
  export declare function buildArgs(prompt: string, sessionPath: string, extraArgs: string[]): string[];
3
4
  export declare function isStreamingDelta(line: string): boolean;
4
5
  export declare function stripThinkingSignature(line: string): string;
@@ -6,4 +7,5 @@ export declare function extractResultFromLog(logPath: string): string;
6
7
  export declare function checkAgentError(logPath: string): string;
7
8
  export declare function extractPiError(stderr: string, fallback: string): string;
8
9
  export declare function collectUsage(sessionsDir: string, sessionIds: string[]): Promise<Record<string, TokenUsage[]>>;
10
+ export declare function parseModels(output: string): ModelInfo[];
9
11
  export declare const engine: Engine;
package/dist/pi.js CHANGED
@@ -1,9 +1,12 @@
1
+ import { execFile } from "node:child_process";
1
2
  import { createReadStream, mkdirSync, readFileSync, readdirSync, statSync } from "node:fs";
2
3
  import { basename, join } from "node:path";
3
4
  import { createInterface } from "node:readline";
4
5
  import { fileURLToPath } from "node:url";
5
- import { cliVersion, findCli, spawnCli } from "@theopenbee/engine-sdk";
6
+ import { cliVersion, findCli, log, spawnCli } from "@theopenbee/engine-sdk";
6
7
  const EXTENSION_PATH = fileURLToPath(new URL("../assets/openbee-mcp.js", import.meta.url));
8
+ const MODELS_TIMEOUT_MS = 20_000;
9
+ const MODELS_MAX_BUFFER = 64 * 1024 * 1024;
7
10
  function isObject(value) {
8
11
  return typeof value === "object" && value !== null && !Array.isArray(value);
9
12
  }
@@ -14,6 +17,15 @@ function isMissing(err) {
14
17
  const code = err?.code;
15
18
  return code === "ENOENT" || code === "ENOTDIR";
16
19
  }
20
+ export function modelArgs(model, extraArgs) {
21
+ if (typeof model !== "string" || model === "")
22
+ return [];
23
+ if (extraArgs.some((arg) => arg === "--model" || arg.startsWith("--model="))) {
24
+ log.warn(`engine args already set a model, ignoring model ${JSON.stringify(model)}`);
25
+ return [];
26
+ }
27
+ return ["--model", model];
28
+ }
17
29
  export function buildArgs(prompt, sessionPath, extraArgs) {
18
30
  return ["--mode", "json", "--session", sessionPath, "-p", prompt, ...extraArgs];
19
31
  }
@@ -211,6 +223,53 @@ function exitText(code, signal) {
211
223
  return `pi was terminated by signal ${signal ?? "unknown"}`;
212
224
  return `pi exited with code ${code}`;
213
225
  }
226
+ function lastNonEmptyLine(text) {
227
+ const lines = text.split("\n");
228
+ for (let i = lines.length - 1; i >= 0; i--) {
229
+ const line = lines[i].trim();
230
+ if (line !== "")
231
+ return line;
232
+ }
233
+ return "";
234
+ }
235
+ function failureText(code, signal, stderr) {
236
+ const detail = lastNonEmptyLine(stderr);
237
+ return detail === "" ? exitText(code, signal) : `${exitText(code, signal)}: ${detail}`;
238
+ }
239
+ export function parseModels(output) {
240
+ const models = [];
241
+ let inTable = false;
242
+ for (const line of output.split("\n")) {
243
+ const columns = line.trim().split(/\s+/);
244
+ if (!inTable) {
245
+ inTable = columns[0] === "provider" && columns[1] === "model";
246
+ continue;
247
+ }
248
+ if (columns.length < 2)
249
+ continue;
250
+ models.push({ id: `${columns[0]}/${columns[1]}` });
251
+ }
252
+ return models;
253
+ }
254
+ function listModels(command) {
255
+ return new Promise((resolve, reject) => {
256
+ execFile(command, ["--list-models"], { timeout: MODELS_TIMEOUT_MS, killSignal: "SIGKILL", maxBuffer: MODELS_MAX_BUFFER, windowsHide: true }, (err, stdout, stderr) => {
257
+ if (!err) {
258
+ resolve(String(stdout));
259
+ return;
260
+ }
261
+ const { code, killed, signal } = err;
262
+ if (typeof code === "number")
263
+ reject(new Error(failureText(code, null, String(stderr))));
264
+ else if (killed)
265
+ reject(new Error(`pi --list-models timed out after ${MODELS_TIMEOUT_MS / 1000}s`));
266
+ else if (code === null)
267
+ reject(new Error(failureText(null, signal ?? null, String(stderr))));
268
+ else
269
+ reject(new Error(`pi --list-models failed: ${errorMessage(err)}`));
270
+ });
271
+ });
272
+ }
214
273
  export const engine = {
215
274
  async check(req) {
216
275
  const path = findCli(req.config, "pi");
@@ -227,7 +286,11 @@ export const engine = {
227
286
  const sessionPath = join(sessionsDir, `${params.session_id}.jsonl`);
228
287
  const mcpURL = params.mcp?.url ?? "";
229
288
  const extraArgs = params.extra_args ?? [];
230
- const args = buildArgs(params.prompt, sessionPath, mcpURL ? ["-e", EXTENSION_PATH, ...extraArgs] : extraArgs);
289
+ const args = buildArgs(params.prompt, sessionPath, [
290
+ ...(mcpURL ? ["-e", EXTENSION_PATH] : []),
291
+ ...modelArgs(params.model, extraArgs),
292
+ ...extraArgs,
293
+ ]);
231
294
  let outcome;
232
295
  try {
233
296
  outcome = await spawnCli({
@@ -255,4 +318,7 @@ export const engine = {
255
318
  async usage(req) {
256
319
  return collectUsage(join(req.state_dir, "sessions"), req.params.session_ids ?? []);
257
320
  },
321
+ async models(req) {
322
+ return parseModels(await listModels(findCli(req.config, "pi")));
323
+ },
258
324
  };
package/icon.svg ADDED
@@ -0,0 +1,4 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 800">
2
+ <path fill="currentColor" fill-rule="evenodd" d="M165.29 165.29 H517.36 V400 H400 V517.36 H282.65 V634.72 H165.29 Z M282.65 282.65 V400 H400 V282.65 Z"/>
3
+ <path fill="currentColor" d="M517.36 400 H634.72 V634.72 H517.36 Z"/>
4
+ </svg>
@@ -5,6 +5,7 @@
5
5
  "name": { "en": "Pi", "zh": "Pi" },
6
6
  "description": { "en": "Runs tasks with the pi coding agent", "zh": "用 pi 编程助手执行任务" },
7
7
  "command": ["node", "dist/index.js"],
8
+ "icon": "icon.svg",
8
9
  "config": [
9
10
  {
10
11
  "key": "path",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theopenbee/engine-pi",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "pi engine plugin for openbee",
5
5
  "keywords": [
6
6
  "openbee",
@@ -22,6 +22,7 @@
22
22
  "files": [
23
23
  "dist",
24
24
  "assets",
25
+ "icon.svg",
25
26
  "openbee.plugin.json"
26
27
  ],
27
28
  "engines": {
@@ -37,7 +38,7 @@
37
38
  "prepare": "npm run build"
38
39
  },
39
40
  "dependencies": {
40
- "@theopenbee/engine-sdk": "^0.1.0"
41
+ "@theopenbee/engine-sdk": "^0.2.0"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@types/node": "^22.10.0",