Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"js/ts.tsdk.path": "node_modules/typescript/lib",
Comment thread
Vizards marked this conversation as resolved.
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"chat"
],
"activationEvents": [
"onUri",
"onStartupFinished"
],
"enabledApiProposals": [],
Expand Down
123 changes: 123 additions & 0 deletions src/client/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import type {
ApiProviderId,
HttpErrorLinkDefinition,
HttpErrorLinkStatusKey,
NetworkErrorCategory,
} from './types';
import { EXTERNAL_URLS } from '../consts';

export const OFFICIAL_DEEPSEEK_API_HOST = 'api.deepseek.com';
export const MAX_DIAGNOSTIC_FIELD_LENGTH = 300;

export const API_PROVIDER_HTTP_ERROR_LINKS: Readonly<
Record<HttpErrorLinkStatusKey, Readonly<Partial<Record<ApiProviderId, HttpErrorLinkDefinition>>>>
> = {
401: {
deepseek: {
labelKey: 'error.action.createApiKey',
url: EXTERNAL_URLS.deepseek.apiKeys,
},
},
402: {
deepseek: {
labelKey: 'error.action.viewUsage',
url: EXTERNAL_URLS.deepseek.usage,
},
},
'5xx': {
deepseek: {
labelKey: 'error.action.checkDeepSeekStatus',
url: EXTERNAL_URLS.deepseek.status,
},
},
};

/**
* Curated network error codes observed from Node.js fetch failures.
*
* Sources: Node errno / c-ares DNS codes (`NodeJS.ErrnoException.code`),
* Node TLS/OpenSSL error codes, and undici error `code` / `name` literals
* from the `undici-types` package bundled through `@types/node`.
*
* This is intentionally not exhaustive: unknown codes fall back to `generic`
* while still being shown to the user in the error message.
*/
export const NETWORK_ERROR_CATEGORY_BY_CODE = {
ENOTFOUND: 'dns',
EAI_AGAIN: 'dns',
ENODATA: 'dns',
ESERVFAIL: 'dns',
EFORMERR: 'dns',
ENONAME: 'dns',
EBADNAME: 'dns',
EBADQUERY: 'dns',
EBADFAMILY: 'dns',
EBADRESP: 'dns',
ENOTIMP: 'dns',
EREFUSED: 'dns',
ENOTINITIALIZED: 'dns',
ELOADIPHLPAPI: 'dns',
EADDRGETNETWORKPARAMS: 'dns',
ECONNREFUSED: 'unreachable',
ENETUNREACH: 'unreachable',
EHOSTUNREACH: 'unreachable',
EADDRNOTAVAIL: 'unreachable',
ENETDOWN: 'unreachable',
EHOSTDOWN: 'unreachable',
ECONNRESET: 'interrupted',
ECONNABORTED: 'interrupted',
ENETRESET: 'interrupted',
ENOTCONN: 'interrupted',
EPIPE: 'interrupted',
EOF: 'interrupted',
UND_ERR_SOCKET: 'interrupted',
SocketError: 'interrupted',
ETIMEDOUT: 'timeout',
ETIMEOUT: 'timeout',
ESOCKETTIMEDOUT: 'timeout',
UND_ERR_CONNECT_TIMEOUT: 'timeout',
UND_ERR_HEADERS_TIMEOUT: 'timeout',
UND_ERR_BODY_TIMEOUT: 'timeout',
ERR_TLS_HANDSHAKE_TIMEOUT: 'timeout',
TimeoutError: 'timeout',
ConnectTimeoutError: 'timeout',
HeadersTimeoutError: 'timeout',
BodyTimeoutError: 'timeout',
CERT_HAS_EXPIRED: 'tls',
CERT_NOT_YET_VALID: 'tls',
CERT_UNTRUSTED: 'tls',
CERT_REJECTED: 'tls',
CERT_SIGNATURE_FAILURE: 'tls',
SELF_SIGNED_CERT_IN_CHAIN: 'tls',
DEPTH_ZERO_SELF_SIGNED_CERT: 'tls',
UNABLE_TO_VERIFY_LEAF_SIGNATURE: 'tls',
UNABLE_TO_GET_ISSUER_CERT_LOCALLY: 'tls',
UNABLE_TO_GET_ISSUER_CERT: 'tls',
UNABLE_TO_GET_CRL: 'tls',
UNABLE_TO_DECRYPT_CERT_SIGNATURE: 'tls',
UNABLE_TO_DECRYPT_CRL_SIGNATURE: 'tls',
UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: 'tls',
CRL_SIGNATURE_FAILURE: 'tls',
ERR_TLS_CERT_ALTNAME_INVALID: 'tls',
UND_ERR_PRX_TLS: 'tls',
SecureProxyConnectionError: 'tls',
ABORT_ERR: 'aborted',
AbortError: 'aborted',
UND_ERR_ABORTED: 'aborted',
ECANCELLED: 'aborted',
UND_ERR_HEADERS_OVERFLOW: 'protocol',
UND_ERR_RESPONSE: 'protocol',
UND_ERR_REQ_CONTENT_LENGTH_MISMATCH: 'protocol',
UND_ERR_RES_CONTENT_LENGTH_MISMATCH: 'protocol',
UND_ERR_RES_EXCEEDED_MAX_SIZE: 'protocol',
HTTPParserError: 'protocol',
HeadersOverflowError: 'protocol',
ResponseError: 'protocol',
ResponseContentLengthMismatchError: 'protocol',
ResponseExceededMaxSizeError: 'protocol',
ERR_INVALID_URL: 'configuration',
ERR_INVALID_ARG_TYPE: 'configuration',
ERR_INVALID_ARG_VALUE: 'configuration',
UND_ERR_INVALID_ARG: 'configuration',
InvalidArgumentError: 'configuration',
} as const satisfies Record<string, NetworkErrorCategory>;
27 changes: 14 additions & 13 deletions src/client.ts → src/client/core.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { CancellationToken } from 'vscode';
import { safeStringify } from './json';
import { logger } from './logger';
import { safeStringify } from '../json';
import { logger } from '../logger';
import type {
DeepSeekRequest,
DeepSeekStreamChunk,
DeepSeekToolCall,
StreamCallbacks,
} from './types';
} from '../types';
import { createHttpError, normalizeRequestError } from './error';

