Skip to content

feat: [PRO-474] implement invoke command with API, webhook, and local options#116

Merged
xpander-moriel merged 5 commits into
mainfrom
dt-20251210-pro-474
Dec 15, 2025
Merged

feat: [PRO-474] implement invoke command with API, webhook, and local options#116
xpander-moriel merged 5 commits into
mainfrom
dt-20251210-pro-474

Conversation

@dudutwizer

@dudutwizer dudutwizer commented Dec 11, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR implements explicit invocation methods for the invoke command with API as the default, removing automatic fallbacks between methods.

Key Changes

1. API Invocation (New Default)

What: Uses /v1/agents/<agent-id>/invoke REST API endpoint
How: Sends {"input": {"text": "message"}} payload with API key and org ID headers
Why: More reliable and consistent than webhook, better for production use

2. Explicit Invocation Options

  • --api: API invocation (default - no flag needed)
  • --webhook: Webhook invocation (explicit flag required)
  • --local: Local handler invocation (explicit flag required)

3. Removed Fallback Logic

Before: Local → Webhook fallback chain
After: Each method is independent, fails immediately if unavailable
Why: Makes behavior predictable and errors clear

4. Top-Level Command Alias

Added: xpander invoke as alias for xpander agent invoke
Why: Convenience and consistency with other top-level commands

Files Modified

Examples

# API invocation (default)
xpander invoke "MyAgent" "Hello world"

# Webhook invocation (explicit)
xpander invoke --webhook "MyAgent" "Hello world"

# Local handler invocation (explicit)
xpander invoke --local "MyAgent" "Hello world"

# Also works with agent subcommand
xpander agent invoke "MyAgent" "Hello world"

Breaking Changes

⚠️ Default behavior changed:

  • Old: Local handler with webhook fallback
  • New: API invocation by default

Migration: Add --local flag if you need local handler behavior

Test plan

  • Test API invocation works by default
  • Test webhook invocation with --webhook flag
  • Test local invocation with --local flag
  • Test top-level xpander invoke command
  • Verify no fallbacks occur between methods
  • Test with various agent types

🤖 Generated with Claude Code


Note

Adds explicit API (default), webhook, and local invocation modes to agent invoke, enhances message/agent resolution (including .env), and introduces a top‑level xpander invoke alias.

  • CLI – agent invoke
    • Invocation modes: Add explicit --api (default), --webhook, and --local paths; remove automatic fallbacks.
    • API path: Calls POST /v1/agents/{agentId}/invoke with {"input": {"text": message}}, using x-api-key and x-organization-id headers.
    • Input parsing: Add --message; support .env agent auto-detection; maintain legacy --agent/--agent-id/--agent-name; improved interactive prompts.
    • UX: Updated help examples; show selected agent; spinners and response-time display; better JSON output extraction for local runs.
    • Errors: Clear failures per mode; enhanced handling for billing (429), 401/404, and timeouts.
  • Top-level alias
    • Add xpander invoke via configureInvokeCommand and register it in src/index.ts.

Written by Cursor Bugbot for commit 54f7cd6. This will update automatically on new commits. Configure here.

… options

## Summary
- Added --api (default), --webhook, and --local invocation options
- Changed default invocation from local handler to API
- Removed fallback logic between invocation methods
- Added top-level `xpander invoke` command alias

## Changes

### Invoke Command Options
- Added `--api` flag for API invocation (now the default)
- Added `--webhook` flag for webhook invocation
- Added `--local` flag for local handler invocation
- Each method is explicit with no automatic fallbacks

### API Invocation (Default)
- Uses `/v1/agents/<agent-id>/invoke` API endpoint
- Sends request with `input.text` payload
- Requires both API key and organization ID headers
- Returns structured response with proper error handling

### Webhook Invocation
- Maintains existing webhook functionality
- Uses agent's configured webhook_url or fallback URL
- No longer used as fallback from local handler failures

### Local Handler Invocation
- Must be explicitly requested with `--local` flag
- Fails immediately if xpander_handler.py not available
- No fallback to webhook or API on failure

### Top-Level Command Alias
- Created `xpander invoke` as alias for `xpander agent invoke`
- Uses same registration function for consistency
- Available at root command level for convenience

## Files Modified
- src/commands/agent/commands/invoke.ts - Updated invoke logic
- src/commands/invoke.ts - New top-level command registration
- src/index.ts - Registered invoke command

## Breaking Changes
- Default invocation method changed from local to API
- Local handler no longer auto-selected when available
- No automatic fallbacks between invocation methods

## Test plan
- [ ] Test `xpander invoke` API invocation (default)
- [ ] Test `xpander invoke --webhook` webhook invocation
- [ ] Test `xpander invoke --local` local handler invocation
- [ ] Test `xpander agent invoke` still works
- [ ] Verify no fallbacks occur between methods

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@notion-workspace

Copy link
Copy Markdown

…nt not found

Previously, when a user ran `xpander invoke <invalid-name>`, the command would fail without checking the .env file for XPANDER_AGENT_ID. Now the agent resolution logic:

1. Always checks for .env file first to see if agent_id is present
2. If an agent name/ID is provided, tries to resolve it
3. If resolution fails AND .env has agent_id, falls back to using the .env agent_id
4. Shows a warning message when falling back to .env

This makes the invoke command more intelligent when running in an agent directory.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Comment thread src/utils/agent-resolver.ts Outdated
);
}
return envAgentId;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Silent fallback to .env agent when explicit agent fails

When a user explicitly provides an agent name that doesn't exist, getAgentIdFromEnvOrSelection silently falls back to using the agent_id from the .env file. This contradicts the PR's stated goal of removing fallback logic and making errors clear. A user who types xpander invoke "WrongAgentName" "message" might unknowingly invoke a completely different agent from their .env file. The invoke command's error handling at lines 101-108 never triggers because the function returns the env agent ID instead of null.

Fix in Cursor Fix in Web

Comment thread src/commands/agent/commands/invoke.ts
Comment thread src/commands/agent/commands/invoke.ts Outdated
Added explicit --message flag to allow users to clearly specify the message when invoking agents. This works alongside --agent-id and --agent-name flags for maximum flexibility.

Changes:
- Added --message flag for explicit message specification
- Improved argument parsing priority:
  1. --message flag takes precedence
  2. --agent/--agent-id/--agent-name flags for explicit agent specification
  3. In agent directory with .env: all positional args become message
  4. Otherwise: first arg is agent, rest is message
- Updated help text with examples of all invocation patterns
- Fixed duplicate "Using agent" message by using silent mode in resolveAgentId

Usage examples:
- xpander agent invoke --agent-id abc123 --message "hi"
- xpander agent invoke --agent-name "MyAgent" -m "hello"
- xpander agent invoke "hi" (in agent directory)
- xpander agent invoke MyAgent "hi" (outside agent directory)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Fixed 3 bugs identified by Cursor code review:

1. Bug #1 (Silent fallback): Already fixed in previous commit - removed
   fallback logic from agent-resolver so explicit agent failures are clear

2. Bug #2 (Missing org ID validation): Added validation for organization ID
   before making API request. Now shows clear error message if org ID is
   missing, matching the API key validation pattern

3. Bug #3 (Staging URL support): Added IS_STG environment variable check
   to switch between production and staging API URLs:
   - Production: https://api.xpander.ai
   - Staging: https://api-stg.xpander.ai

Changes in invoke.ts:
- Lines 317-324: Added orgId validation with clear error message
- Lines 326-330: Added staging environment detection and dynamic base URL

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@xpander-moriel
xpander-moriel merged commit 710e4c9 into main Dec 15, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants