typebulb 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/claude/client.js +341 -205
- package/dist/agents/claude/styles.css +3 -21
- package/dist/ai/aiProvider.d.ts +59 -0
- package/dist/ai/aiProvider.d.ts.map +1 -0
- package/dist/ai/aiProvider.js +109 -0
- package/dist/ai/aiProvider.js.map +1 -0
- package/dist/ai/aiProviders.d.ts +28 -0
- package/dist/ai/aiProviders.d.ts.map +1 -0
- package/dist/ai/aiProviders.js +47 -0
- package/dist/ai/aiProviders.js.map +1 -0
- package/dist/ai/chat.d.ts +32 -0
- package/dist/ai/chat.d.ts.map +1 -0
- package/dist/ai/chat.js +5 -0
- package/dist/ai/chat.js.map +1 -0
- package/dist/ai/index.d.ts +9 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/index.js +11 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/ai/protocol.d.ts +18 -0
- package/dist/ai/protocol.d.ts.map +1 -0
- package/dist/ai/protocol.js +3 -0
- package/dist/ai/protocol.js.map +1 -0
- package/dist/ai/providers/anthropic.d.ts +123 -0
- package/dist/ai/providers/anthropic.d.ts.map +1 -0
- package/dist/ai/providers/anthropic.js +130 -0
- package/dist/ai/providers/anthropic.js.map +1 -0
- package/dist/ai/providers/gemini.d.ts +62 -0
- package/dist/ai/providers/gemini.d.ts.map +1 -0
- package/dist/ai/providers/gemini.js +136 -0
- package/dist/ai/providers/gemini.js.map +1 -0
- package/dist/ai/providers/openAI.d.ts +146 -0
- package/dist/ai/providers/openAI.d.ts.map +1 -0
- package/dist/ai/providers/openAI.js +127 -0
- package/dist/ai/providers/openAI.js.map +1 -0
- package/dist/ai/providers/openRouter.d.ts +65 -0
- package/dist/ai/providers/openRouter.d.ts.map +1 -0
- package/dist/ai/providers/openRouter.js +82 -0
- package/dist/ai/providers/openRouter.js.map +1 -0
- package/dist/ai/sseParser.d.ts +32 -0
- package/dist/ai/sseParser.d.ts.map +1 -0
- package/dist/ai/sseParser.js +121 -0
- package/dist/ai/sseParser.js.map +1 -0
- package/dist/ai/stream.d.ts +20 -0
- package/dist/ai/stream.d.ts.map +1 -0
- package/dist/ai/stream.js +6 -0
- package/dist/ai/stream.js.map +1 -0
- package/dist/dts/cache.d.ts +30 -0
- package/dist/dts/cache.d.ts.map +1 -0
- package/dist/dts/cache.js +2 -0
- package/dist/dts/cache.js.map +1 -0
- package/dist/dts/definitelyTypedProvider.d.ts +20 -0
- package/dist/dts/definitelyTypedProvider.d.ts.map +1 -0
- package/dist/dts/definitelyTypedProvider.js +93 -0
- package/dist/dts/definitelyTypedProvider.js.map +1 -0
- package/dist/dts/dtsConfig.d.ts +12 -0
- package/dist/dts/dtsConfig.d.ts.map +1 -0
- package/dist/dts/dtsConfig.js +24 -0
- package/dist/dts/dtsConfig.js.map +1 -0
- package/dist/dts/dtsResolver.d.ts +63 -0
- package/dist/dts/dtsResolver.d.ts.map +1 -0
- package/dist/dts/dtsResolver.js +272 -0
- package/dist/dts/dtsResolver.js.map +1 -0
- package/dist/dts/fetchDts.d.ts +13 -0
- package/dist/dts/fetchDts.d.ts.map +1 -0
- package/dist/dts/fetchDts.js +46 -0
- package/dist/dts/fetchDts.js.map +1 -0
- package/dist/dts/httpFetch.d.ts +17 -0
- package/dist/dts/httpFetch.d.ts.map +1 -0
- package/dist/dts/httpFetch.js +35 -0
- package/dist/dts/httpFetch.js.map +1 -0
- package/dist/dts/index.d.ts +14 -0
- package/dist/dts/index.d.ts.map +1 -0
- package/dist/dts/index.js +14 -0
- package/dist/dts/index.js.map +1 -0
- package/dist/dts/libManifest.d.ts +41 -0
- package/dist/dts/libManifest.d.ts.map +1 -0
- package/dist/dts/libManifest.js +109 -0
- package/dist/dts/libManifest.js.map +1 -0
- package/dist/dts/negativeCacheHelper.d.ts +29 -0
- package/dist/dts/negativeCacheHelper.d.ts.map +1 -0
- package/dist/dts/negativeCacheHelper.js +44 -0
- package/dist/dts/negativeCacheHelper.js.map +1 -0
- package/dist/dts/tarballFetcher.d.ts +13 -0
- package/dist/dts/tarballFetcher.d.ts.map +1 -0
- package/dist/dts/tarballFetcher.js +58 -0
- package/dist/dts/tarballFetcher.js.map +1 -0
- package/dist/dts/tbTypings.d.ts +15 -0
- package/dist/dts/tbTypings.d.ts.map +1 -0
- package/dist/dts/tbTypings.js +210 -0
- package/dist/dts/tbTypings.js.map +1 -0
- package/dist/dts/typeProvider.d.ts +37 -0
- package/dist/dts/typeProvider.d.ts.map +1 -0
- package/dist/dts/typeProvider.js +30 -0
- package/dist/dts/typeProvider.js.map +1 -0
- package/dist/dts/typeRefScanner.d.ts +8 -0
- package/dist/dts/typeRefScanner.d.ts.map +1 -0
- package/dist/dts/typeRefScanner.js +33 -0
- package/dist/dts/typeRefScanner.js.map +1 -0
- package/dist/dts/typescriptProvider.d.ts +21 -0
- package/dist/dts/typescriptProvider.d.ts.map +1 -0
- package/dist/dts/typescriptProvider.js +140 -0
- package/dist/dts/typescriptProvider.js.map +1 -0
- package/dist/dts/virtualFs.d.ts +20 -0
- package/dist/dts/virtualFs.d.ts.map +1 -0
- package/dist/dts/virtualFs.js +48 -0
- package/dist/dts/virtualFs.js.map +1 -0
- package/dist/format/chunks.d.ts +20 -0
- package/dist/format/chunks.d.ts.map +1 -0
- package/dist/format/chunks.js +51 -0
- package/dist/format/chunks.js.map +1 -0
- package/dist/format/config.d.ts +17 -0
- package/dist/format/config.d.ts.map +1 -0
- package/dist/format/config.js +13 -0
- package/dist/format/config.js.map +1 -0
- package/dist/format/detection.d.ts +11 -0
- package/dist/format/detection.d.ts.map +1 -0
- package/dist/format/detection.js +43 -0
- package/dist/format/detection.js.map +1 -0
- package/dist/format/index.d.ts +8 -0
- package/dist/format/index.d.ts.map +1 -0
- package/dist/format/index.js +8 -0
- package/dist/format/index.js.map +1 -0
- package/dist/format/parse.d.ts +20 -0
- package/dist/format/parse.d.ts.map +1 -0
- package/dist/format/parse.js +88 -0
- package/dist/format/parse.js.map +1 -0
- package/dist/format/registry.d.ts +24 -0
- package/dist/format/registry.d.ts.map +1 -0
- package/dist/format/registry.js +31 -0
- package/dist/format/registry.js.map +1 -0
- package/dist/format/serialize.d.ts +8 -0
- package/dist/format/serialize.d.ts.map +1 -0
- package/dist/format/serialize.js +30 -0
- package/dist/format/serialize.js.map +1 -0
- package/dist/format/yaml.d.ts +4 -0
- package/dist/format/yaml.d.ts.map +1 -0
- package/dist/format/yaml.js +17 -0
- package/dist/format/yaml.js.map +1 -0
- package/dist/index.js +385 -84
- package/dist/lint/index.d.ts +29 -0
- package/dist/lint/index.d.ts.map +1 -0
- package/dist/lint/index.js +257 -0
- package/dist/lint/index.js.map +1 -0
- package/dist/render.js +174 -38
- package/dist/resolver/attempt.d.ts +2 -0
- package/dist/resolver/attempt.d.ts.map +1 -0
- package/dist/resolver/attempt.js +9 -0
- package/dist/resolver/attempt.js.map +1 -0
- package/dist/resolver/cdnClient.d.ts +46 -0
- package/dist/resolver/cdnClient.d.ts.map +1 -0
- package/dist/resolver/cdnClient.js +107 -0
- package/dist/resolver/cdnClient.js.map +1 -0
- package/dist/resolver/cdnConstants.d.ts +10 -0
- package/dist/resolver/cdnConstants.d.ts.map +1 -0
- package/dist/resolver/cdnConstants.js +10 -0
- package/dist/resolver/cdnConstants.js.map +1 -0
- package/dist/resolver/index.d.ts +20 -0
- package/dist/resolver/index.d.ts.map +1 -0
- package/dist/resolver/index.js +20 -0
- package/dist/resolver/index.js.map +1 -0
- package/dist/resolver/packageRef.d.ts +21 -0
- package/dist/resolver/packageRef.d.ts.map +1 -0
- package/dist/resolver/packageRef.js +89 -0
- package/dist/resolver/packageRef.js.map +1 -0
- package/dist/resolver/packageService.d.ts +40 -0
- package/dist/resolver/packageService.d.ts.map +1 -0
- package/dist/resolver/packageService.js +156 -0
- package/dist/resolver/packageService.js.map +1 -0
- package/dist/resolver/peerResolver.d.ts +33 -0
- package/dist/resolver/peerResolver.d.ts.map +1 -0
- package/dist/resolver/peerResolver.js +66 -0
- package/dist/resolver/peerResolver.js.map +1 -0
- package/dist/resolver/semver.d.ts +17 -0
- package/dist/resolver/semver.d.ts.map +1 -0
- package/dist/resolver/semver.js +56 -0
- package/dist/resolver/semver.js.map +1 -0
- package/dist/resolver/types.d.ts +33 -0
- package/dist/resolver/types.d.ts.map +1 -0
- package/dist/resolver/types.js +2 -0
- package/dist/resolver/types.js.map +1 -0
- package/dist/resolver/versionResolver.d.ts +29 -0
- package/dist/resolver/versionResolver.d.ts.map +1 -0
- package/dist/resolver/versionResolver.js +79 -0
- package/dist/resolver/versionResolver.js.map +1 -0
- package/dist/servers.js +142 -3
- package/dist/transpile/index.d.ts +19 -0
- package/dist/transpile/index.d.ts.map +1 -0
- package/dist/transpile/index.js +23 -0
- package/dist/transpile/index.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +99 -68
|
@@ -108,7 +108,7 @@ body {
|
|
|
108
108
|
+ fixed height centers the glyph run by the box, not by the inherited line-box.
|
|
109
109
|
Opaque so they read cleanly over the message text the transparent strip lets
|
|
110
110
|
through. */
|
|
111
|
-
.pill, .working {
|
|
111
|
+
.pill, .working, .token {
|
|
112
112
|
display: inline-flex;
|
|
113
113
|
align-items: center;
|
|
114
114
|
justify-content: center;
|
|
@@ -145,7 +145,7 @@ body {
|
|
|
145
145
|
doubling the visible gap between its neighbours. Collapse the empties so the
|
|
146
146
|
cluster spaces evenly regardless of which pills are present. */
|
|
147
147
|
.token-wrap:empty, .sid-wrap:empty, .servers-wrap:empty { display: none; }
|
|
148
|
-
.
|
|
148
|
+
.picker, .servers-pop {
|
|
149
149
|
position: absolute;
|
|
150
150
|
bottom: calc(100% + .35rem);
|
|
151
151
|
right: 0;
|
|
@@ -155,24 +155,6 @@ body {
|
|
|
155
155
|
border-radius: 10px;
|
|
156
156
|
box-shadow: 0 8px 30px rgba(0, 0, 0, .45);
|
|
157
157
|
}
|
|
158
|
-
.token-pop {
|
|
159
|
-
min-width: 360px;
|
|
160
|
-
max-width: 92vw;
|
|
161
|
-
padding: .35rem;
|
|
162
|
-
font-size: .85rem;
|
|
163
|
-
}
|
|
164
|
-
/* Each row: left text block, number pushed to the right edge. Sizing/rhythm matches .picker-row
|
|
165
|
-
(font .85rem, row padding .45rem .55rem) so the three status-bar popovers read as one family. */
|
|
166
|
-
.token-line {
|
|
167
|
-
display: flex;
|
|
168
|
-
justify-content: space-between;
|
|
169
|
-
align-items: baseline;
|
|
170
|
-
gap: .6rem;
|
|
171
|
-
padding: .45rem .55rem;
|
|
172
|
-
}
|
|
173
|
-
.token-line-title { font-weight: 600; color: var(--fg); margin-right: .5rem; }
|
|
174
|
-
.token-line-lbl { color: var(--muted); font-size: .72rem; }
|
|
175
|
-
.token-line-num { color: var(--fg); font-variant-numeric: tabular-nums; white-space: nowrap; }
|
|
176
158
|
|
|
177
159
|
/* "working…" indicator: CC is mid-turn. Passive — driven by the live-chain leaf,
|
|
178
160
|
not a live token stream (the JSONL is flushed in batches, so this can sit lit
|
|
@@ -189,7 +171,7 @@ body {
|
|
|
189
171
|
@keyframes working-pulse { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }
|
|
190
172
|
@media (prefers-reduced-motion: reduce) { .working-dot { animation: none; opacity: .8; } }
|
|
191
173
|
|
|
192
|
-
/* Session list — shares the popover chrome (.
|
|
174
|
+
/* Session list — shares the popover chrome (.picker above); these are
|
|
193
175
|
just its own dimensions. */
|
|
194
176
|
.picker {
|
|
195
177
|
width: min(440px, calc(100vw - 2rem));
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ChatMessageDto, UpstreamErrorDto, ReasoningDepth, ChatStreamPieceDto, ChatResponseDto, ProviderResponseDto, ProviderStreamEventDto, StreamErrorCode } from './protocol.js';
|
|
2
|
+
/** Options for building chat request payloads */
|
|
3
|
+
export type ChatRequestOpts = {
|
|
4
|
+
reasoning?: ReasoningDepth;
|
|
5
|
+
webSearch?: boolean;
|
|
6
|
+
} | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Error thrown by provider stream parsing when the provider or server reports an error.
|
|
9
|
+
* Carries structured error info (code, retryable) so callers can handle appropriately.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ProviderStreamError extends Error {
|
|
12
|
+
readonly code: StreamErrorCode;
|
|
13
|
+
readonly retryable: boolean;
|
|
14
|
+
constructor(message: string, code?: StreamErrorCode, retryable?: boolean);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Unified base class for all AI providers.
|
|
18
|
+
* Handles both request building (outbound) and response parsing (inbound).
|
|
19
|
+
*/
|
|
20
|
+
export declare abstract class AIProvider {
|
|
21
|
+
protected abstract readonly providerName: string;
|
|
22
|
+
abstract readonly defaultBaseUrl: string;
|
|
23
|
+
abstract readonly path: string;
|
|
24
|
+
/**
|
|
25
|
+
* Get the request path. Override for providers that need dynamic paths (e.g., model in URL).
|
|
26
|
+
*/
|
|
27
|
+
getPath(_model: string, _stream: boolean): string;
|
|
28
|
+
abstract buildHeaders(apiKey: string, origin?: string): Record<string, string>;
|
|
29
|
+
abstract buildPayload(messages: ChatMessageDto[], model: string, opts: ChatRequestOpts, stream: boolean): unknown;
|
|
30
|
+
abstract parseError(errorText: string, status: number): UpstreamErrorDto;
|
|
31
|
+
abstract parseNonStreamingResponse(json: ProviderResponseDto): ChatResponseDto;
|
|
32
|
+
/**
|
|
33
|
+
* Parse a streaming chunk. Handles server errors (rate limit, etc.) before
|
|
34
|
+
* delegating to provider-specific parsing.
|
|
35
|
+
*/
|
|
36
|
+
parseStreamChunk(json: ProviderStreamEventDto): ChatStreamPieceDto | null;
|
|
37
|
+
protected abstract parseProviderStreamChunk(json: ProviderStreamEventDto): ChatStreamPieceDto | null;
|
|
38
|
+
protected isReasoningEnabled(opts: {
|
|
39
|
+
reasoning?: ReasoningDepth;
|
|
40
|
+
} | undefined): boolean;
|
|
41
|
+
/** Split system messages from conversation messages. */
|
|
42
|
+
protected extractSystemMessages(messages: ChatMessageDto[], separator?: string): {
|
|
43
|
+
system: string | undefined;
|
|
44
|
+
conversationMessages: ChatMessageDto[];
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Parse JSON error response with nested error object (common OpenAI/Anthropic format)
|
|
48
|
+
*/
|
|
49
|
+
protected parseJsonError(errorText: string, status: number, includeCode?: boolean): UpstreamErrorDto;
|
|
50
|
+
/**
|
|
51
|
+
* Check if the JSON contains an error and throw if so.
|
|
52
|
+
* Handles two formats:
|
|
53
|
+
* - Unified format: { type: 'error', code, message, retryable }
|
|
54
|
+
* - Provider format: { error: "message" } or { error: { message: "..." } }
|
|
55
|
+
*/
|
|
56
|
+
private checkAndThrowError;
|
|
57
|
+
private extractErrorMessage;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=aiProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiProvider.d.ts","sourceRoot":"","sources":["../../ai/src/aiProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,sBAAsB,EACtB,eAAe,EAChB,MAAM,eAAe,CAAA;AAEtB,iDAAiD;AACjD,MAAM,MAAM,eAAe,GAAG;IAAE,SAAS,CAAC,EAAE,cAAc,CAAC;IAAC,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAAA;AAE7F;;;GAGG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;aAG1B,IAAI,EAAE,eAAe;aACrB,SAAS,EAAE,OAAO;gBAFlC,OAAO,EAAE,MAAM,EACC,IAAI,GAAE,eAA2B,EACjC,SAAS,GAAE,OAAe;CAI7C;AAED;;;GAGG;AACH,8BAAsB,UAAU;IAC9B,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAChD,QAAQ,CAAC,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;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}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown by provider stream parsing when the provider or server reports an error.
|
|
3
|
+
* Carries structured error info (code, retryable) so callers can handle appropriately.
|
|
4
|
+
*/
|
|
5
|
+
export class ProviderStreamError extends Error {
|
|
6
|
+
constructor(message, code = 'unknown', retryable = false) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.retryable = retryable;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Unified base class for all AI providers.
|
|
14
|
+
* Handles both request building (outbound) and response parsing (inbound).
|
|
15
|
+
*/
|
|
16
|
+
export class AIProvider {
|
|
17
|
+
// ── Request building (outbound) ──────────────────────────────────
|
|
18
|
+
/**
|
|
19
|
+
* Get the request path. Override for providers that need dynamic paths (e.g., model in URL).
|
|
20
|
+
*/
|
|
21
|
+
getPath(_model, _stream) {
|
|
22
|
+
return this.path;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parse a streaming chunk. Handles server errors (rate limit, etc.) before
|
|
26
|
+
* delegating to provider-specific parsing.
|
|
27
|
+
*/
|
|
28
|
+
parseStreamChunk(json) {
|
|
29
|
+
this.checkAndThrowError(json);
|
|
30
|
+
return this.parseProviderStreamChunk(json);
|
|
31
|
+
}
|
|
32
|
+
// ── Shared helpers ───────────────────────────────────────────────
|
|
33
|
+
isReasoningEnabled(opts) {
|
|
34
|
+
return opts?.reasoning !== undefined && opts.reasoning > 0;
|
|
35
|
+
}
|
|
36
|
+
/** Split system messages from conversation messages. */
|
|
37
|
+
extractSystemMessages(messages, separator = '\n\n') {
|
|
38
|
+
const systemParts = messages.filter(m => m.role === 'system').map(m => m.content);
|
|
39
|
+
return {
|
|
40
|
+
system: systemParts.length ? systemParts.join(separator) : undefined,
|
|
41
|
+
conversationMessages: messages.filter(m => m.role !== 'system')
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Parse JSON error response with nested error object (common OpenAI/Anthropic format)
|
|
46
|
+
*/
|
|
47
|
+
parseJsonError(errorText, status, includeCode = false) {
|
|
48
|
+
if (!errorText) {
|
|
49
|
+
return { message: `HTTP ${status}` };
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(errorText);
|
|
53
|
+
if (parsed.error && typeof parsed.error === 'object') {
|
|
54
|
+
const result = {
|
|
55
|
+
message: parsed.error.message || `HTTP ${status}`,
|
|
56
|
+
type: parsed.error.type
|
|
57
|
+
};
|
|
58
|
+
if (includeCode) {
|
|
59
|
+
result.code = parsed.error.code;
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
if (parsed.message) {
|
|
64
|
+
return { message: parsed.message };
|
|
65
|
+
}
|
|
66
|
+
return { message: errorText };
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return { message: errorText };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Check if the JSON contains an error and throw if so.
|
|
74
|
+
* Handles two formats:
|
|
75
|
+
* - Unified format: { type: 'error', code, message, retryable }
|
|
76
|
+
* - Provider format: { error: "message" } or { error: { message: "..." } }
|
|
77
|
+
*/
|
|
78
|
+
checkAndThrowError(json) {
|
|
79
|
+
if (typeof json !== 'object' || json === null)
|
|
80
|
+
return;
|
|
81
|
+
const obj = json;
|
|
82
|
+
// Unified error format (used by both chat and infer endpoints)
|
|
83
|
+
if (obj.type === 'error' && 'message' in obj) {
|
|
84
|
+
const code = obj.code || 'unknown';
|
|
85
|
+
throw new ProviderStreamError(obj.message, code, !!obj.retryable);
|
|
86
|
+
}
|
|
87
|
+
// Provider error format (OpenAI/Anthropic style)
|
|
88
|
+
if (obj.error) {
|
|
89
|
+
throw new ProviderStreamError(this.extractErrorMessage(obj.error));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
extractErrorMessage(error) {
|
|
93
|
+
if (typeof error === 'string') {
|
|
94
|
+
try {
|
|
95
|
+
const parsed = JSON.parse(error);
|
|
96
|
+
return parsed.error?.message || parsed.message || error;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (typeof error === 'object' && error !== null) {
|
|
103
|
+
const err = error;
|
|
104
|
+
return err.message || JSON.stringify(error);
|
|
105
|
+
}
|
|
106
|
+
return `${this.providerName} returned an error`;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=aiProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiProvider.js","sourceRoot":"","sources":["../../ai/src/aiProvider.ts"],"names":[],"mappings":"AAcA;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,YACE,OAAe,EACC,OAAwB,SAAS,EACjC,YAAqB,KAAK;QAE1C,KAAK,CAAC,OAAO,CAAC,CAAA;QAHE,SAAI,GAAJ,IAAI,CAA6B;QACjC,cAAS,GAAT,SAAS,CAAiB;IAG5C,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAgB,UAAU;IAK9B,oEAAoE;IAEpE;;OAEG;IACH,OAAO,CAAC,MAAc,EAAE,OAAgB;QACtC,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;IAiBD;;;OAGG;IACH,gBAAgB,CAAC,IAA4B;QAC3C,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAA;QAC7B,OAAO,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAA;IAC5C,CAAC;IAID,oEAAoE;IAE1D,kBAAkB,CAAC,IAAgD;QAC3E,OAAO,IAAI,EAAE,SAAS,KAAK,SAAS,IAAI,IAAI,CAAC,SAAS,GAAG,CAAC,CAAA;IAC5D,CAAC;IAED,wDAAwD;IAC9C,qBAAqB,CAAC,QAA0B,EAAE,SAAS,GAAG,MAAM;QAI5E,MAAM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CAAA;QACzD,CAAC;QACD,OAAO,GAAG,IAAI,CAAC,YAAY,oBAAoB,CAAA;IACjD,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ProviderProtocol, ChatMessageDto, ReasoningDepth, StreamErrorPayload } from './protocol.js';
|
|
2
|
+
import { AIProvider } from './aiProvider.js';
|
|
3
|
+
/** Get the provider implementation for a given protocol. */
|
|
4
|
+
export declare function getProvider(protocol: ProviderProtocol): AIProvider;
|
|
5
|
+
/** Resolved provider credentials for AI requests */
|
|
6
|
+
export interface ResolvedAIProvider {
|
|
7
|
+
apiKey: string;
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
protocol: ProviderProtocol;
|
|
10
|
+
model: string;
|
|
11
|
+
isFreeModel: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** Options for sending AI requests */
|
|
14
|
+
export interface SendAIRequestOpts {
|
|
15
|
+
model: string;
|
|
16
|
+
messages: ChatMessageDto[];
|
|
17
|
+
stream: boolean;
|
|
18
|
+
reasoning?: ReasoningDepth;
|
|
19
|
+
webSearch?: boolean;
|
|
20
|
+
origin?: string;
|
|
21
|
+
signal?: AbortSignal;
|
|
22
|
+
modifyPayload?: (payload: Record<string, unknown>) => void;
|
|
23
|
+
}
|
|
24
|
+
/** Send a request to an AI provider */
|
|
25
|
+
export declare function sendAIRequest(provider: ResolvedAIProvider, opts: SendAIRequestOpts): Promise<Response>;
|
|
26
|
+
/** Parse an upstream error response into our standard error payload */
|
|
27
|
+
export declare function normalizeUpstreamError(response: Response, protocol: ProviderProtocol): Promise<Required<StreamErrorPayload>>;
|
|
28
|
+
//# sourceMappingURL=aiProviders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aiProviders.d.ts","sourceRoot":"","sources":["../../ai/src/aiProviders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AACzG,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAa5C,4DAA4D;AAC5D,wBAAgB,WAAW,CAAC,QAAQ,EAAE,gBAAgB,GAAG,UAAU,CAMlE;AAED,oDAAoD;AACpD,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,gBAAgB,CAAA;IAC1B,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,OAAO,CAAA;CACrB;AAED,sCAAsC;AACtC,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,cAAc,EAAE,CAAA;IAC1B,MAAM,EAAE,OAAO,CAAA;IACf,SAAS,CAAC,EAAE,cAAc,CAAA;IAC1B,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAA;CAC3D;AAED,uCAAuC;AACvC,wBAAsB,aAAa,CACjC,QAAQ,EAAE,kBAAkB,EAC5B,IAAI,EAAE,iBAAiB,GACtB,OAAO,CAAC,QAAQ,CAAC,CAqBnB;AAED,uEAAuE;AACvE,wBAAsB,sBAAsB,CAC1C,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,CAWvC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { OpenAIProvider } from './providers/openAI.js';
|
|
2
|
+
import { OpenRouterProvider } from './providers/openRouter.js';
|
|
3
|
+
import { AnthropicProvider } from './providers/anthropic.js';
|
|
4
|
+
import { GeminiProvider } from './providers/gemini.js';
|
|
5
|
+
const providers = new Map([
|
|
6
|
+
['openai', new OpenAIProvider()],
|
|
7
|
+
['openrouter', new OpenRouterProvider()],
|
|
8
|
+
['anthropic', new AnthropicProvider()],
|
|
9
|
+
['gemini', new GeminiProvider()]
|
|
10
|
+
]);
|
|
11
|
+
/** Get the provider implementation for a given protocol. */
|
|
12
|
+
export function getProvider(protocol) {
|
|
13
|
+
const provider = providers.get(protocol);
|
|
14
|
+
if (!provider) {
|
|
15
|
+
throw new Error(`Unsupported protocol: ${protocol}`);
|
|
16
|
+
}
|
|
17
|
+
return provider;
|
|
18
|
+
}
|
|
19
|
+
/** Send a request to an AI provider */
|
|
20
|
+
export async function sendAIRequest(provider, opts) {
|
|
21
|
+
const spec = getProvider(provider.protocol);
|
|
22
|
+
const path = spec.getPath(opts.model, opts.stream);
|
|
23
|
+
const url = new URL(path, provider.baseUrl).toString();
|
|
24
|
+
const headers = spec.buildHeaders(provider.apiKey, opts.origin);
|
|
25
|
+
const payload = spec.buildPayload(opts.messages, opts.model, { reasoning: opts.reasoning, webSearch: opts.webSearch }, opts.stream);
|
|
26
|
+
opts.modifyPayload?.(payload);
|
|
27
|
+
return fetch(url, {
|
|
28
|
+
method: 'POST',
|
|
29
|
+
headers,
|
|
30
|
+
body: JSON.stringify(payload),
|
|
31
|
+
signal: opts.signal
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** Parse an upstream error response into our standard error payload */
|
|
35
|
+
export async function normalizeUpstreamError(response, protocol) {
|
|
36
|
+
const spec = getProvider(protocol);
|
|
37
|
+
const errorText = await response.text().catch(() => '');
|
|
38
|
+
const { message } = spec.parseError(errorText, response.status);
|
|
39
|
+
const status = response.status;
|
|
40
|
+
let code = 'unknown';
|
|
41
|
+
if (status === 429)
|
|
42
|
+
code = 'rate_limit';
|
|
43
|
+
else if (status === 413)
|
|
44
|
+
code = 'context_exceeded';
|
|
45
|
+
return { code, message, retryable: status === 429 };
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=aiProviders.js.map
|
|
@@ -0,0 +1 @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Common chat types shared across all providers
|
|
3
|
+
*/
|
|
4
|
+
export type ChatRole = 'user' | 'assistant' | 'system';
|
|
5
|
+
export interface ChatMessageDto {
|
|
6
|
+
role: ChatRole;
|
|
7
|
+
content: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ChatResponseDto {
|
|
10
|
+
text: string;
|
|
11
|
+
reasoning?: string;
|
|
12
|
+
status?: 'complete' | 'interrupted' | 'failed' | 'cancelled';
|
|
13
|
+
error?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface UpstreamErrorDto {
|
|
16
|
+
message: string;
|
|
17
|
+
type?: string;
|
|
18
|
+
code?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ChatStreamPieceDto {
|
|
21
|
+
text?: string;
|
|
22
|
+
reasoning?: string;
|
|
23
|
+
}
|
|
24
|
+
/** A provider's raw JSON response body. Each AIProvider narrows it to its own DTO — the interfaces that extend this (see providers/*). */
|
|
25
|
+
export interface ProviderResponseDto {
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
/** One raw JSON SSE event from a provider stream. Each AIProvider casts it to its own *SseEventDto (see providers/*). */
|
|
29
|
+
export interface ProviderStreamEventDto {
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=chat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../ai/src/chat.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,WAAW,GAAG,QAAQ,CAAA;AAEtD,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,QAAQ,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,MAAM,CAAC,EAAE,UAAU,GAAG,aAAa,GAAG,QAAQ,GAAG,WAAW,CAAA;IAC5D,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAGD,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAGD,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,0IAA0I;AAC1I,MAAM,WAAW,mBAAmB;IAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE;AAE/D,yHAAyH;AACzH,MAAM,WAAW,sBAAsB;IAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE"}
|
package/dist/ai/chat.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat.js","sourceRoot":"","sources":["../../ai/src/chat.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './protocol.js';
|
|
2
|
+
export { AIProvider, ProviderStreamError, type ChatRequestOpts } from './aiProvider.js';
|
|
3
|
+
export { getProvider, sendAIRequest, normalizeUpstreamError, type ResolvedAIProvider, type SendAIRequestOpts } from './aiProviders.js';
|
|
4
|
+
export { parseSseBlock, consumeSseStream, consumeStreamText } from './sseParser.js';
|
|
5
|
+
export * from './providers/anthropic.js';
|
|
6
|
+
export * from './providers/gemini.js';
|
|
7
|
+
export * from './providers/openAI.js';
|
|
8
|
+
export * from './providers/openRouter.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../ai/src/index.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAA;AAE7B,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACvF,OAAO,EACL,WAAW,EACX,aAAa,EACb,sBAAsB,EACtB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACvB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAGnF,cAAc,0BAA0B,CAAA;AACxC,cAAc,uBAAuB,CAAA;AACrC,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA"}
|
package/dist/ai/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// AI wire protocol types (chat/stream DTOs, ProviderProtocol/ReasoningDepth/TbModelDto)
|
|
2
|
+
export * from './protocol.js';
|
|
3
|
+
export { AIProvider, ProviderStreamError } from './aiProvider.js';
|
|
4
|
+
export { getProvider, sendAIRequest, normalizeUpstreamError } from './aiProviders.js';
|
|
5
|
+
export { parseSseBlock, consumeSseStream, consumeStreamText } from './sseParser.js';
|
|
6
|
+
// Per-provider wire DTOs + provider implementations.
|
|
7
|
+
export * from './providers/anthropic.js';
|
|
8
|
+
export * from './providers/gemini.js';
|
|
9
|
+
export * from './providers/openAI.js';
|
|
10
|
+
export * from './providers/openRouter.js';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ai/src/index.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,cAAc,eAAe,CAAA;AAE7B,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAwB,MAAM,iBAAiB,CAAA;AACvF,OAAO,EACL,WAAW,EACX,aAAa,EACb,sBAAsB,EAGvB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAEnF,qDAAqD;AACrD,cAAc,0BAA0B,CAAA;AACxC,cAAc,uBAAuB,CAAA;AACrC,cAAc,uBAAuB,CAAA;AACrC,cAAc,2BAA2B,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './chat.js';
|
|
2
|
+
export * from './stream.js';
|
|
3
|
+
/** AI provider wire protocol identifier */
|
|
4
|
+
export type ProviderProtocol = 'openai' | 'anthropic' | 'openrouter' | 'gemini';
|
|
5
|
+
/** Extended-thinking hint, 0 (off) – 3 (max) */
|
|
6
|
+
export type ReasoningDepth = 0 | 1 | 2 | 3;
|
|
7
|
+
/** tb.models() response — model available to the current user */
|
|
8
|
+
export interface TbModelDto {
|
|
9
|
+
/** Provider protocol: "anthropic", "openai", "gemini", "openrouter" */
|
|
10
|
+
provider: string;
|
|
11
|
+
/** Model identifier, e.g. "claude-sonnet-4-6" */
|
|
12
|
+
name: string;
|
|
13
|
+
/** Human-readable display name, e.g. "Sonnet 4.6" */
|
|
14
|
+
friendlyName: string;
|
|
15
|
+
/** Provider display name, e.g. "Anthropic" */
|
|
16
|
+
providerName: string;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../ai/src/protocol.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAE3B,2CAA2C;AAC3C,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG,WAAW,GAAG,YAAY,GAAG,QAAQ,CAAA;AAE/E,gDAAgD;AAChD,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AAE1C,iEAAiE;AACjE,MAAM,WAAW,UAAU;IACzB,uEAAuE;IACvE,QAAQ,EAAE,MAAM,CAAA;IAChB,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAA;IACZ,qDAAqD;IACrD,YAAY,EAAE,MAAM,CAAA;IACpB,8CAA8C;IAC9C,YAAY,EAAE,MAAM,CAAA;CACrB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../ai/src/protocol.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic Messages API (/v1/messages) — wire types and provider implementation.
|
|
3
|
+
*/
|
|
4
|
+
import type { ChatMessageDto, UpstreamErrorDto, ChatStreamPieceDto, ChatResponseDto, ProviderResponseDto, ProviderStreamEventDto } from '../protocol.js';
|
|
5
|
+
import { AIProvider, type ChatRequestOpts } from '../aiProvider.js';
|
|
6
|
+
export interface AnthropicTextContent {
|
|
7
|
+
type: 'text';
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
export interface AnthropicThinkingContent {
|
|
11
|
+
type: 'thinking';
|
|
12
|
+
thinking: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AnthropicNonStreamingResponseDto extends ProviderResponseDto {
|
|
15
|
+
type: 'message';
|
|
16
|
+
content: Array<AnthropicTextContent | AnthropicThinkingContent>;
|
|
17
|
+
}
|
|
18
|
+
export interface AnthropicErrorResponseDto {
|
|
19
|
+
type: 'error';
|
|
20
|
+
error: {
|
|
21
|
+
type: string;
|
|
22
|
+
message: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface AnthropicContentBlockDeltaDto {
|
|
26
|
+
type: 'content_block_delta';
|
|
27
|
+
index: number;
|
|
28
|
+
delta: {
|
|
29
|
+
type: 'text_delta';
|
|
30
|
+
text: string;
|
|
31
|
+
} | {
|
|
32
|
+
type: 'thinking_delta';
|
|
33
|
+
thinking: string;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export interface AnthropicContentBlockStartDto {
|
|
37
|
+
type: 'content_block_start';
|
|
38
|
+
index: number;
|
|
39
|
+
content_block: {
|
|
40
|
+
type: 'text';
|
|
41
|
+
text: string;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export interface AnthropicContentBlockStopDto {
|
|
45
|
+
type: 'content_block_stop';
|
|
46
|
+
index: number;
|
|
47
|
+
}
|
|
48
|
+
export interface AnthropicMessageStartDto {
|
|
49
|
+
type: 'message_start';
|
|
50
|
+
message: Record<string, unknown>;
|
|
51
|
+
}
|
|
52
|
+
export interface AnthropicMessageDeltaDto {
|
|
53
|
+
type: 'message_delta';
|
|
54
|
+
delta: Record<string, unknown>;
|
|
55
|
+
usage?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
export interface AnthropicMessageStopDto {
|
|
58
|
+
type: 'message_stop';
|
|
59
|
+
}
|
|
60
|
+
export interface AnthropicPingDto {
|
|
61
|
+
type: 'ping';
|
|
62
|
+
}
|
|
63
|
+
export type AnthropicSseEventDto = AnthropicContentBlockDeltaDto | AnthropicContentBlockStartDto | AnthropicContentBlockStopDto | AnthropicMessageStartDto | AnthropicMessageDeltaDto | AnthropicMessageStopDto | AnthropicPingDto | AnthropicErrorResponseDto;
|
|
64
|
+
/** Anthropic thinking (extended reasoning) */
|
|
65
|
+
type AnthropicThinkingConfig = {
|
|
66
|
+
type: 'enabled';
|
|
67
|
+
budget_tokens: number;
|
|
68
|
+
} | {
|
|
69
|
+
type: 'adaptive';
|
|
70
|
+
};
|
|
71
|
+
/** Anthropic web search tool definition */
|
|
72
|
+
interface AnthropicWebSearchTool {
|
|
73
|
+
type: 'web_search_20250305';
|
|
74
|
+
name: 'web_search';
|
|
75
|
+
max_uses?: number;
|
|
76
|
+
}
|
|
77
|
+
/** Ephemeral prompt-cache breakpoint */
|
|
78
|
+
type CacheControl = {
|
|
79
|
+
type: 'ephemeral';
|
|
80
|
+
};
|
|
81
|
+
/** Outbound text block with an optional cache breakpoint (request-side only) */
|
|
82
|
+
type AnthropicTextBlock = {
|
|
83
|
+
type: 'text';
|
|
84
|
+
text: string;
|
|
85
|
+
cache_control?: CacheControl;
|
|
86
|
+
};
|
|
87
|
+
/** Message whose content may be a plain string or block array (block form carries cache_control) */
|
|
88
|
+
type AnthropicMessage = {
|
|
89
|
+
role: ChatMessageDto['role'];
|
|
90
|
+
content: string | AnthropicTextBlock[];
|
|
91
|
+
};
|
|
92
|
+
/** Anthropic request payload */
|
|
93
|
+
export type AnthropicRequestPayload = {
|
|
94
|
+
model: string;
|
|
95
|
+
messages: AnthropicMessage[];
|
|
96
|
+
max_tokens: number;
|
|
97
|
+
stream: boolean;
|
|
98
|
+
system?: string | AnthropicTextBlock[];
|
|
99
|
+
thinking?: AnthropicThinkingConfig;
|
|
100
|
+
output_config?: {
|
|
101
|
+
effort: 'low' | 'medium' | 'high' | 'max';
|
|
102
|
+
};
|
|
103
|
+
tools?: AnthropicWebSearchTool[];
|
|
104
|
+
};
|
|
105
|
+
export declare class AnthropicProvider extends AIProvider {
|
|
106
|
+
protected readonly providerName = "Anthropic";
|
|
107
|
+
readonly defaultBaseUrl = "https://api.anthropic.com";
|
|
108
|
+
readonly path = "/v1/messages";
|
|
109
|
+
buildHeaders(apiKey: string): Record<string, string>;
|
|
110
|
+
buildPayload(messages: ChatMessageDto[], model: string, opts: ChatRequestOpts, stream: boolean): AnthropicRequestPayload;
|
|
111
|
+
parseError(errorText: string, status: number): UpstreamErrorDto;
|
|
112
|
+
parseNonStreamingResponse(json: ProviderResponseDto): ChatResponseDto;
|
|
113
|
+
protected parseProviderStreamChunk(json: ProviderStreamEventDto): ChatStreamPieceDto | null;
|
|
114
|
+
/** Convert the final message to block form with a cache breakpoint; others pass through as-is. */
|
|
115
|
+
private withLastMessageCached;
|
|
116
|
+
private isAnthropicResponse;
|
|
117
|
+
private isAnthropicEvent;
|
|
118
|
+
/** Claude 4.6+: adaptive thinking, higher output limit */
|
|
119
|
+
private isModernModel;
|
|
120
|
+
private getMaxTokens;
|
|
121
|
+
}
|
|
122
|
+
export {};
|
|
123
|
+
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1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