Method signature

async generateCode({
  prompt,
  mode,
  branch,
  history,
  callbacks,
}: {
  prompt: string;
  mode: "ask" | "code";
  branch?: string;
  history?: Conversation[];
  callbacks?: VibeKitStreamCallbacks;
}): Promise<AgentResponse>

Parameters

ParameterTypeRequiredDefaultDescription
promptstringYes-The text prompt describing what code to generate or question to ask
mode"ask" | "code"Yes-Interactive Q&A mode ("ask") or code generation mode ("code")
branchstringNo-Branch identifier for version control or environment context
historyConversation[]No-Previous conversation history to provide context for the generation
callbacksVibeKitStreamCallbacksNo-Streaming callbacks for real-time updates

VibeKitStreamCallbacks Interface

PropertyTypeRequiredDescription
onUpdate(message: string) => voidNoCalled with streaming updates
onError(error: string) => voidNoCalled when errors occur

Return value

TypeDescription
Promise<AgentResponse>Promise that resolves to either a CodexResponse or ClaudeResponse depending on the configured agent

CodexResponse (for Codex Agent)

PropertyTypeDescription
sandboxIdstringUnique identifier for the sandbox environment
stdoutstringStandard output from code execution
stderrstringStandard error from code execution
exitCodenumberExit code from code execution

ClaudeResponse (for Claude Agent)

PropertyTypeDescription
codestringGenerated code response

Agent-specific behavior

Codex Agent

  • Streaming Support: Full streaming support with real-time updates
  • Sandbox Environment: Executes code in isolated E2B sandbox
  • Telemetry: Comprehensive tracking of generation, streaming, and errors
  • Mode Support: Both “ask” and “code” modes fully supported

Claude Agent

  • Streaming Support: Limited - provides start/end notifications only
  • Execution: Direct API calls without sandbox environment
  • Telemetry: Basic tracking of generation start/end and errors
  • Mode Support: Both modes supported with fallback behavior

Examples

Basic Code Generation

const vibeKit = new VibeKit(config);

const response = await vibeKit.generateCode({
  prompt: "Create a React component for a todo list",
  mode: "code"
});

console.log(response);

With Streaming Callbacks

const response = await vibeKit.generateCode({
  prompt: "Explain how React hooks work",
  mode: "ask",
  callbacks: {
    onUpdate: (message) => {
      console.log("Update:", message);
    },
    onError: (error) => {
      console.error("Error:", error);
    }
  }
});

With Conversation History and Branch

const history = [
  {
    role: "user",
    content: "What is React?"
  },
  {
    role: "assistant", 
    content: "React is a JavaScript library..."
  }
];

const response = await vibeKit.generateCode({
  prompt: "Now show me a React component example",
  mode: "code",
  branch: "feature-react-components",
  history
});

Error handling

The method throws errors in the following cases:

  • Agent not initialized: When the configured agent type doesn’t match the initialized agent
  • Agent-specific errors:
    • Codex: Sandbox creation/execution failures, API errors
    • Claude: API authentication issues, rate limits
  • Configuration errors: Missing required API keys or invalid setup
try {
  const response = await vibeKit.generateCode({
    prompt,
    mode: "code"
  });
} catch (error) {
  if (error.message.includes('not initialized')) {
    // Handle initialization error
  } else {
    // Handle generation error
  }
}

Telemetry

When telemetry is enabled, the method automatically tracks:

  • Generation start/end events
  • Streaming data (for supported agents)
  • Error events with context
  • Performance metrics like response times and data sizes

Notes

  • Required Mode: The mode parameter is now required and must be specified
  • Object Parameters: All parameters are now passed as a destructured object for better API consistency
  • Branch Support: The optional branch parameter allows for version control or environment context
  • Streaming Differences: Codex provides real-time streaming, while Claude only provides start/end notifications
  • Environment Support: Daytona environment is not yet supported and will throw an error
  • Conversation Context: History is preserved across calls to maintain conversation context