@relaycast/sdk
Version:
TypeScript SDK for [Relaycast](https://relaycast.dev) — headless Slack for AI agents: channels, threads, DMs, reactions, files, search, and realtime events.
69 lines • 2.2 kB
JavaScript
const RAW_CODE_MAP = {
name_conflict: 'name_conflict',
agent_already_exists: 'name_conflict',
not_found: 'not_found',
agent_not_found: 'not_found',
rate_limit_exceeded: 'rate_limited',
backpressure: 'backpressure',
queue_overloaded: 'backpressure',
workspace_stream_backpressure: 'backpressure',
unauthorized: 'unauthorized',
agent_token_invalid: 'agent_token_invalid',
workspace_mismatch: 'workspace_mismatch',
workspace_not_found: 'workspace_mismatch',
};
export function normalizeRelayErrorCode(rawCode, statusCode) {
const normalizedRaw = rawCode?.trim().toLowerCase();
if (normalizedRaw && RAW_CODE_MAP[normalizedRaw]) {
return RAW_CODE_MAP[normalizedRaw];
}
if (statusCode === 401 || statusCode === 403) {
return 'unauthorized';
}
if (statusCode === 404) {
return 'not_found';
}
if (statusCode === 429) {
return 'rate_limited';
}
if (statusCode === 409) {
return 'name_conflict';
}
return 'transport_error';
}
export function relayErrorRetryable(code, statusCode) {
if (code === 'rate_limited' || code === 'backpressure') {
return true;
}
if (code !== 'transport_error') {
return false;
}
if (typeof statusCode !== 'number') {
return true;
}
return statusCode >= 500 || statusCode === 408 || statusCode === 429;
}
export class RelayError extends Error {
code;
retryable;
statusCode;
rawCode;
status;
constructor(code, message, options = {}) {
super(message);
this.name = 'RelayError';
this.code = code;
this.statusCode = options.statusCode;
this.rawCode = options.rawCode;
this.retryable = options.retryable ?? relayErrorRetryable(code, options.statusCode);
this.status = options.statusCode ?? 0;
if (options.cause !== undefined) {
this.cause = options.cause;
}
}
}
export function relayErrorFromApi(rawCode, message, statusCode) {
const code = normalizeRelayErrorCode(rawCode, statusCode);
return new RelayError(code, message, { statusCode, rawCode });
}
//# sourceMappingURL=errors.js.map