@firstpick/pi-utils 0.1.1 → 0.1.3
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 +18 -0
- package/index.ts +172 -0
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -5,5 +5,23 @@ Shared helper utilities used by `@firstpick/pi-extension-*` packages.
|
|
|
5
5
|
## Exports
|
|
6
6
|
|
|
7
7
|
- `getAgentDir()`
|
|
8
|
+
- `getPiDir()`
|
|
9
|
+
- `getAgentEnvPath()`
|
|
10
|
+
- `getAgentSettingsPath()`
|
|
11
|
+
- `getWorkspaceEnvPath(cwd?)`
|
|
8
12
|
- `envFlag(name, fallback?)`
|
|
9
13
|
- `resolvePathFromAgentDir(configuredPath)`
|
|
14
|
+
- `parseEnvFile(filePath)`
|
|
15
|
+
- `readEnvValue(filePath, key)`
|
|
16
|
+
- `resolveEnvValue(key, options?)`
|
|
17
|
+
- `quoteEnvValue(value)`
|
|
18
|
+
- `upsertEnvValue(filePath, key, value)`
|
|
19
|
+
- `slugify(input, options?)`
|
|
20
|
+
- `formatTokens(count)`
|
|
21
|
+
- `estimateTokensFromCharCount(charCount)`
|
|
22
|
+
- `estimatePromptInjectionTokens(systemPrompt)`
|
|
23
|
+
- `delay(ms)`
|
|
24
|
+
- `createExtensionWorkingIndicator(ctx, initialMessage, options?)`
|
|
25
|
+
- `withExtensionWorkingIndicator(ctx, initialMessage, run, options?)`
|
|
26
|
+
|
|
27
|
+
`createExtensionWorkingIndicator` renders a reusable extension-owned spinner using `ctx.ui.setWidget` plus footer `setStatus`, so it works inside slash-command handlers where Pi's built-in model-streaming working row is not shown.
|
package/index.ts
CHANGED
|
@@ -1,7 +1,32 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
1
2
|
import os from "node:os";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
4
5
|
|
|
6
|
+
export type ExtensionWorkingIndicator = {
|
|
7
|
+
update(message: string): void;
|
|
8
|
+
stop(): void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ExtensionWorkingIndicatorOptions = {
|
|
12
|
+
id?: string;
|
|
13
|
+
title?: string;
|
|
14
|
+
placement?: "aboveEditor" | "belowEditor";
|
|
15
|
+
intervalMs?: number;
|
|
16
|
+
frames?: string[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type EnvResolution = {
|
|
20
|
+
value?: string;
|
|
21
|
+
source?: "environment" | "workspace .env" | "Pi global .env";
|
|
22
|
+
path?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type SlugifyOptions = {
|
|
26
|
+
maxLength?: number;
|
|
27
|
+
fallback?: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
5
30
|
export function getAgentDir(): string {
|
|
6
31
|
const env = process.env.PI_CODING_AGENT_DIR?.trim();
|
|
7
32
|
if (env) return path.resolve(env);
|
|
@@ -18,6 +43,153 @@ export function resolvePathFromAgentDir(configuredPath: string): string {
|
|
|
18
43
|
return path.isAbsolute(configuredPath) ? path.normalize(configuredPath) : path.resolve(getAgentDir(), configuredPath);
|
|
19
44
|
}
|
|
20
45
|
|
|
46
|
+
export function getPiDir(): string {
|
|
47
|
+
return path.dirname(getAgentDir());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function getAgentEnvPath(): string {
|
|
51
|
+
return path.join(getAgentDir(), ".env");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function getAgentSettingsPath(): string {
|
|
55
|
+
return path.join(getAgentDir(), "settings.json");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getWorkspaceEnvPath(cwd = process.cwd()): string {
|
|
59
|
+
return path.join(cwd, ".env");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function parseEnvFile(filePath: string): Record<string, string> {
|
|
63
|
+
if (!fs.existsSync(filePath)) return {};
|
|
64
|
+
const values: Record<string, string> = {};
|
|
65
|
+
for (const rawLine of fs.readFileSync(filePath, "utf8").split(/\r?\n/)) {
|
|
66
|
+
const line = rawLine.trim();
|
|
67
|
+
if (!line || line.startsWith("#")) continue;
|
|
68
|
+
const match = line.match(/^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
|
69
|
+
if (!match) continue;
|
|
70
|
+
let value = match[2] ?? "";
|
|
71
|
+
const commentStart = value.search(/\s#/);
|
|
72
|
+
if (commentStart >= 0) value = value.slice(0, commentStart);
|
|
73
|
+
value = value.trim();
|
|
74
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
75
|
+
value = value.slice(1, -1);
|
|
76
|
+
}
|
|
77
|
+
values[match[1] ?? ""] = value.replace(/\\n/g, "\n");
|
|
78
|
+
}
|
|
79
|
+
return values;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function readEnvValue(filePath: string, key: string): string | undefined {
|
|
83
|
+
return parseEnvFile(filePath)[key];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function resolveEnvValue(key: string, options: { includeWorkspace?: boolean; cwd?: string } = {}): EnvResolution {
|
|
87
|
+
const envValue = process.env[key]?.trim();
|
|
88
|
+
if (envValue) return { value: envValue, source: "environment" };
|
|
89
|
+
|
|
90
|
+
if (options.includeWorkspace) {
|
|
91
|
+
const workspaceEnvPath = getWorkspaceEnvPath(options.cwd);
|
|
92
|
+
const workspaceValue = readEnvValue(workspaceEnvPath, key)?.trim();
|
|
93
|
+
if (workspaceValue) return { value: workspaceValue, source: "workspace .env", path: workspaceEnvPath };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const globalEnvPath = getAgentEnvPath();
|
|
97
|
+
const globalValue = readEnvValue(globalEnvPath, key)?.trim();
|
|
98
|
+
if (globalValue) return { value: globalValue, source: "Pi global .env", path: globalEnvPath };
|
|
99
|
+
|
|
100
|
+
return {};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function quoteEnvValue(value: string): string {
|
|
104
|
+
return JSON.stringify(value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function upsertEnvValue(filePath: string, key: string, value: string): void {
|
|
108
|
+
let content = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8") : "";
|
|
109
|
+
const line = `${key}=${quoteEnvValue(value)}`;
|
|
110
|
+
const pattern = new RegExp(`^\\s*(?:export\\s+)?${key}\\s*=.*$`, "m");
|
|
111
|
+
content = pattern.test(content) ? content.replace(pattern, line) : `${content}${content && !content.endsWith("\n") ? "\n" : ""}${line}\n`;
|
|
112
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
113
|
+
fs.writeFileSync(filePath, content, { mode: 0o600 });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function slugify(input: string, options: SlugifyOptions = {}): string {
|
|
117
|
+
const maxLength = options.maxLength ?? 80;
|
|
118
|
+
const slug = input
|
|
119
|
+
.toLowerCase()
|
|
120
|
+
.trim()
|
|
121
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
122
|
+
.replace(/^-+|-+$/g, "")
|
|
123
|
+
.slice(0, maxLength);
|
|
124
|
+
return slug || options.fallback || "";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function formatTokens(count: number): string {
|
|
128
|
+
if (count < 1000) return count.toString();
|
|
129
|
+
if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
|
|
130
|
+
if (count < 1000000) return `${Math.round(count / 1000)}k`;
|
|
131
|
+
if (count < 10000000) return `${(count / 1000000).toFixed(1)}M`;
|
|
132
|
+
return `${Math.round(count / 1000000)}M`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function estimateTokensFromCharCount(charCount: number): number {
|
|
136
|
+
return Math.max(0, Math.round(charCount / 4));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function estimatePromptInjectionTokens(systemPrompt: string): number {
|
|
140
|
+
return estimateTokensFromCharCount(systemPrompt.length);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function delay(ms: number): Promise<void> {
|
|
144
|
+
if (ms <= 0) return Promise.resolve();
|
|
145
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function createExtensionWorkingIndicator(ctx: any, initialMessage: string, options: ExtensionWorkingIndicatorOptions = {}): ExtensionWorkingIndicator {
|
|
149
|
+
const id = options.id ?? "extension-working";
|
|
150
|
+
const title = options.title ?? "Working";
|
|
151
|
+
const frames = options.frames ?? ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
152
|
+
const intervalMs = options.intervalMs ?? 100;
|
|
153
|
+
const placement = options.placement ?? "aboveEditor";
|
|
154
|
+
let frameIndex = 0;
|
|
155
|
+
let message = initialMessage;
|
|
156
|
+
let stopped = false;
|
|
157
|
+
|
|
158
|
+
const render = () => {
|
|
159
|
+
if (stopped) return;
|
|
160
|
+
const frame = frames[frameIndex % frames.length] ?? "•";
|
|
161
|
+
frameIndex += 1;
|
|
162
|
+
ctx?.ui?.setStatus?.(id, `${frame} ${message}`);
|
|
163
|
+
ctx?.ui?.setWidget?.(id, [`${frame} ${title}… ${message}`], { placement });
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
render();
|
|
167
|
+
const timer = setInterval(render, intervalMs);
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
update(nextMessage: string) {
|
|
171
|
+
message = nextMessage;
|
|
172
|
+
render();
|
|
173
|
+
},
|
|
174
|
+
stop() {
|
|
175
|
+
if (stopped) return;
|
|
176
|
+
stopped = true;
|
|
177
|
+
clearInterval(timer);
|
|
178
|
+
ctx?.ui?.setStatus?.(id, undefined);
|
|
179
|
+
ctx?.ui?.setWidget?.(id, undefined);
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function withExtensionWorkingIndicator<T>(ctx: any, initialMessage: string, run: (indicator: ExtensionWorkingIndicator) => Promise<T>, options?: ExtensionWorkingIndicatorOptions): Promise<T> {
|
|
185
|
+
const indicator = createExtensionWorkingIndicator(ctx, initialMessage, options);
|
|
186
|
+
try {
|
|
187
|
+
return await run(indicator);
|
|
188
|
+
} finally {
|
|
189
|
+
indicator.stop();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
21
193
|
export default function piUtilsExtension(_pi: ExtensionAPI): void {
|
|
22
194
|
// Utility package: no runtime behavior.
|
|
23
195
|
}
|
package/package.json
CHANGED