Skip to content

Conversation

@deepracticexs
Copy link
Member

Summary

Add ability to pass custom authentication headers and business context when creating remote AgentX connections. This addresses the integration needs described in #192.

Features

🔐 Custom Headers

  • Node.js: Headers sent during WebSocket handshake
  • Browser: Headers sent as first authentication message (WebSocket API limitation)
  • Supports static values, sync functions, and async functions

📦 Business Context

  • Automatically inject business context (userId, tenantId, permissions, etc.) into all requests
  • Context is merged into the data field of every request
  • Request-level context can override global context
  • Supports static values, sync functions, and async functions

Usage

import { createAgentX } from "agentxjs";

// Static configuration
const agentx = await createAgentX({
  serverUrl: "ws://localhost:5200",
  headers: { Authorization: "Bearer sk-xxx" },
  context: { userId: "123", tenantId: "abc" },
});

// Dynamic configuration (sync)
const agentx = await createAgentX({
  serverUrl: "ws://localhost:5200",
  headers: () => ({ Authorization: \`Bearer \${getToken()}\` }),
  context: () => ({ userId: getCurrentUser().id }),
});

// Dynamic configuration (async)
const agentx = await createAgentX({
  serverUrl: "ws://localhost:5200",
  headers: async () => ({ Authorization: \`Bearer \${await fetchToken()}\` }),
  context: async () => ({ userId: await getUserId() }),
});

Changes

  • RemoteConfig: Add optional headers and context fields
  • ChannelClientOptions: Add optional headers field
  • WebSocketClient: Implement header passing for Node.js and browser
  • createRemoteAgentX: Add context merging logic

Testing

All functionality has been validated:

  • ✅ Static headers/context
  • ✅ Dynamic functions (sync)
  • ✅ Dynamic functions (async)
  • ✅ Request-level context override
  • ✅ TypeScript type checking
  • ✅ ESLint validation

Test scripts available in temp/ directory:

  • temp/test-server-headers.ts - Test WebSocket server
  • temp/test-client-headers.ts - Comprehensive client tests
  • temp/README-HEADERS-TEST.md - Documentation

Related

Closes #192

Add ability to pass custom authentication headers and business context
when creating remote AgentX connections. Addresses integration needs
for backend framework integrations.

Features:
- Custom headers for WebSocket authentication
  - Node.js: sent during handshake
  - Browser: sent as first message (WebSocket API limitation)
- Business context auto-injection into all requests
- Support for static, sync, and async value resolution
- Request-level context override capability

Changes:
- RemoteConfig: add headers and context fields
- ChannelClientOptions: add headers field
- WebSocketClient: implement header passing
- createRemoteAgentX: add context merging logic

Resolves #192

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
@deepracticexs deepracticexs merged commit 09b990b into main Jan 9, 2026
3 checks passed
@deepracticexs deepracticexs deleted the feat/add-headers-context-support branch January 9, 2026 10:59
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.

紧急需求:创建服务的时候可以选择自定义请求头等消息

2 participants