@molecule/api-ai-deepseek 1.0.5 → 1.0.7
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/LICENSE +111 -24
- package/README.md +68 -7
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -0
- package/dist/provider.d.ts +29 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +271 -36
- package/dist/types.d.ts +26 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
Apache License
|
|
2
3
|
Version 2.0, January 2004
|
|
3
4
|
http://www.apache.org/licenses/
|
|
@@ -34,21 +35,33 @@
|
|
|
34
35
|
|
|
35
36
|
"Work" shall mean the work of authorship, whether in Source or
|
|
36
37
|
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
38
40
|
|
|
39
41
|
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
42
|
form, that is based on (or derived from) the Work and for which the
|
|
41
43
|
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
-
represent, as a whole, an original work of authorship.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
49
62
|
|
|
50
63
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
-
on behalf of whom a Contribution has been received by
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
52
65
|
subsequently incorporated within the Work.
|
|
53
66
|
|
|
54
67
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
@@ -61,8 +74,18 @@
|
|
|
61
74
|
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
75
|
this License, each Contributor hereby grants to You a perpetual,
|
|
63
76
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
-
|
|
65
|
-
and otherwise transfer the Work
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
66
89
|
|
|
67
90
|
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
91
|
Work or Derivative Works thereof in any medium, with or without
|
|
@@ -84,23 +107,87 @@
|
|
|
84
107
|
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
108
|
distribution, then any Derivative Works that You distribute must
|
|
86
109
|
include a readable copy of the attribution notices contained
|
|
87
|
-
within such NOTICE file
|
|
88
|
-
|
|
89
|
-
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
90
138
|
|
|
91
139
|
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
-
names, trademarks, service marks, or product names of the Licensor
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
93
143
|
|
|
94
144
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
-
agreed to in writing, Licensor provides the Work
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
104
191
|
|
|
105
192
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
193
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-10-16T10:23:43.043Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-ai-deepseek
|
|
@@ -14,6 +14,26 @@ Generated: 2026-08-04T01:47:29.897Z
|
|
|
14
14
|
|
|
15
15
|
DeepSeek AI provider for molecule.dev.
|
|
16
16
|
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
// npm install @molecule/api-ai-deepseek --workspace=api
|
|
21
|
+
import { setProvider, requireProvider } from '@molecule/api-ai'
|
|
22
|
+
import { provider } from '@molecule/api-ai-deepseek'
|
|
23
|
+
|
|
24
|
+
// Named registration — several AI providers can be bonded side by side
|
|
25
|
+
// (getProviderByName('deepseek') targets this one); the FIRST one
|
|
26
|
+
// registered also answers requireProvider() (see @molecule/api-ai).
|
|
27
|
+
setProvider('deepseek', provider) // reads DEEPSEEK_API_KEY from the environment
|
|
28
|
+
|
|
29
|
+
let reply = ''
|
|
30
|
+
for await (const event of requireProvider().chat({
|
|
31
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
32
|
+
})) {
|
|
33
|
+
if (event.type === 'text') reply += event.content // or forward the chunk to the client (SSE)
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
17
37
|
## Type
|
|
18
38
|
|
|
19
39
|
`provider`
|
|
@@ -60,6 +80,31 @@ interface DeepseekConfig {
|
|
|
60
80
|
modelMap?: Record<string, string>
|
|
61
81
|
/** Called on each rate-limited/overloaded upstream response, before any retry sleep. */
|
|
62
82
|
onRateLimit?: AiRateLimitCallback
|
|
83
|
+
/** Called when a stream accepts the request and then produces no content for the whole first-token window. */
|
|
84
|
+
onStreamStall?: AiStreamStallCallback
|
|
85
|
+
/**
|
|
86
|
+
* How long a stream may wait for its FIRST content event before the provider
|
|
87
|
+
* abandons it and asks again (ms). Defaults to 45 000.
|
|
88
|
+
*
|
|
89
|
+
* Counted from the moment the request is sent, and cleared by the first
|
|
90
|
+
* `text` / `thinking` / `tool_use` event — NOT by a liveness frame, because
|
|
91
|
+
* DeepSeek keeps a stalled prefill's connection alive with `: keep-alive`
|
|
92
|
+
* comments, so a byte-level idle timeout never sees the stall at all. A turn's
|
|
93
|
+
* healthy first-token wait is ~1 s even on million-token prompts, so this
|
|
94
|
+
* window is a stall bound, not a latency budget.
|
|
95
|
+
*/
|
|
96
|
+
firstTokenTimeoutMs?: number
|
|
97
|
+
/**
|
|
98
|
+
* How long a stream that has ALREADY started may go without a single byte
|
|
99
|
+
* before the provider abandons it (ms). Defaults to 60 000.
|
|
100
|
+
*
|
|
101
|
+
* Measured over the socket, not over content: once the reply has started, any
|
|
102
|
+
* byte proves the connection is alive, and a stretch with none is a broken
|
|
103
|
+
* stream rather than a slow one. Abandoning it here is not retried — the
|
|
104
|
+
* partial reply is already on the wire — it is reported as an error event for
|
|
105
|
+
* the caller's own ladder, which keeps the partial.
|
|
106
|
+
*/
|
|
107
|
+
streamSilenceTimeoutMs?: number
|
|
63
108
|
}
|
|
64
109
|
```
|
|
65
110
|
|
|
@@ -93,12 +138,20 @@ function createProvider(config?: DeepseekConfig): AIProvider
|
|
|
93
138
|
|
|
94
139
|
### Constants
|
|
95
140
|
|
|
96
|
-
#### `
|
|
141
|
+
#### `provider`
|
|
97
142
|
|
|
98
|
-
|
|
143
|
+
The provider implementation.
|
|
99
144
|
|
|
100
145
|
```typescript
|
|
101
|
-
const
|
|
146
|
+
const provider: AIProvider
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
#### `DEFAULT_FIRST_TOKEN_TIMEOUT_MS`
|
|
150
|
+
|
|
151
|
+
How long a stream may wait for its first content event.
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
const DEFAULT_FIRST_TOKEN_TIMEOUT_MS: 45000
|
|
102
155
|
```
|
|
103
156
|
|
|
104
157
|
#### `DeepseekAIProvider`
|
|
@@ -109,12 +162,12 @@ Back-compat alias for the provider class (the scaffold exported this name).
|
|
|
109
162
|
const DeepseekAIProvider: typeof DeepseekAIProviderImpl
|
|
110
163
|
```
|
|
111
164
|
|
|
112
|
-
#### `
|
|
165
|
+
#### `aiDeepseekSecretDefinitions`
|
|
113
166
|
|
|
114
|
-
|
|
167
|
+
Secret definitions required by the DeepSeek AI bond.
|
|
115
168
|
|
|
116
169
|
```typescript
|
|
117
|
-
const
|
|
170
|
+
const aiDeepseekSecretDefinitions: SecretDefinition[]
|
|
118
171
|
```
|
|
119
172
|
|
|
120
173
|
## Core Interface
|
|
@@ -169,6 +222,14 @@ not at bond/module-load time) — it never silently sends an empty key.
|
|
|
169
222
|
malformed tool schema) gets its own non-retryable message distinct from the generic
|
|
170
223
|
"AI service error. Please try again." used for retryable failures.
|
|
171
224
|
|
|
225
|
+
**A stream that opens and then produces nothing is bounded, not waited on.** DeepSeek answers
|
|
226
|
+
200 and then keeps a stalled prefill alive with `: keep-alive` comments, so no HTTP status is
|
|
227
|
+
ever wrong and a byte-level idle timeout never sees the stall. `chat()` therefore bounds the
|
|
228
|
+
wait for the first CONTENT event (45 s, `MOL_DEEPSEEK_FIRST_TOKEN_TIMEOUT_MS`) and asks once
|
|
229
|
+
more with a fresh stream; after the reply has started it bounds the silence between chunks
|
|
230
|
+
instead (60 s, `MOL_DEEPSEEK_STREAM_SILENCE_TIMEOUT_MS`) and surfaces an error event, keeping
|
|
231
|
+
the partial reply. Hosts observe both through the `onStreamStall` callback.
|
|
232
|
+
|
|
172
233
|
## E2E Tests
|
|
173
234
|
|
|
174
235
|
Integration checklist — drive the real UI (live preview, no mocks), adapt
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* DeepSeek AI provider for molecule.dev.
|
|
3
3
|
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* // npm install @molecule/api-ai-deepseek --workspace=api
|
|
7
|
+
* import { setProvider, requireProvider } from '@molecule/api-ai'
|
|
8
|
+
* import { provider } from '@molecule/api-ai-deepseek'
|
|
9
|
+
*
|
|
10
|
+
* // Named registration — several AI providers can be bonded side by side
|
|
11
|
+
* // (getProviderByName('deepseek') targets this one); the FIRST one
|
|
12
|
+
* // registered also answers requireProvider() (see @molecule/api-ai).
|
|
13
|
+
* setProvider('deepseek', provider) // reads DEEPSEEK_API_KEY from the environment
|
|
14
|
+
*
|
|
15
|
+
* let reply = ''
|
|
16
|
+
* for await (const event of requireProvider().chat({
|
|
17
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
18
|
+
* })) {
|
|
19
|
+
* if (event.type === 'text') reply += event.content // or forward the chunk to the client (SSE)
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
4
23
|
* @remarks
|
|
5
24
|
* Config: `DEEPSEEK_API_KEY` (SERVER-side only) plus an optional default model id/base URL.
|
|
6
25
|
*
|
|
@@ -12,6 +31,14 @@
|
|
|
12
31
|
* malformed tool schema) gets its own non-retryable message distinct from the generic
|
|
13
32
|
* "AI service error. Please try again." used for retryable failures.
|
|
14
33
|
*
|
|
34
|
+
* **A stream that opens and then produces nothing is bounded, not waited on.** DeepSeek answers
|
|
35
|
+
* 200 and then keeps a stalled prefill alive with `: keep-alive` comments, so no HTTP status is
|
|
36
|
+
* ever wrong and a byte-level idle timeout never sees the stall. `chat()` therefore bounds the
|
|
37
|
+
* wait for the first CONTENT event (45 s, `MOL_DEEPSEEK_FIRST_TOKEN_TIMEOUT_MS`) and asks once
|
|
38
|
+
* more with a fresh stream; after the reply has started it bounds the silence between chunks
|
|
39
|
+
* instead (60 s, `MOL_DEEPSEEK_STREAM_SILENCE_TIMEOUT_MS`) and surfaces an error event, keeping
|
|
40
|
+
* the partial reply. Hosts observe both through the `onStreamStall` callback.
|
|
41
|
+
*
|
|
15
42
|
* @module
|
|
16
43
|
*/
|
|
17
44
|
export * from './browser-guard.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAE1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAMlD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,UAUrB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* DeepSeek AI provider for molecule.dev.
|
|
3
3
|
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* // npm install @molecule/api-ai-deepseek --workspace=api
|
|
7
|
+
* import { setProvider, requireProvider } from '@molecule/api-ai'
|
|
8
|
+
* import { provider } from '@molecule/api-ai-deepseek'
|
|
9
|
+
*
|
|
10
|
+
* // Named registration — several AI providers can be bonded side by side
|
|
11
|
+
* // (getProviderByName('deepseek') targets this one); the FIRST one
|
|
12
|
+
* // registered also answers requireProvider() (see @molecule/api-ai).
|
|
13
|
+
* setProvider('deepseek', provider) // reads DEEPSEEK_API_KEY from the environment
|
|
14
|
+
*
|
|
15
|
+
* let reply = ''
|
|
16
|
+
* for await (const event of requireProvider().chat({
|
|
17
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
18
|
+
* })) {
|
|
19
|
+
* if (event.type === 'text') reply += event.content // or forward the chunk to the client (SSE)
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
4
23
|
* @remarks
|
|
5
24
|
* Config: `DEEPSEEK_API_KEY` (SERVER-side only) plus an optional default model id/base URL.
|
|
6
25
|
*
|
|
@@ -12,6 +31,14 @@
|
|
|
12
31
|
* malformed tool schema) gets its own non-retryable message distinct from the generic
|
|
13
32
|
* "AI service error. Please try again." used for retryable failures.
|
|
14
33
|
*
|
|
34
|
+
* **A stream that opens and then produces nothing is bounded, not waited on.** DeepSeek answers
|
|
35
|
+
* 200 and then keeps a stalled prefill alive with `: keep-alive` comments, so no HTTP status is
|
|
36
|
+
* ever wrong and a byte-level idle timeout never sees the stall. `chat()` therefore bounds the
|
|
37
|
+
* wait for the first CONTENT event (45 s, `MOL_DEEPSEEK_FIRST_TOKEN_TIMEOUT_MS`) and asks once
|
|
38
|
+
* more with a fresh stream; after the reply has started it bounds the silence between chunks
|
|
39
|
+
* instead (60 s, `MOL_DEEPSEEK_STREAM_SILENCE_TIMEOUT_MS`) and surfaces an error event, keeping
|
|
40
|
+
* the partial reply. Hosts observe both through the `onStreamStall` callback.
|
|
41
|
+
*
|
|
15
42
|
* @module
|
|
16
43
|
*/
|
|
17
44
|
export * from './browser-guard.js';
|
package/dist/provider.d.ts
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
import type { AIProvider, ChatEvent, ChatParams } from '@molecule/api-ai';
|
|
12
12
|
import './secrets.js';
|
|
13
13
|
import type { DeepseekConfig } from './types.js';
|
|
14
|
+
/** How long a stream may wait for its first content event. */
|
|
15
|
+
export declare const DEFAULT_FIRST_TOKEN_TIMEOUT_MS = 45000;
|
|
14
16
|
/**
|
|
15
17
|
* DeepSeek AI provider that implements the `AIProvider` interface using
|
|
16
18
|
* DeepSeek's OpenAI-compatible Chat Completions API with support for streaming,
|
|
@@ -25,6 +27,9 @@ declare class DeepseekAIProviderImpl implements AIProvider {
|
|
|
25
27
|
private completionsPath;
|
|
26
28
|
private modelMap;
|
|
27
29
|
private onRateLimit?;
|
|
30
|
+
private onStreamStall?;
|
|
31
|
+
private firstTokenTimeoutMs;
|
|
32
|
+
private streamSilenceTimeoutMs;
|
|
28
33
|
constructor(config?: DeepseekConfig);
|
|
29
34
|
/**
|
|
30
35
|
* Sends a chat request and yields streamed events (text chunks, tool use, done/error).
|
|
@@ -33,6 +38,26 @@ declare class DeepseekAIProviderImpl implements AIProvider {
|
|
|
33
38
|
* @yields {ChatEvent} Chat events including text chunks, tool use requests, done signals, and errors.
|
|
34
39
|
*/
|
|
35
40
|
chat(params: ChatParams): AsyncIterable<ChatEvent>;
|
|
41
|
+
/**
|
|
42
|
+
* POST the chat body, retrying with exponential backoff on a rate-limited
|
|
43
|
+
* (429) or overloaded (503) status. Extracted from {@link chat} so the
|
|
44
|
+
* stream-level first-token retry can wrap it: a rate-limited request and a
|
|
45
|
+
* stalled stream are different failures with different budgets, and nesting
|
|
46
|
+
* them in one loop made each one's attempt count depend on the other's.
|
|
47
|
+
*
|
|
48
|
+
* @param args - The request body, headers, upstream model and abort signal.
|
|
49
|
+
* @returns The upstream response (possibly a 429/503 that exhausted retries).
|
|
50
|
+
*/
|
|
51
|
+
private requestWithRateLimitRetries;
|
|
52
|
+
/**
|
|
53
|
+
* Record one stalled attempt: a warn line always, plus the host's
|
|
54
|
+
* `onStreamStall` callback so the gap is queryable from the turn's own
|
|
55
|
+
* analytics rather than only from a log buffer that has since rotated.
|
|
56
|
+
*
|
|
57
|
+
* @param model - The upstream model id the stalled request targeted.
|
|
58
|
+
* @param stall - What the stalled attempt looked like.
|
|
59
|
+
*/
|
|
60
|
+
private reportStreamStall;
|
|
36
61
|
/**
|
|
37
62
|
* Converts internal `ChatMessage` objects to the OpenAI-compatible message format.
|
|
38
63
|
* System prompt is prepended as a system role message.
|
|
@@ -61,6 +86,10 @@ declare class DeepseekAIProviderImpl implements AIProvider {
|
|
|
61
86
|
* format), yielding `ChatEvent` objects as they arrive.
|
|
62
87
|
*
|
|
63
88
|
* @param response - The fetch Response with an SSE body stream.
|
|
89
|
+
* @param state - Mutable counters for this attempt's tokens, liveness frames
|
|
90
|
+
* and streamed content. Owned by the caller so a retry can read what the
|
|
91
|
+
* abandoned attempt produced.
|
|
92
|
+
* @param bounds - How to re-arm the stream bound after the reply starts.
|
|
64
93
|
* @yields {ChatEvent} Chat events as they arrive from the SSE stream.
|
|
65
94
|
*/
|
|
66
95
|
private parseStreamingResponse;
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EACV,UAAU,
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EACV,UAAU,EAIV,SAAS,EAET,UAAU,EAEX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAoFhD,8DAA8D;AAC9D,eAAO,MAAM,8BAA8B,QAAS,CAAA;AAyFpD;;;;GAIG;AACH,cAAM,sBAAuB,YAAW,UAAU;IAChD,QAAQ,CAAC,IAAI,cAAa;IAC1B,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,OAAO,CAAQ;IACvB,OAAO,CAAC,eAAe,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAwB;IACxC,OAAO,CAAC,WAAW,CAAC,CAAqB;IACzC,OAAO,CAAC,aAAa,CAAC,CAAuB;IAC7C,OAAO,CAAC,mBAAmB,CAAQ;IACnC,OAAO,CAAC,sBAAsB,CAAQ;gBAE1B,MAAM,GAAE,cAAmB;IAwBvC;;;;;OAKG;IACI,IAAI,CAAC,MAAM,EAAE,UAAU,GAAG,aAAa,CAAC,SAAS,CAAC;IA2MzD;;;;;;;;;OASG;YACW,2BAA2B;IA0EzC;;;;;;;OAOG;IACH,OAAO,CAAC,iBAAiB;IA2BzB;;;;;;;OAOG;IACH,OAAO,CAAC,cAAc;IAgFtB;;;;;OAKG;IACH,OAAO,CAAC,WAAW;IAWnB;;;;;OAKG;IACH,OAAO,CAAE,yBAAyB;IAuDlC;;;;;;;;;;OAUG;YACY,sBAAsB;IAyGrC;;;;;;;;OAQG;IACH,OAAO,CAAE,eAAe;CAkIzB;AAED;;GAEG;AACH,eAAO,MAAM,kBAAkB,+BAAyB,CAAA;AAExD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,cAAc,GAAG,UAAU,CAElE"}
|
package/dist/provider.js
CHANGED
|
@@ -26,6 +26,73 @@ import './secrets.js';
|
|
|
26
26
|
function budgetToEffort(budgetTokens) {
|
|
27
27
|
return budgetTokens >= 12_000 ? 'max' : 'high';
|
|
28
28
|
}
|
|
29
|
+
/** How long a stream may wait for its first content event. */
|
|
30
|
+
export const DEFAULT_FIRST_TOKEN_TIMEOUT_MS = 45_000;
|
|
31
|
+
/** How long a stream that HAS started may go without a single byte. */
|
|
32
|
+
const DEFAULT_STREAM_SILENCE_TIMEOUT_MS = 60_000;
|
|
33
|
+
/** Node's `setTimeout` ceiling: a larger delay is clamped to 1 ms, i.e. instant. */
|
|
34
|
+
const MAX_TIMER_MS = 2_147_483_647;
|
|
35
|
+
/**
|
|
36
|
+
* Resolve a stream bound from the config, then the environment, then the
|
|
37
|
+
* default. A value that is not a positive finite number is IGNORED rather than
|
|
38
|
+
* armed: these values arm `setTimeout`, and a non-positive one (or one past
|
|
39
|
+
* Node's 32-bit timer ceiling, which `setTimeout` clamps to 1 ms) would abandon
|
|
40
|
+
* every stream on its first tick — turning a stall bound into a universal
|
|
41
|
+
* failure.
|
|
42
|
+
*
|
|
43
|
+
* @param configured - The value from the provider config, when set.
|
|
44
|
+
* @param envValue - The raw environment value, when set.
|
|
45
|
+
* @param fallback - The default to use when neither resolves.
|
|
46
|
+
* @returns Milliseconds for the bound.
|
|
47
|
+
*/
|
|
48
|
+
function resolveBoundMs(configured, envValue, fallback) {
|
|
49
|
+
for (const candidate of [configured, Number(envValue)]) {
|
|
50
|
+
if (typeof candidate === 'number' &&
|
|
51
|
+
Number.isFinite(candidate) &&
|
|
52
|
+
candidate > 0 &&
|
|
53
|
+
candidate <= MAX_TIMER_MS) {
|
|
54
|
+
return candidate;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return fallback;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The first-token bound, from the config, then
|
|
61
|
+
* `MOL_DEEPSEEK_FIRST_TOKEN_TIMEOUT_MS`, then the default.
|
|
62
|
+
*
|
|
63
|
+
* @param configured - The value from {@link DeepseekConfig.firstTokenTimeoutMs}.
|
|
64
|
+
* @returns Milliseconds to wait for the first content event.
|
|
65
|
+
*/
|
|
66
|
+
function resolveFirstTokenTimeoutMs(configured) {
|
|
67
|
+
return resolveBoundMs(configured, process.env.MOL_DEEPSEEK_FIRST_TOKEN_TIMEOUT_MS, DEFAULT_FIRST_TOKEN_TIMEOUT_MS);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The post-content inter-chunk silence bound, from the config, then
|
|
71
|
+
* `MOL_DEEPSEEK_STREAM_SILENCE_TIMEOUT_MS`, then the default.
|
|
72
|
+
*
|
|
73
|
+
* @param configured - The value from {@link DeepseekConfig.streamSilenceTimeoutMs}.
|
|
74
|
+
* @returns Milliseconds a started stream may go without a byte.
|
|
75
|
+
*/
|
|
76
|
+
function resolveStreamSilenceTimeoutMs(configured) {
|
|
77
|
+
return resolveBoundMs(configured, process.env.MOL_DEEPSEEK_STREAM_SILENCE_TIMEOUT_MS, DEFAULT_STREAM_SILENCE_TIMEOUT_MS);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Whether an event means the reply has STARTED, i.e. the first-token bound no
|
|
81
|
+
* longer applies. A `keep_alive` is deliberately NOT content: it is exactly what
|
|
82
|
+
* a stalled upstream keeps dripping, so treating it as progress would disarm the
|
|
83
|
+
* bound on the one stream it exists to catch. `usage` and `done` are not content
|
|
84
|
+
* either — a stream that reports usage and closes produced no reply.
|
|
85
|
+
*
|
|
86
|
+
* @param event - One event from the provider stream.
|
|
87
|
+
* @returns True for the first real output of a reply.
|
|
88
|
+
*/
|
|
89
|
+
function isContentEvent(event) {
|
|
90
|
+
return (event.type === 'text' ||
|
|
91
|
+
event.type === 'thinking' ||
|
|
92
|
+
event.type === 'tool_use' ||
|
|
93
|
+
event.type === 'tool_use_start' ||
|
|
94
|
+
event.type === 'tool_input_delta');
|
|
95
|
+
}
|
|
29
96
|
/**
|
|
30
97
|
* DeepSeek AI provider that implements the `AIProvider` interface using
|
|
31
98
|
* DeepSeek's OpenAI-compatible Chat Completions API with support for streaming,
|
|
@@ -40,6 +107,9 @@ class DeepseekAIProviderImpl {
|
|
|
40
107
|
completionsPath;
|
|
41
108
|
modelMap;
|
|
42
109
|
onRateLimit;
|
|
110
|
+
onStreamStall;
|
|
111
|
+
firstTokenTimeoutMs;
|
|
112
|
+
streamSilenceTimeoutMs;
|
|
43
113
|
constructor(config = {}) {
|
|
44
114
|
this.apiKey = config.apiKey ?? process.env.DEEPSEEK_API_KEY ?? '';
|
|
45
115
|
this.defaultModel = config.defaultModel ?? 'deepseek-flash';
|
|
@@ -49,6 +119,9 @@ class DeepseekAIProviderImpl {
|
|
|
49
119
|
config.completionsPath ?? process.env.DEEPSEEK_COMPLETIONS_PATH ?? '/v1/chat/completions';
|
|
50
120
|
this.modelMap = config.modelMap ?? {};
|
|
51
121
|
this.onRateLimit = config.onRateLimit;
|
|
122
|
+
this.onStreamStall = config.onStreamStall;
|
|
123
|
+
this.firstTokenTimeoutMs = resolveFirstTokenTimeoutMs(config.firstTokenTimeoutMs);
|
|
124
|
+
this.streamSilenceTimeoutMs = resolveStreamSilenceTimeoutMs(config.streamSilenceTimeoutMs);
|
|
52
125
|
// Fail fast with an actionable local error rather than a cryptic 401 on the
|
|
53
126
|
// first request. The default `provider` export constructs lazily on first
|
|
54
127
|
// use (see index.ts), so this surfaces the moment the provider is actually
|
|
@@ -137,7 +210,146 @@ class DeepseekAIProviderImpl {
|
|
|
137
210
|
Authorization: `Bearer ${this.apiKey}`,
|
|
138
211
|
};
|
|
139
212
|
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
140
|
-
const
|
|
213
|
+
const firstTokenTimeoutMs = this.firstTokenTimeoutMs;
|
|
214
|
+
const MAX_STALL_RETRIES = 1;
|
|
215
|
+
// A stream that OPENS and then produces no content is invisible to every
|
|
216
|
+
// other signal this provider has: the request returns 200, no status is ever
|
|
217
|
+
// wrong, and DeepSeek keeps a stalled prefill's connection alive with
|
|
218
|
+
// `: keep-alive` comments — so a byte-level idle timeout stays quiet too.
|
|
219
|
+
// Left alone, the whole first-token window is spent before anything notices.
|
|
220
|
+
// Measured in production (X0 xr-molecule-flash-m71, 2026-10-16): the chat
|
|
221
|
+
// handler's 180 s progress watchdog was the ONLY bound, and a 3 min 15 s hole
|
|
222
|
+
// opened in the middle of a live turn. Bound it here instead, where the same
|
|
223
|
+
// request can simply be asked again.
|
|
224
|
+
for (let stalledAttempt = 0;; stalledAttempt++) {
|
|
225
|
+
const controller = new AbortController();
|
|
226
|
+
const forwardAbort = () => controller.abort();
|
|
227
|
+
if (params.signal?.aborted)
|
|
228
|
+
controller.abort();
|
|
229
|
+
else
|
|
230
|
+
params.signal?.addEventListener('abort', forwardAbort, { once: true });
|
|
231
|
+
// The provider's own ceiling bounds each ATTEMPT; the caller's signal, when
|
|
232
|
+
// present, still wins over it (both abort this one controller).
|
|
233
|
+
const ceiling = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT_MS);
|
|
234
|
+
ceiling.unref?.();
|
|
235
|
+
const signal = controller.signal;
|
|
236
|
+
try {
|
|
237
|
+
const response = await this.requestWithRateLimitRetries({
|
|
238
|
+
body,
|
|
239
|
+
headers,
|
|
240
|
+
model,
|
|
241
|
+
signal,
|
|
242
|
+
});
|
|
243
|
+
if (!response.ok) {
|
|
244
|
+
const errorBody = await response.text();
|
|
245
|
+
let detail = `HTTP ${response.status}`;
|
|
246
|
+
try {
|
|
247
|
+
const parsed = JSON.parse(errorBody);
|
|
248
|
+
if (parsed.error?.message)
|
|
249
|
+
detail = parsed.error.message;
|
|
250
|
+
}
|
|
251
|
+
catch (_error) {
|
|
252
|
+
// JSON.parse failed — raw text is already in `errorBody`; use it as-is.
|
|
253
|
+
if (errorBody.length > 0 && errorBody.length < 200)
|
|
254
|
+
detail = errorBody;
|
|
255
|
+
}
|
|
256
|
+
logger.error('DeepSeek API error', { status: response.status, detail, errorBody });
|
|
257
|
+
const clientMessage = response.status === 429
|
|
258
|
+
? 'AI rate limit exceeded. Please try again shortly.'
|
|
259
|
+
: response.status === 401
|
|
260
|
+
? 'AI service configuration error.'
|
|
261
|
+
: response.status === 400 &&
|
|
262
|
+
/prompt is too long|too many tokens|token.*limit|context.*length/i.test(detail)
|
|
263
|
+
? "Conversation too long for the model's context window. Use /compact to free space, or start a new conversation."
|
|
264
|
+
: response.status === 400
|
|
265
|
+
? 'AI request was invalid — check the model and request parameters.'
|
|
266
|
+
: response.status === 503
|
|
267
|
+
? 'AI service is temporarily overloaded. Please try again in a moment.'
|
|
268
|
+
: 'AI service error. Please try again.';
|
|
269
|
+
yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (params.stream === false) {
|
|
273
|
+
const data = (await response.json());
|
|
274
|
+
yield* this.parseNonStreamingResponse(data);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const state = {
|
|
278
|
+
inputTokens: 0,
|
|
279
|
+
outputTokens: 0,
|
|
280
|
+
cachedTokens: 0,
|
|
281
|
+
keepAlives: 0,
|
|
282
|
+
streamedChars: 0,
|
|
283
|
+
contentSeen: false,
|
|
284
|
+
};
|
|
285
|
+
// Both bounds abort the same request; `expired` says which one did, and
|
|
286
|
+
// `windowStartedAt` how long that one had been running — the stall is
|
|
287
|
+
// the WINDOW that expired, not the time since the request went out (an
|
|
288
|
+
// inter-chunk stall after a long reply would otherwise report minutes).
|
|
289
|
+
const bounds = { expired: null, timer: null, windowStartedAt: 0 };
|
|
290
|
+
const armBound = (ms, phase) => {
|
|
291
|
+
if (bounds.timer)
|
|
292
|
+
clearTimeout(bounds.timer);
|
|
293
|
+
bounds.windowStartedAt = Date.now();
|
|
294
|
+
bounds.timer = setTimeout(() => {
|
|
295
|
+
bounds.expired = phase;
|
|
296
|
+
controller.abort();
|
|
297
|
+
}, ms);
|
|
298
|
+
};
|
|
299
|
+
armBound(firstTokenTimeoutMs, 'before_content');
|
|
300
|
+
try {
|
|
301
|
+
yield* this.parseStreamingResponse(response, state, {
|
|
302
|
+
silenceMs: this.streamSilenceTimeoutMs,
|
|
303
|
+
arm: armBound,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
// Only a bound expiry is this provider's to absorb — anything else
|
|
308
|
+
// (the caller aborting, a socket failure) belongs to the caller.
|
|
309
|
+
if (!bounds.expired)
|
|
310
|
+
throw error;
|
|
311
|
+
const stall = {
|
|
312
|
+
stallMs: Date.now() - bounds.windowStartedAt,
|
|
313
|
+
attempt: stalledAttempt + 1,
|
|
314
|
+
// Only a stream that never started may be asked again: a reply that
|
|
315
|
+
// already partly reached the consumer would be duplicated.
|
|
316
|
+
willRetry: bounds.expired === 'before_content' && stalledAttempt < MAX_STALL_RETRIES,
|
|
317
|
+
streamedChars: state.streamedChars,
|
|
318
|
+
keepAlives: state.keepAlives,
|
|
319
|
+
phase: bounds.expired,
|
|
320
|
+
};
|
|
321
|
+
this.reportStreamStall(model, stall);
|
|
322
|
+
if (stall.willRetry)
|
|
323
|
+
continue;
|
|
324
|
+
// Out of retries, or the partial reply is already on the wire. Hand it
|
|
325
|
+
// to the caller's own error ladder, which keeps the partial.
|
|
326
|
+
yield { type: 'error', message: t('ai.error.apiError'), errorKey: 'ai.error.apiError' };
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
finally {
|
|
330
|
+
if (bounds.timer)
|
|
331
|
+
clearTimeout(bounds.timer);
|
|
332
|
+
}
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
finally {
|
|
336
|
+
clearTimeout(ceiling);
|
|
337
|
+
params.signal?.removeEventListener('abort', forwardAbort);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* POST the chat body, retrying with exponential backoff on a rate-limited
|
|
343
|
+
* (429) or overloaded (503) status. Extracted from {@link chat} so the
|
|
344
|
+
* stream-level first-token retry can wrap it: a rate-limited request and a
|
|
345
|
+
* stalled stream are different failures with different budgets, and nesting
|
|
346
|
+
* them in one loop made each one's attempt count depend on the other's.
|
|
347
|
+
*
|
|
348
|
+
* @param args - The request body, headers, upstream model and abort signal.
|
|
349
|
+
* @returns The upstream response (possibly a 429/503 that exhausted retries).
|
|
350
|
+
*/
|
|
351
|
+
async requestWithRateLimitRetries(args) {
|
|
352
|
+
const { body, headers, model, signal } = args;
|
|
141
353
|
// Retry with exponential backoff for rate limits (429) and overloaded (503)
|
|
142
354
|
const MAX_RETRIES = 3;
|
|
143
355
|
let response = null;
|
|
@@ -198,41 +410,42 @@ class DeepseekAIProviderImpl {
|
|
|
198
410
|
}
|
|
199
411
|
break;
|
|
200
412
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
413
|
+
return response;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Record one stalled attempt: a warn line always, plus the host's
|
|
417
|
+
* `onStreamStall` callback so the gap is queryable from the turn's own
|
|
418
|
+
* analytics rather than only from a log buffer that has since rotated.
|
|
419
|
+
*
|
|
420
|
+
* @param model - The upstream model id the stalled request targeted.
|
|
421
|
+
* @param stall - What the stalled attempt looked like.
|
|
422
|
+
*/
|
|
423
|
+
reportStreamStall(model, stall) {
|
|
424
|
+
logger.warn('DeepSeek stream stalled', {
|
|
425
|
+
model,
|
|
426
|
+
phase: stall.phase,
|
|
427
|
+
stallMs: stall.stallMs,
|
|
428
|
+
attempt: stall.attempt,
|
|
429
|
+
willRetry: stall.willRetry,
|
|
430
|
+
streamedChars: stall.streamedChars,
|
|
431
|
+
keepAlives: stall.keepAlives,
|
|
432
|
+
});
|
|
433
|
+
try {
|
|
434
|
+
this.onStreamStall?.({
|
|
435
|
+
provider: this.name,
|
|
436
|
+
model,
|
|
437
|
+
phase: stall.phase,
|
|
438
|
+
stallMs: stall.stallMs,
|
|
439
|
+
attempt: stall.attempt,
|
|
440
|
+
willRetry: stall.willRetry,
|
|
441
|
+
streamedChars: stall.streamedChars,
|
|
442
|
+
keepAlives: stall.keepAlives,
|
|
443
|
+
});
|
|
229
444
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return;
|
|
445
|
+
catch (error) {
|
|
446
|
+
// Host-app telemetry; its failure must never break the request.
|
|
447
|
+
logger.warn('onStreamStall callback threw', { error });
|
|
234
448
|
}
|
|
235
|
-
yield* this.parseStreamingResponse(response);
|
|
236
449
|
}
|
|
237
450
|
/**
|
|
238
451
|
* Converts internal `ChatMessage` objects to the OpenAI-compatible message format.
|
|
@@ -390,9 +603,13 @@ class DeepseekAIProviderImpl {
|
|
|
390
603
|
* format), yielding `ChatEvent` objects as they arrive.
|
|
391
604
|
*
|
|
392
605
|
* @param response - The fetch Response with an SSE body stream.
|
|
606
|
+
* @param state - Mutable counters for this attempt's tokens, liveness frames
|
|
607
|
+
* and streamed content. Owned by the caller so a retry can read what the
|
|
608
|
+
* abandoned attempt produced.
|
|
609
|
+
* @param bounds - How to re-arm the stream bound after the reply starts.
|
|
393
610
|
* @yields {ChatEvent} Chat events as they arrive from the SSE stream.
|
|
394
611
|
*/
|
|
395
|
-
async *parseStreamingResponse(response) {
|
|
612
|
+
async *parseStreamingResponse(response, state, bounds) {
|
|
396
613
|
const reader = response.body?.getReader();
|
|
397
614
|
if (!reader) {
|
|
398
615
|
yield {
|
|
@@ -404,7 +621,6 @@ class DeepseekAIProviderImpl {
|
|
|
404
621
|
}
|
|
405
622
|
const decoder = new TextDecoder();
|
|
406
623
|
let buffer = '';
|
|
407
|
-
const state = { inputTokens: 0, outputTokens: 0, cachedTokens: 0 };
|
|
408
624
|
// Track pending tool calls (accumulated across deltas). `started` guards the
|
|
409
625
|
// one-shot tool_use_start emission once the id + name are first known.
|
|
410
626
|
const pendingTools = new Map();
|
|
@@ -419,6 +635,20 @@ class DeepseekAIProviderImpl {
|
|
|
419
635
|
let yieldedContent = false;
|
|
420
636
|
for (const event of this.processSSELines(lines, pendingTools, state)) {
|
|
421
637
|
yieldedContent = true;
|
|
638
|
+
// The reply has started. Counting this from the EVENTS (rather than
|
|
639
|
+
// from the byte stream) is what lets the first-token bound see a
|
|
640
|
+
// stall whose connection is kept alive by empty frames — the shape
|
|
641
|
+
// DeepSeek actually produces. And the bound is swapped the instant it
|
|
642
|
+
// starts, not at the end of the batch: a first-token timer that fired
|
|
643
|
+
// on a reply already yielded to the consumer would retry and
|
|
644
|
+
// duplicate it.
|
|
645
|
+
if (isContentEvent(event) && !state.contentSeen) {
|
|
646
|
+
state.contentSeen = true;
|
|
647
|
+
bounds.arm(bounds.silenceMs, 'after_content');
|
|
648
|
+
}
|
|
649
|
+
if (event.type === 'text' || event.type === 'thinking') {
|
|
650
|
+
state.streamedChars += event.content.length;
|
|
651
|
+
}
|
|
422
652
|
yield event;
|
|
423
653
|
// A mid-stream error (rate-limit/overload during high load, sent as a
|
|
424
654
|
// `data: {"error": {...}}` chunk) means the turn was truncated. Stop
|
|
@@ -432,8 +662,13 @@ class DeepseekAIProviderImpl {
|
|
|
432
662
|
// (e.g., during extended reasoning with empty chunks or keepalive comments).
|
|
433
663
|
// This resets the consumer's stream timeout without forwarding to the client.
|
|
434
664
|
if (!yieldedContent) {
|
|
665
|
+
state.keepAlives += 1;
|
|
435
666
|
yield { type: 'keep_alive' };
|
|
436
667
|
}
|
|
668
|
+
// Any chunk at all proves the socket is alive, so the post-content bound
|
|
669
|
+
// is re-armed here rather than on content events only.
|
|
670
|
+
if (state.contentSeen)
|
|
671
|
+
bounds.arm(bounds.silenceMs, 'after_content');
|
|
437
672
|
}
|
|
438
673
|
// Flush any remaining data in the buffer after EOF
|
|
439
674
|
if (buffer.trim()) {
|
package/dist/types.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module
|
|
5
5
|
*/
|
|
6
|
-
import type { AiRateLimitCallback } from '@molecule/api-ai';
|
|
6
|
+
import type { AiRateLimitCallback, AiStreamStallCallback } from '@molecule/api-ai';
|
|
7
7
|
/**
|
|
8
8
|
* Configuration for DeepSeek.
|
|
9
9
|
*/
|
|
@@ -34,6 +34,31 @@ export interface DeepseekConfig {
|
|
|
34
34
|
modelMap?: Record<string, string>;
|
|
35
35
|
/** Called on each rate-limited/overloaded upstream response, before any retry sleep. */
|
|
36
36
|
onRateLimit?: AiRateLimitCallback;
|
|
37
|
+
/** Called when a stream accepts the request and then produces no content for the whole first-token window. */
|
|
38
|
+
onStreamStall?: AiStreamStallCallback;
|
|
39
|
+
/**
|
|
40
|
+
* How long a stream may wait for its FIRST content event before the provider
|
|
41
|
+
* abandons it and asks again (ms). Defaults to 45 000.
|
|
42
|
+
*
|
|
43
|
+
* Counted from the moment the request is sent, and cleared by the first
|
|
44
|
+
* `text` / `thinking` / `tool_use` event — NOT by a liveness frame, because
|
|
45
|
+
* DeepSeek keeps a stalled prefill's connection alive with `: keep-alive`
|
|
46
|
+
* comments, so a byte-level idle timeout never sees the stall at all. A turn's
|
|
47
|
+
* healthy first-token wait is ~1 s even on million-token prompts, so this
|
|
48
|
+
* window is a stall bound, not a latency budget.
|
|
49
|
+
*/
|
|
50
|
+
firstTokenTimeoutMs?: number;
|
|
51
|
+
/**
|
|
52
|
+
* How long a stream that has ALREADY started may go without a single byte
|
|
53
|
+
* before the provider abandons it (ms). Defaults to 60 000.
|
|
54
|
+
*
|
|
55
|
+
* Measured over the socket, not over content: once the reply has started, any
|
|
56
|
+
* byte proves the connection is alive, and a stretch with none is a broken
|
|
57
|
+
* stream rather than a slow one. Abandoning it here is not retried — the
|
|
58
|
+
* partial reply is already on the wire — it is reported as an error event for
|
|
59
|
+
* the caller's own ladder, which keeps the partial.
|
|
60
|
+
*/
|
|
61
|
+
streamSilenceTimeoutMs?: number;
|
|
37
62
|
}
|
|
38
63
|
/**
|
|
39
64
|
* Process Env interface.
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAA;AAElF;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,qDAAqD;IACrD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,sDAAsD;IACtD,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,sCAAsC;IACtC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,+EAA+E;IAC/E,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC,wFAAwF;IACxF,WAAW,CAAC,EAAE,mBAAmB,CAAA;IACjC,8GAA8G;IAC9G,aAAa,CAAC,EAAE,qBAAqB,CAAA;IACrC;;;;;;;;;;OAUG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B;;;;;;;;;OASG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAA;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,gBAAgB,EAAE,MAAM,CAAA;IACxB,0FAA0F;IAC1F,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,mFAAmF;IACnF,yBAAyB,CAAC,EAAE,MAAM,CAAA;CACnC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-deepseek",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "DeepSeek AI provider for molecule.dev",
|
|
5
5
|
"homepage": "https://www.molecule.dev/packages/api-ai-deepseek",
|
|
6
6
|
"type": "module",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"directory": "packages/api/bonds/ai/deepseek"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@molecule/api-ai": "1.3.
|
|
36
|
+
"@molecule/api-ai": "1.3.1",
|
|
37
37
|
"@molecule/api-bond": "1.0.2",
|
|
38
38
|
"@types/node": "26.1.2",
|
|
39
39
|
"typescript": "6.0.3",
|