@firstpick/pi-utils 0.1.2 → 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 +14 -0
- package/index.ts +114 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,8 +5,22 @@ 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)`
|
|
10
24
|
- `createExtensionWorkingIndicator(ctx, initialMessage, options?)`
|
|
11
25
|
- `withExtensionWorkingIndicator(ctx, initialMessage, run, options?)`
|
|
12
26
|
|
package/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
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";
|
|
@@ -15,6 +16,17 @@ export type ExtensionWorkingIndicatorOptions = {
|
|
|
15
16
|
frames?: string[];
|
|
16
17
|
};
|
|
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
|
+
|
|
18
30
|
export function getAgentDir(): string {
|
|
19
31
|
const env = process.env.PI_CODING_AGENT_DIR?.trim();
|
|
20
32
|
if (env) return path.resolve(env);
|
|
@@ -31,6 +43,108 @@ export function resolvePathFromAgentDir(configuredPath: string): string {
|
|
|
31
43
|
return path.isAbsolute(configuredPath) ? path.normalize(configuredPath) : path.resolve(getAgentDir(), configuredPath);
|
|
32
44
|
}
|
|
33
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
|
+
|
|
34
148
|
export function createExtensionWorkingIndicator(ctx: any, initialMessage: string, options: ExtensionWorkingIndicatorOptions = {}): ExtensionWorkingIndicator {
|
|
35
149
|
const id = options.id ?? "extension-working";
|
|
36
150
|
const title = options.title ?? "Working";
|