pi-lilac-provider 1.9.4 → 1.9.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-lilac-provider",
3
- "version": "1.9.4",
3
+ "version": "1.9.6",
4
4
  "description": "Lilac provider extension for pi - Access Kimi K2.6, GLM 5.1, and Gemma 4 models through Lilac's OpenAI-compatible API on idle GPUs",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -30,7 +30,7 @@
30
30
  "@earendil-works/pi-coding-agent": ">=0.74.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@earendil-works/pi-coding-agent": "0.83.0",
33
+ "@earendil-works/pi-coding-agent": "0.84.1",
34
34
  "@types/node": "25.9.1",
35
35
  "knip": "6.14.1",
36
36
  "typescript": "6.0.3"
@@ -22,15 +22,119 @@
22
22
  * patch.json and custom-models.json are applied at runtime by the provider.
23
23
  * They are NOT baked into models.json, but ARE used to generate the README table.
24
24
  *
25
- * Requires LILAC_API_KEY environment variable.
25
+ * API key: the stored `lilac` credential in ~/.pi/agent/auth.json wins, then
26
+ * the LILAC_API_KEY environment variable. The script refuses to run without one.
26
27
  */
27
28
 
28
29
  import fs from 'fs';
30
+ import os from 'os';
31
+ import { execSync } from 'child_process';
29
32
  import path from 'path';
30
33
  import { fileURLToPath } from 'url';
31
34
 
32
35
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
33
36
 
37
+ // pi's agent directory: PI_CODING_AGENT_DIR (with ~ expansion) or ~/.pi/agent.
38
+ function piAgentDir() {
39
+ const envDir = process.env.PI_CODING_AGENT_DIR;
40
+ if (envDir) {
41
+ return envDir.startsWith('~/') || envDir === '~'
42
+ ? path.join(os.homedir(), envDir.slice(1))
43
+ : envDir;
44
+ }
45
+ return path.join(os.homedir(), '.pi', 'agent');
46
+ }
47
+
48
+ const AUTH_JSON_PATH = path.join(piAgentDir(), 'auth.json');
49
+
50
+ /**
51
+ * Resolve a configured value using pi's semantics (resolve-config-value.ts in
52
+ * pi-mono): "!command" runs via the shell (10s timeout) and uses trimmed
53
+ * stdout; "$VAR" / "${VAR}" interpolate environment variables ("$$" escapes a
54
+ * literal "$", "$!" a literal "!"); anything else is a literal. Returns
55
+ * undefined when a referenced env var is unset or a command fails.
56
+ */
57
+ function resolveConfigValue(config, env) {
58
+ if (typeof config !== 'string' || config.length === 0) return undefined;
59
+ if (config.startsWith('!')) {
60
+ try {
61
+ const out = execSync(config.slice(1), {
62
+ encoding: 'utf8',
63
+ timeout: 10000,
64
+ stdio: ['ignore', 'pipe', 'ignore'],
65
+ });
66
+ return out.trim() || undefined;
67
+ } catch {
68
+ return undefined;
69
+ }
70
+ }
71
+ const ENV_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
72
+ let resolved = '';
73
+ let index = 0;
74
+ while (index < config.length) {
75
+ const dollar = config.indexOf('$', index);
76
+ if (dollar < 0) {
77
+ resolved += config.slice(index);
78
+ break;
79
+ }
80
+ resolved += config.slice(index, dollar);
81
+ const next = config[dollar + 1];
82
+ let name;
83
+ if (next === '$' || next === '!') {
84
+ resolved += next;
85
+ index = dollar + 2;
86
+ continue;
87
+ } else if (next === '{') {
88
+ const end = config.indexOf('}', dollar + 2);
89
+ if (end < 0) {
90
+ resolved += '$';
91
+ index = dollar + 1;
92
+ continue;
93
+ }
94
+ const inner = config.slice(dollar + 2, end);
95
+ if (!ENV_NAME_RE.test(inner)) {
96
+ resolved += config.slice(dollar, end + 1);
97
+ index = end + 1;
98
+ continue;
99
+ }
100
+ name = inner;
101
+ index = end + 1;
102
+ } else {
103
+ const match = config.slice(dollar + 1).match(/^[A-Za-z_][A-Za-z0-9_]*/);
104
+ if (!match) {
105
+ resolved += '$';
106
+ index = dollar + 1;
107
+ continue;
108
+ }
109
+ name = match[0];
110
+ index = dollar + 1 + name.length;
111
+ }
112
+ const value = (env && env[name]) || process.env[name] || undefined;
113
+ if (value === undefined) return undefined;
114
+ resolved += value;
115
+ }
116
+ return resolved;
117
+ }
118
+
119
+ /**
120
+ * The API key, resolved the way pi itself resolves it for this provider: the
121
+ * stored `lilac` credential in ~/.pi/agent/auth.json wins, then
122
+ * the LILAC_API_KEY environment variable.
123
+ */
124
+ function resolveApiKey() {
125
+ try {
126
+ const auth = JSON.parse(fs.readFileSync(AUTH_JSON_PATH, 'utf8'));
127
+ const credential = auth?.lilac;
128
+ if (credential && credential.type === 'api_key' && typeof credential.key === 'string') {
129
+ const key = resolveConfigValue(credential.key, credential.env);
130
+ if (key) return key;
131
+ }
132
+ } catch {
133
+ // Missing or unparseable auth.json: fall through to the env var.
134
+ }
135
+ return process.env.LILAC_API_KEY || undefined;
136
+ }
137
+
34
138
  const MODELS_API_URL = 'https://api.getlilac.com/v1/models';
35
139
  const MODELS_JSON_PATH = path.join(__dirname, '..', 'models.json');
36
140
  const PATCH_JSON_PATH = path.join(__dirname, '..', 'patch.json');
@@ -63,9 +167,9 @@ function toPerMillion(val) {
63
167
  // ─── API fetch ───────────────────────────────────────────────────────────────
64
168
 
65
169
  async function fetchModels() {
66
- const apiKey = process.env.LILAC_API_KEY;
170
+ const apiKey = resolveApiKey();
67
171
  if (!apiKey) {
68
- throw new Error('LILAC_API_KEY environment variable is required');
172
+ throw new Error('No API key found: no `lilac` credential resolved from ' + AUTH_JSON_PATH + ' and LILAC_API_KEY is not set');
69
173
  }
70
174
 
71
175
  console.log(`Fetching models from ${MODELS_API_URL}...`);