import type { componentsV1, PhoenixClient } from "@arizeai/phoenix-client";
import { createPhoenixClient } from "../client";
import { getConfigErrorMessage, resolveConfig } from "../config";
import { writeError, writeOutput, writeProgress } from "../io";
import { formatPromptsOutput, type OutputFormat } from "./formatPrompts";
import { Command } from "commander";
type Prompt = componentsV1["schemas"]["Prompt"];
interface PromptsOptions {
endpoint?: string;
apiKey?: string;
format?: OutputFormat;
progress?: boolean;
limit?: number;
}
/**
* Fetch all prompts from Phoenix
*/
async function fetchPrompts(
client: PhoenixClient,
options: { limit?: number } = {}
): Promise<Prompt[]> {
const allPrompts: Prompt[] = [];
let cursor: string | undefined;
const pageLimit = options.limit || 100;
do {
const response = await client.GET("/v1/prompts", {
params: {
query: {
cursor,
limit: pageLimit,
},
},
});
if (response.error || !response.data) {
throw new Error(`Failed to fetch prompts: ${response.error}`);
}
allPrompts.push(...response.data.data);
cursor = response.data.next_cursor || undefined;
// If we've fetched enough for the requested limit, stop
if (options.limit && allPrompts.length >= options.limit) {
break;
}
} while (cursor);
return allPrompts;
}
/**
* Prompts command handler
*/
async function promptsHandler(options: PromptsOptions): Promise<void> {
try {
// Resolve configuration
const config = resolveConfig({
cliOptions: {
endpoint: options.endpoint,
apiKey: options.apiKey,
},
});
// Validate that we have endpoint
if (!config.endpoint) {
const errors = [
"Phoenix endpoint not configured. Set PHOENIX_HOST environment variable or use --endpoint flag.",
];
writeError({ message: getConfigErrorMessage({ errors }) });
process.exit(1);
}
// Create client
const client = createPhoenixClient({ config });
writeProgress({
message: "Fetching prompts...",
noProgress: !options.progress,
});
// Fetch prompts
const prompts = await fetchPrompts(client, {
limit: options.limit,
});
writeProgress({
message: `Found ${prompts.length} prompt(s)`,
noProgress: !options.progress,
});
// Output prompts
const output = formatPromptsOutput({
prompts,
format: options.format,
});
writeOutput({ message: output });
} catch (error) {
writeError({
message: `Error fetching prompts: ${error instanceof Error ? error.message : String(error)}`,
});
process.exit(1);
}
}
/**
* Create the prompts command
*/
export function createPromptsCommand(): Command {
const command = new Command("prompts");
command
.description("List all available Phoenix prompts")
.option("--endpoint <url>", "Phoenix API endpoint")
.option("--api-key <key>", "Phoenix API key for authentication")
.option(
"--format <format>",
"Output format: pretty, json, or raw",
"pretty"
)
.option("--no-progress", "Disable progress indicators")
.option("--limit <number>", "Maximum number of prompts to fetch", parseInt)
.action(promptsHandler);
return command;
}