/**
* Lightweight SSE-streaming DeepSeek API client.
Expand Down Expand Up @@ -53,15 +54,7 @@ export class DeepSeekClient {
});

if (!response.ok) {
const errorText = await response.text();
let errorMessage: string;
try {
const errorJson = JSON.parse(errorText);
errorMessage = errorJson.error?.message || errorJson.message || errorText;
} catch {
errorMessage = errorText;
}
throw new Error(`DeepSeek API error (${response.status}): ${errorMessage}`);
throw await createHttpError(response, this.baseUrl);
}

if (!response.body) {
Expand Down Expand Up @@ -178,7 +171,9 @@ export class DeepSeekClient {
if (isAbortError(error) && cancellationToken?.isCancellationRequested) {
return;
}
callbacks.onError(error instanceof Error ? error : new Error(String(error)));
const normalizedError = normalizeRequestError(error);
logger.error('DeepSeek request failed:', getDiagnosticMessage(normalizedError), error);
callbacks.onError(normalizedError);
} finally {
cancelListener?.dispose();
}
Expand All @@ -188,3 +183,9 @@ export class DeepSeekClient {
function isAbortError(error: unknown): boolean {
return error instanceof Error && error.name === 'AbortError';
}

function getDiagnosticMessage(error: Error): string {
return 'diagnosticMessage' in error && typeof error.diagnosticMessage === 'string'
? error.diagnosticMessage
: error.message;
}
Loading
Loading