@cephalization/phoenix-insight 0.1.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.
Files changed (54) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +620 -0
  3. package/dist/agent/index.js +230 -0
  4. package/dist/cli.js +640 -0
  5. package/dist/commands/index.js +2 -0
  6. package/dist/commands/px-fetch-more-spans.js +98 -0
  7. package/dist/commands/px-fetch-more-trace.js +110 -0
  8. package/dist/config/index.js +165 -0
  9. package/dist/config/loader.js +141 -0
  10. package/dist/config/schema.js +53 -0
  11. package/dist/index.js +1 -0
  12. package/dist/modes/index.js +17 -0
  13. package/dist/modes/local.js +134 -0
  14. package/dist/modes/sandbox.js +121 -0
  15. package/dist/modes/types.js +1 -0
  16. package/dist/observability/index.js +65 -0
  17. package/dist/progress.js +209 -0
  18. package/dist/prompts/index.js +1 -0
  19. package/dist/prompts/system.js +30 -0
  20. package/dist/snapshot/client.js +74 -0
  21. package/dist/snapshot/context.js +332 -0
  22. package/dist/snapshot/datasets.js +68 -0
  23. package/dist/snapshot/experiments.js +135 -0
  24. package/dist/snapshot/index.js +262 -0
  25. package/dist/snapshot/projects.js +44 -0
  26. package/dist/snapshot/prompts.js +199 -0
  27. package/dist/snapshot/spans.js +80 -0
  28. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  29. package/package.json +75 -0
  30. package/src/agent/index.ts +323 -0
  31. package/src/cli.ts +782 -0
  32. package/src/commands/index.ts +8 -0
  33. package/src/commands/px-fetch-more-spans.ts +174 -0
  34. package/src/commands/px-fetch-more-trace.ts +183 -0
  35. package/src/config/index.ts +225 -0
  36. package/src/config/loader.ts +173 -0
  37. package/src/config/schema.ts +66 -0
  38. package/src/index.ts +1 -0
  39. package/src/modes/index.ts +21 -0
  40. package/src/modes/local.ts +163 -0
  41. package/src/modes/sandbox.ts +144 -0
  42. package/src/modes/types.ts +31 -0
  43. package/src/observability/index.ts +90 -0
  44. package/src/progress.ts +239 -0
  45. package/src/prompts/index.ts +1 -0
  46. package/src/prompts/system.ts +31 -0
  47. package/src/snapshot/client.ts +129 -0
  48. package/src/snapshot/context.ts +462 -0
  49. package/src/snapshot/datasets.ts +132 -0
  50. package/src/snapshot/experiments.ts +246 -0
  51. package/src/snapshot/index.ts +403 -0
  52. package/src/snapshot/projects.ts +58 -0
  53. package/src/snapshot/prompts.ts +267 -0
  54. package/src/snapshot/spans.ts +142 -0
