@cephalization/phoenix-insight 0.1.0 → 0.3.0
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 +454 -337
- package/dist/cli.js +64 -9
- package/dist/snapshot/context.js +171 -62
- package/dist/snapshot/utils.js +112 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/cli.ts +106 -34
- package/src/snapshot/context.ts +200 -75
- package/src/snapshot/utils.ts +140 -0
package/src/snapshot/context.ts
CHANGED
|
@@ -39,6 +39,131 @@ interface PromptInfo {
|
|
|
39
39
|
updatedAt?: string;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
// =============================================================================
|
|
43
|
+
// Static Section Templates
|
|
44
|
+
// =============================================================================
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Quick Start section for external agents - appears at the top for discoverability
|
|
48
|
+
*/
|
|
49
|
+
const QUICK_START_SECTION = `## Quick Start for External Agents
|
|
50
|
+
|
|
51
|
+
This is a **read-only snapshot** of Phoenix observability data. You cannot modify this data.
|
|
52
|
+
|
|
53
|
+
### Key Files to Start With
|
|
54
|
+
|
|
55
|
+
| File | Description |
|
|
56
|
+
|------|-------------|
|
|
57
|
+
| \`/phoenix/projects/index.jsonl\` | List of all projects with traces |
|
|
58
|
+
| \`/phoenix/datasets/index.jsonl\` | List of all datasets |
|
|
59
|
+
| \`/phoenix/experiments/index.jsonl\` | List of all experiments |
|
|
60
|
+
| \`/phoenix/prompts/index.jsonl\` | List of all prompts |
|
|
61
|
+
|
|
62
|
+
### How to Parse Each File Format
|
|
63
|
+
|
|
64
|
+
**JSONL files** (\`.jsonl\`): One JSON object per line
|
|
65
|
+
\`\`\`bash
|
|
66
|
+
# Read all lines as a JSON array
|
|
67
|
+
cat /phoenix/projects/index.jsonl | jq -s '.'
|
|
68
|
+
|
|
69
|
+
# Process each line individually
|
|
70
|
+
while read -r line; do echo "$line" | jq '.name'; done < /phoenix/projects/index.jsonl
|
|
71
|
+
|
|
72
|
+
# Get first N items
|
|
73
|
+
head -n 5 /phoenix/projects/index.jsonl | jq -s '.'
|
|
74
|
+
\`\`\`
|
|
75
|
+
|
|
76
|
+
**JSON files** (\`.json\`): Standard JSON format
|
|
77
|
+
\`\`\`bash
|
|
78
|
+
# Read and pretty-print
|
|
79
|
+
cat /phoenix/projects/my-project/metadata.json | jq '.'
|
|
80
|
+
|
|
81
|
+
# Extract specific field
|
|
82
|
+
cat /phoenix/projects/my-project/metadata.json | jq '.name'
|
|
83
|
+
\`\`\`
|
|
84
|
+
|
|
85
|
+
**Markdown files** (\`.md\`): Plain text prompt templates
|
|
86
|
+
\`\`\`bash
|
|
87
|
+
# Read prompt template
|
|
88
|
+
cat /phoenix/prompts/my-prompt/versions/v1.md
|
|
89
|
+
\`\`\`
|
|
90
|
+
|
|
91
|
+
### Common Operations
|
|
92
|
+
|
|
93
|
+
\`\`\`bash
|
|
94
|
+
# List all project names
|
|
95
|
+
cat /phoenix/projects/index.jsonl | jq -r '.name'
|
|
96
|
+
|
|
97
|
+
# Count spans in a project
|
|
98
|
+
wc -l < /phoenix/projects/my-project/spans/index.jsonl
|
|
99
|
+
|
|
100
|
+
# Find spans with errors
|
|
101
|
+
cat /phoenix/projects/my-project/spans/index.jsonl | jq 'select(.status_code == "ERROR")'
|
|
102
|
+
|
|
103
|
+
# Get dataset examples
|
|
104
|
+
cat /phoenix/datasets/my-dataset/examples.jsonl | jq -s '.' | head -n 100
|
|
105
|
+
|
|
106
|
+
# Search across all files
|
|
107
|
+
grep -r "error" /phoenix/
|
|
108
|
+
\`\`\``;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Directory Structure section showing the snapshot layout
|
|
112
|
+
*/
|
|
113
|
+
const DIRECTORY_STRUCTURE_SECTION = `## Directory Structure
|
|
114
|
+
|
|
115
|
+
\`\`\`
|
|
116
|
+
/phoenix/
|
|
117
|
+
_context.md # This file - start here!
|
|
118
|
+
/projects/
|
|
119
|
+
index.jsonl # List of all projects
|
|
120
|
+
/{project_name}/
|
|
121
|
+
metadata.json # Project details
|
|
122
|
+
/spans/
|
|
123
|
+
index.jsonl # Span data (may be sampled)
|
|
124
|
+
metadata.json # Span snapshot metadata
|
|
125
|
+
/datasets/
|
|
126
|
+
index.jsonl # List of all datasets
|
|
127
|
+
/{dataset_name}/
|
|
128
|
+
metadata.json # Dataset details
|
|
129
|
+
examples.jsonl # Dataset examples
|
|
130
|
+
/experiments/
|
|
131
|
+
index.jsonl # List of all experiments
|
|
132
|
+
/{experiment_id}/
|
|
133
|
+
metadata.json # Experiment details
|
|
134
|
+
runs.jsonl # Experiment runs
|
|
135
|
+
/prompts/
|
|
136
|
+
index.jsonl # List of all prompts
|
|
137
|
+
/{prompt_name}/
|
|
138
|
+
metadata.json # Prompt details
|
|
139
|
+
/versions/
|
|
140
|
+
index.jsonl # Version list
|
|
141
|
+
/{version_id}.md # Version template
|
|
142
|
+
/_meta/
|
|
143
|
+
snapshot.json # Snapshot metadata
|
|
144
|
+
\`\`\``;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* What You Can Do section describing available operations
|
|
148
|
+
*/
|
|
149
|
+
const WHAT_YOU_CAN_DO_SECTION = `## What You Can Do
|
|
150
|
+
|
|
151
|
+
- **Explore**: ls, cat, grep, find, jq, awk, sed
|
|
152
|
+
- **Fetch more data**: \`px-fetch-more spans --project <name> --limit 500\`
|
|
153
|
+
- **Fetch specific trace**: \`px-fetch-more trace --trace-id <id>\``;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Data Freshness section with refresh instructions
|
|
157
|
+
*/
|
|
158
|
+
const DATA_FRESHNESS_SECTION = `## Data Freshness
|
|
159
|
+
|
|
160
|
+
This is a **read-only snapshot**. Data may have changed since capture.
|
|
161
|
+
Run with \`--refresh\` to get latest data.`;
|
|
162
|
+
|
|
163
|
+
// =============================================================================
|
|
164
|
+
// Main Context Generation
|
|
165
|
+
// =============================================================================
|
|
166
|
+
|
|
42
167
|
/**
|
|
43
168
|
* Generates a _context.md summary file for the Phoenix snapshot
|
|
44
169
|
* This provides human and agent-readable context about what data is available
|
|
@@ -47,17 +172,54 @@ export async function generateContext(
|
|
|
47
172
|
mode: ExecutionMode,
|
|
48
173
|
metadata: ContextMetadata
|
|
49
174
|
): Promise<void> {
|
|
50
|
-
const lines: string[] = [];
|
|
51
|
-
|
|
52
|
-
// Header
|
|
53
|
-
lines.push("# Phoenix Snapshot Context");
|
|
54
|
-
lines.push("");
|
|
55
|
-
|
|
56
175
|
// Collect stats from the snapshot
|
|
57
176
|
const stats = await collectSnapshotStats(mode);
|
|
58
177
|
|
|
59
|
-
// What's Here section
|
|
178
|
+
// Build the dynamic "What's Here" section
|
|
179
|
+
const whatsHereSection = buildWhatsHereSection(stats, metadata);
|
|
180
|
+
|
|
181
|
+
// Build the dynamic "Recent Activity" section (may be empty)
|
|
182
|
+
const recentActivitySection = buildRecentActivitySection(stats);
|
|
183
|
+
|
|
184
|
+
// Compose the full context document
|
|
185
|
+
const content = [
|
|
186
|
+
"# Phoenix Snapshot Context",
|
|
187
|
+
"",
|
|
188
|
+
QUICK_START_SECTION,
|
|
189
|
+
"",
|
|
190
|
+
whatsHereSection,
|
|
191
|
+
recentActivitySection,
|
|
192
|
+
DIRECTORY_STRUCTURE_SECTION,
|
|
193
|
+
"",
|
|
194
|
+
WHAT_YOU_CAN_DO_SECTION,
|
|
195
|
+
"",
|
|
196
|
+
DATA_FRESHNESS_SECTION,
|
|
197
|
+
].join("\n");
|
|
198
|
+
|
|
199
|
+
// Write the context file
|
|
200
|
+
await mode.writeFile("/phoenix/_context.md", content);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// =============================================================================
|
|
204
|
+
// Dynamic Section Builders
|
|
205
|
+
// =============================================================================
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Builds the "What's Here" section with project/dataset/experiment/prompt summaries
|
|
209
|
+
*/
|
|
210
|
+
function buildWhatsHereSection(
|
|
211
|
+
stats: {
|
|
212
|
+
projects: ProjectStats[];
|
|
213
|
+
datasets: DatasetInfo[];
|
|
214
|
+
experiments: ExperimentInfo[];
|
|
215
|
+
prompts: PromptInfo[];
|
|
216
|
+
},
|
|
217
|
+
metadata: ContextMetadata
|
|
218
|
+
): string {
|
|
219
|
+
const lines: string[] = [];
|
|
220
|
+
|
|
60
221
|
lines.push("## What's Here");
|
|
222
|
+
lines.push("");
|
|
61
223
|
|
|
62
224
|
// Projects summary
|
|
63
225
|
if (stats.projects.length > 0) {
|
|
@@ -115,81 +277,40 @@ export async function generateContext(
|
|
|
115
277
|
);
|
|
116
278
|
lines.push("");
|
|
117
279
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (recentActivity.length > 0) {
|
|
121
|
-
lines.push("## Recent Activity");
|
|
122
|
-
for (const activity of recentActivity) {
|
|
123
|
-
lines.push(`- ${activity}`);
|
|
124
|
-
}
|
|
125
|
-
lines.push("");
|
|
126
|
-
}
|
|
280
|
+
return lines.join("\n");
|
|
281
|
+
}
|
|
127
282
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
283
|
+
/**
|
|
284
|
+
* Builds the "Recent Activity" section if there are recent updates
|
|
285
|
+
* Returns an empty string if no recent activity
|
|
286
|
+
*/
|
|
287
|
+
function buildRecentActivitySection(stats: {
|
|
288
|
+
projects: ProjectStats[];
|
|
289
|
+
datasets: DatasetInfo[];
|
|
290
|
+
experiments: ExperimentInfo[];
|
|
291
|
+
prompts: PromptInfo[];
|
|
292
|
+
}): string {
|
|
293
|
+
const activities = getRecentActivity(stats);
|
|
138
294
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
"This is a **read-only snapshot**. Data may have changed since capture."
|
|
143
|
-
);
|
|
144
|
-
lines.push("Run with `--refresh` to get latest data.");
|
|
145
|
-
lines.push("");
|
|
295
|
+
if (activities.length === 0) {
|
|
296
|
+
return "";
|
|
297
|
+
}
|
|
146
298
|
|
|
147
|
-
|
|
148
|
-
lines.push("##
|
|
149
|
-
lines.push(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
lines.push("- `.md` files: Markdown (prompt templates)");
|
|
299
|
+
const lines: string[] = [];
|
|
300
|
+
lines.push("## Recent Activity");
|
|
301
|
+
lines.push("");
|
|
302
|
+
for (const activity of activities) {
|
|
303
|
+
lines.push(`- ${activity}`);
|
|
304
|
+
}
|
|
154
305
|
lines.push("");
|
|
155
306
|
|
|
156
|
-
|
|
157
|
-
lines.push("## Directory Structure");
|
|
158
|
-
lines.push("```");
|
|
159
|
-
lines.push("/phoenix/");
|
|
160
|
-
lines.push(" _context.md # This file");
|
|
161
|
-
lines.push(" /projects/");
|
|
162
|
-
lines.push(" index.jsonl # List of all projects");
|
|
163
|
-
lines.push(" /{project_name}/");
|
|
164
|
-
lines.push(" metadata.json # Project details");
|
|
165
|
-
lines.push(" /spans/");
|
|
166
|
-
lines.push(" index.jsonl # Span data (may be sampled)");
|
|
167
|
-
lines.push(" metadata.json # Span snapshot metadata");
|
|
168
|
-
lines.push(" /datasets/");
|
|
169
|
-
lines.push(" index.jsonl # List of all datasets");
|
|
170
|
-
lines.push(" /{dataset_name}/");
|
|
171
|
-
lines.push(" metadata.json # Dataset details");
|
|
172
|
-
lines.push(" examples.jsonl # Dataset examples");
|
|
173
|
-
lines.push(" /experiments/");
|
|
174
|
-
lines.push(" index.jsonl # List of all experiments");
|
|
175
|
-
lines.push(" /{experiment_id}/");
|
|
176
|
-
lines.push(" metadata.json # Experiment details");
|
|
177
|
-
lines.push(" runs.jsonl # Experiment runs");
|
|
178
|
-
lines.push(" /prompts/");
|
|
179
|
-
lines.push(" index.jsonl # List of all prompts");
|
|
180
|
-
lines.push(" /{prompt_name}/");
|
|
181
|
-
lines.push(" metadata.json # Prompt details");
|
|
182
|
-
lines.push(" /versions/");
|
|
183
|
-
lines.push(" index.jsonl # Version list");
|
|
184
|
-
lines.push(" /{version_id}.md # Version template");
|
|
185
|
-
lines.push(" /_meta/");
|
|
186
|
-
lines.push(" snapshot.json # Snapshot metadata");
|
|
187
|
-
lines.push("```");
|
|
188
|
-
|
|
189
|
-
// Write the context file
|
|
190
|
-
await mode.writeFile("/phoenix/_context.md", lines.join("\n"));
|
|
307
|
+
return lines.join("\n");
|
|
191
308
|
}
|
|
192
309
|
|
|
310
|
+
// =============================================================================
|
|
311
|
+
// Data Collection
|
|
312
|
+
// =============================================================================
|
|
313
|
+
|
|
193
314
|
/**
|
|
194
315
|
* Collects statistics from the snapshot filesystem
|
|
195
316
|
*/
|
|
@@ -358,6 +479,10 @@ async function collectSnapshotStats(mode: ExecutionMode): Promise<{
|
|
|
358
479
|
return result;
|
|
359
480
|
}
|
|
360
481
|
|
|
482
|
+
// =============================================================================
|
|
483
|
+
// Helper Functions
|
|
484
|
+
// =============================================================================
|
|
485
|
+
|
|
361
486
|
/**
|
|
362
487
|
* Determines the status of an experiment based on its run counts
|
|
363
488
|
*/
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Snapshot discovery utilities
|
|
3
|
+
*
|
|
4
|
+
* Functions for listing and finding snapshots in the local filesystem.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from "node:fs/promises";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
import * as os from "node:os";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Information about a single snapshot
|
|
13
|
+
*/
|
|
14
|
+
export interface SnapshotInfo {
|
|
15
|
+
/** Absolute path to the snapshot directory (the 'phoenix' subdirectory) */
|
|
16
|
+
path: string;
|
|
17
|
+
/** Timestamp when the snapshot was created (from directory name) */
|
|
18
|
+
timestamp: Date;
|
|
19
|
+
/** Unique identifier for the snapshot (directory name) */
|
|
20
|
+
id: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Get the base snapshots directory path
|
|
25
|
+
*/
|
|
26
|
+
export function getSnapshotsDir(): string {
|
|
27
|
+
return path.join(os.homedir(), ".phoenix-insight", "snapshots");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Parse a snapshot directory name to extract timestamp
|
|
32
|
+
*
|
|
33
|
+
* Directory names are in format: `<timestamp>-<random>` where timestamp is Date.now()
|
|
34
|
+
* Example: "1704067200000-abc123" -> Date(2024-01-01T00:00:00.000Z)
|
|
35
|
+
*
|
|
36
|
+
* @param dirName - The directory name to parse
|
|
37
|
+
* @returns The parsed timestamp as Date, or null if invalid
|
|
38
|
+
*/
|
|
39
|
+
function parseSnapshotDirName(dirName: string): Date | null {
|
|
40
|
+
// Format: <timestamp>-<random>
|
|
41
|
+
const match = dirName.match(/^(\d+)-[\w]+$/);
|
|
42
|
+
if (!match || !match[1]) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const timestamp = parseInt(match[1], 10);
|
|
47
|
+
if (isNaN(timestamp) || timestamp <= 0) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const date = new Date(timestamp);
|
|
52
|
+
// Validate the date is reasonable (between year 2000 and year 3000)
|
|
53
|
+
// Use UTC year to avoid timezone issues
|
|
54
|
+
const year = date.getUTCFullYear();
|
|
55
|
+
if (year < 2000 || year > 3000) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return date;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* List all available snapshots
|
|
64
|
+
*
|
|
65
|
+
* Scans the snapshots directory and returns information about each valid snapshot.
|
|
66
|
+
* Results are sorted by timestamp descending (most recent first).
|
|
67
|
+
*
|
|
68
|
+
* @returns Array of snapshot info objects, sorted by timestamp descending
|
|
69
|
+
*/
|
|
70
|
+
export async function listSnapshots(): Promise<SnapshotInfo[]> {
|
|
71
|
+
const snapshotsDir = getSnapshotsDir();
|
|
72
|
+
|
|
73
|
+
// Check if snapshots directory exists
|
|
74
|
+
try {
|
|
75
|
+
await fs.access(snapshotsDir);
|
|
76
|
+
} catch {
|
|
77
|
+
// Directory doesn't exist - return empty array
|
|
78
|
+
return [];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Read directory contents
|
|
82
|
+
let entries: string[];
|
|
83
|
+
try {
|
|
84
|
+
entries = await fs.readdir(snapshotsDir);
|
|
85
|
+
} catch {
|
|
86
|
+
// Cannot read directory - return empty array
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Filter and parse valid snapshot directories
|
|
91
|
+
const snapshots: SnapshotInfo[] = [];
|
|
92
|
+
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
const timestamp = parseSnapshotDirName(entry);
|
|
95
|
+
if (!timestamp) {
|
|
96
|
+
// Invalid directory name format - skip
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const snapshotPath = path.join(snapshotsDir, entry, "phoenix");
|
|
101
|
+
|
|
102
|
+
// Verify the phoenix subdirectory exists
|
|
103
|
+
try {
|
|
104
|
+
const stat = await fs.stat(snapshotPath);
|
|
105
|
+
if (!stat.isDirectory()) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
} catch {
|
|
109
|
+
// Phoenix subdirectory doesn't exist or can't be accessed - skip
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
snapshots.push({
|
|
114
|
+
path: snapshotPath,
|
|
115
|
+
timestamp,
|
|
116
|
+
id: entry,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Sort by timestamp descending (most recent first)
|
|
121
|
+
snapshots.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());
|
|
122
|
+
|
|
123
|
+
return snapshots;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Get the latest (most recent) snapshot
|
|
128
|
+
*
|
|
129
|
+
* @returns The most recent snapshot info, or null if no snapshots exist
|
|
130
|
+
*/
|
|
131
|
+
export async function getLatestSnapshot(): Promise<SnapshotInfo | null> {
|
|
132
|
+
const snapshots = await listSnapshots();
|
|
133
|
+
|
|
134
|
+
if (snapshots.length === 0) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// First element is the most recent due to descending sort
|
|
139
|
+
return snapshots[0] ?? null;
|
|
140
|
+
}
|