@@ -0,0 +1,323 @@
1
+ /**
2
+ * Phoenix Insight AI agent setup using Vercel AI SDK
3
+ */
4
+
5
+ import {
6
+ generateText,
7
+ streamText,
8
+ tool,
9
+ stepCountIs,
10
+ type GenerateTextResult,
11
+ type StreamTextResult,
12
+ } from "ai";
13
+ import { anthropic } from "@ai-sdk/anthropic";
14
+ import { z } from "zod";
15
+ import type { ExecutionMode } from "../modes/types.js";
16
+ import { INSIGHT_SYSTEM_PROMPT } from "../prompts/system.js";
17
+ import {
18
+ fetchMoreSpans,
19
+ fetchMoreTrace,
20
+ type FetchMoreSpansOptions,
21
+ type FetchMoreTraceOptions,
22
+ } from "../commands/index.js";
23
+ import type { PhoenixClient } from "@arizeai/phoenix-client";
24
+ import { PhoenixClientError } from "../snapshot/client.js";
25
+
26
+ /**
27
+ * Configuration for the Phoenix Insight agent
28
+ */
29
+ export interface PhoenixInsightAgentConfig {
30
+ /** The execution mode (sandbox or local) */
31
+ mode: ExecutionMode;
32
+ /** Phoenix client instance */
33
+ client: PhoenixClient;
34
+ /** Maximum number of agent steps before stopping (default: 25) */
35
+ maxSteps?: number;
36
+ }
37
+
38
+ /**
39
+ * Phoenix Insight Agent
40
+ */
41
+ export class PhoenixInsightAgent {
42
+ private mode: ExecutionMode;
43
+ private client: PhoenixClient;
44
+ private maxSteps: number;
45
+ private tools: Record<string, any> | null = null;
46
+ private model = anthropic("claude-sonnet-4-5");
47
+
48
+ constructor(config: PhoenixInsightAgentConfig) {
49
+ this.mode = config.mode;
50
+ this.client = config.client;
51
+ this.maxSteps = config.maxSteps || 25;
52
+ }
53
+
54
+ /**
55
+ * Initialize the agent tools
56
+ */
57
+ private async initializeTools(): Promise<Record<string, any>> {
58
+ if (this.tools) return this.tools;
59
+
60
+ // Get the bash tool from the execution mode
61
+ const bashTool = await this.mode.getBashTool();
62
+
63
+ // Store references in closure for the custom tools
64
+ const client = this.client;
65
+ const mode = this.mode;
66
+
67
+ // Create custom px-fetch-more-spans tool using AI SDK's tool function
68
+ const pxFetchMoreSpans = tool({
69
+ description:
70
+ "Fetch additional spans from Phoenix. Use when you need more span data than what's in the snapshot. You must provide both project name and optionally a limit.",
71
+ inputSchema: z.object({
72
+ project: z.string().describe("The project name"),
73
+ limit: z
74
+ .number()
75
+ .optional()
76
+ .describe("Number of spans to fetch (default: 500)"),
77
+ startTime: z
78
+ .string()
79
+ .optional()
80
+ .describe("Start time filter in ISO format"),
81
+ endTime: z
82
+ .string()
83
+ .optional()
84
+ .describe("End time filter in ISO format"),
85
+ }),
86
+ execute: async (params) => {
87
+ try {
88
+ const options: FetchMoreSpansOptions = {
89
+ project: params.project,
90
+ limit: params.limit || 500,
91
+ startTime: params.startTime,
92
+ endTime: params.endTime,
93
+ };
94
+
95
+ await fetchMoreSpans(client, mode, options);
96
+
97
+ return {
98
+ success: true,
99
+ message: `Fetched additional spans for project ${params.project}. Data saved to /phoenix/projects/${params.project}/spans/`,
100
+ };
101
+ } catch (error) {
102
+ return {
103
+ success: false,
104
+ error: error instanceof Error ? error.message : String(error),
105
+ };
106
+ }
107
+ },
108
+ });
109
+
110
+ // Create custom px-fetch-more-trace tool using AI SDK's tool function
111
+ const pxFetchMoreTrace = tool({
112
+ description:
113
+ "Fetch a specific trace by ID from Phoenix. Use when you need to examine a particular trace in detail. You must provide both the trace ID and the project name.",
114
+ inputSchema: z.object({
115
+ traceId: z.string().describe("The trace ID to fetch"),
116
+ project: z.string().describe("The project name to search in"),
117
+ }),
118
+ execute: async (params) => {
119
+ try {
120
+ const options: FetchMoreTraceOptions = {
121
+ traceId: params.traceId,
122
+ project: params.project,
123
+ };
124
+
125
+ await fetchMoreTrace(client, mode, options);
126
+
127
+ return {
128
+ success: true,
129
+ message: `Fetched trace ${params.traceId}. Data saved to /phoenix/traces/${params.traceId}/`,
130
+ };
131
+ } catch (error) {
132
+ return {
133
+ success: false,
134
+ error: error instanceof Error ? error.message : String(error),
135
+ };
136
+ }
137
+ },
138
+ });
139
+
140
+ this.tools = {
141
+ bash: bashTool,
142
+ px_fetch_more_spans: pxFetchMoreSpans,
143
+ px_fetch_more_trace: pxFetchMoreTrace,
144
+ };
145
+
146
+ return this.tools;
147
+ }
148
+
149
+ /**
150
+ * Generate a response for a user query
151
+ */
152
+ async generate(
153
+ userQuery: string,
154
+ options?: {
155
+ onStepFinish?: (step: any) => void;
156
+ }
157
+ ): Promise<GenerateTextResult<any, any>> {
158
+ let tools;
159
+ try {
160
+ tools = await this.initializeTools();
161
+ } catch (error) {
162
+ throw new Error(
163
+ `Failed to initialize agent tools: ${error instanceof Error ? error.message : String(error)}`
164
+ );
165
+ }
166
+
167
+ try {
168
+ const result = await generateText({
169
+ model: this.model,
170
+ system: INSIGHT_SYSTEM_PROMPT,
171
+ prompt: userQuery,
172
+ tools,
173
+ stopWhen: stepCountIs(this.maxSteps),
174
+ onStepFinish: options?.onStepFinish,
175
+ experimental_telemetry: {
176
+ isEnabled: true,
177
+ },
178
+ });
179
+
180
+ return result;
181
+ } catch (error) {
182
+ // Check for specific AI SDK errors
183
+ if (error instanceof Error) {
184
+ if (error.message.includes("rate limit")) {
185
+ throw new Error(
186
+ "AI model rate limit exceeded. Please wait and try again."
187
+ );
188
+ }
189
+ if (error.message.includes("timeout")) {
190
+ throw new Error("AI model request timed out. Please try again.");
191
+ }
192
+ if (
193
+ error.message.includes("authentication") ||
194
+ error.message.includes("API key")
195
+ ) {
196
+ throw new Error(
197
+ "AI model authentication failed. Check your API key configuration."
198
+ );
199
+ }
200
+ }
201
+
202
+ throw new Error(
203
+ `AI generation failed: ${error instanceof Error ? error.message : String(error)}`
204
+ );
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Stream a response for a user query
210
+ */
211
+ async stream(
212
+ userQuery: string,
213
+ options?: {
214
+ onStepFinish?: (step: any) => void;
215
+ }
216
+ ): Promise<StreamTextResult<any, any>> {
217
+ let tools;
218
+ try {
219
+ tools = await this.initializeTools();
220
+ } catch (error) {
221
+ throw new Error(
222
+ `Failed to initialize agent tools: ${error instanceof Error ? error.message : String(error)}`
223
+ );
224
+ }
225
+
226
+ try {
227
+ const result = streamText({
228
+ model: this.model,
229
+ system: INSIGHT_SYSTEM_PROMPT,
230
+ prompt: userQuery,
231
+ tools,
232
+ stopWhen: stepCountIs(this.maxSteps),
233
+ onStepFinish: options?.onStepFinish,
234
+ experimental_telemetry: {
235
+ isEnabled: true,
236
+ },
237
+ });
238
+
239
+ return result;
240
+ } catch (error) {
241
+ // Check for specific AI SDK errors
242
+ if (error instanceof Error) {
243
+ if (error.message.includes("rate limit")) {
244
+ throw new Error(
245
+ "AI model rate limit exceeded. Please wait and try again."
246
+ );
247
+ }
248
+ if (error.message.includes("timeout")) {
249
+ throw new Error("AI model request timed out. Please try again.");
250
+ }
251
+ if (
252
+ error.message.includes("authentication") ||
253
+ error.message.includes("API key")
254
+ ) {
255
+ throw new Error(
256
+ "AI model authentication failed. Check your API key configuration."
257
+ );
258
+ }
259
+ }
260
+
261
+ throw new Error(
262
+ `AI streaming failed: ${error instanceof Error ? error.message : String(error)}`
263
+ );
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Clean up resources
269
+ */
270
+ async cleanup(): Promise<void> {
271
+ await this.mode.cleanup();
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Creates a Phoenix Insight agent
277
+ */
278
+ export async function createInsightAgent(
279
+ config: PhoenixInsightAgentConfig
280
+ ): Promise<PhoenixInsightAgent> {
281
+ return new PhoenixInsightAgent(config);
282
+ }
283
+
284
+ /**
285
+ * Run a query with the Phoenix Insight agent
286
+ */
287
+ export async function runQuery(
288
+ agent: PhoenixInsightAgent,
289
+ userQuery: string,
290
+ options?: {
291
+ onStepFinish?: (step: any) => void;
292
+ stream?: boolean;
293
+ }
294
+ ): Promise<GenerateTextResult<any, any> | StreamTextResult<any, any>> {
295
+ const { stream = false, ...callbacks } = options || {};
296
+
297
+ if (stream) {
298
+ return await agent.stream(userQuery, callbacks);
299
+ } else {
300
+ return await agent.generate(userQuery, callbacks);
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Create and run a one-shot query
306
+ */
307
+ export async function runOneShotQuery(
308
+ config: PhoenixInsightAgentConfig,
309
+ userQuery: string,
310
+ options?: {
311
+ onStepFinish?: (step: any) => void;
312
+ stream?: boolean;
313
+ }
314
+ ): Promise<GenerateTextResult<any, any> | StreamTextResult<any, any>> {
315
+ const agent = await createInsightAgent(config);
316
+
317
+ try {
318
+ const result = await runQuery(agent, userQuery, options);
319
+ return result;
320
+ } finally {
321
+ await agent.cleanup();
322
+ }
323
+ }