@sunerpy/opencode-kiro-auth 0.1.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/LICENSE +678 -0
- package/README.md +270 -0
- package/dist/constants.d.ts +28 -0
- package/dist/constants.js +108 -0
- package/dist/core/account/account-selector.d.ts +25 -0
- package/dist/core/account/account-selector.js +80 -0
- package/dist/core/account/usage-tracker.d.ts +20 -0
- package/dist/core/account/usage-tracker.js +56 -0
- package/dist/core/auth/auth-handler.d.ts +18 -0
- package/dist/core/auth/auth-handler.js +236 -0
- package/dist/core/auth/idc-auth-method.d.ts +9 -0
- package/dist/core/auth/idc-auth-method.js +169 -0
- package/dist/core/auth/token-refresher.d.ts +23 -0
- package/dist/core/auth/token-refresher.js +85 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/index.js +9 -0
- package/dist/core/request/error-handler.d.ts +32 -0
- package/dist/core/request/error-handler.js +159 -0
- package/dist/core/request/request-handler.d.ts +35 -0
- package/dist/core/request/request-handler.js +296 -0
- package/dist/core/request/response-handler.d.ts +8 -0
- package/dist/core/request/response-handler.js +137 -0
- package/dist/core/request/retry-strategy.d.ts +18 -0
- package/dist/core/request/retry-strategy.js +28 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.js +2 -0
- package/dist/infrastructure/database/account-cache.d.ts +14 -0
- package/dist/infrastructure/database/account-cache.js +44 -0
- package/dist/infrastructure/database/account-repository.d.ts +12 -0
- package/dist/infrastructure/database/account-repository.js +66 -0
- package/dist/infrastructure/index.d.ts +7 -0
- package/dist/infrastructure/index.js +7 -0
- package/dist/infrastructure/transformers/event-stream-parser.d.ts +7 -0
- package/dist/infrastructure/transformers/event-stream-parser.js +115 -0
- package/dist/infrastructure/transformers/history-builder.d.ts +16 -0
- package/dist/infrastructure/transformers/history-builder.js +226 -0
- package/dist/infrastructure/transformers/message-transformer.d.ts +5 -0
- package/dist/infrastructure/transformers/message-transformer.js +99 -0
- package/dist/infrastructure/transformers/tool-call-parser.d.ts +4 -0
- package/dist/infrastructure/transformers/tool-call-parser.js +45 -0
- package/dist/infrastructure/transformers/tool-transformer.d.ts +2 -0
- package/dist/infrastructure/transformers/tool-transformer.js +19 -0
- package/dist/kiro/auth.d.ts +4 -0
- package/dist/kiro/auth.js +25 -0
- package/dist/kiro/oauth-idc.d.ts +25 -0
- package/dist/kiro/oauth-idc.js +167 -0
- package/dist/plugin/accounts.d.ts +29 -0
- package/dist/plugin/accounts.js +266 -0
- package/dist/plugin/auth-bootstrap.d.ts +9 -0
- package/dist/plugin/auth-bootstrap.js +69 -0
- package/dist/plugin/auth-page.d.ts +4 -0
- package/dist/plugin/auth-page.js +721 -0
- package/dist/plugin/config/index.d.ts +3 -0
- package/dist/plugin/config/index.js +2 -0
- package/dist/plugin/config/loader.d.ts +6 -0
- package/dist/plugin/config/loader.js +129 -0
- package/dist/plugin/config/schema.d.ts +88 -0
- package/dist/plugin/config/schema.js +94 -0
- package/dist/plugin/effort.d.ts +46 -0
- package/dist/plugin/effort.js +113 -0
- package/dist/plugin/errors.d.ts +17 -0
- package/dist/plugin/errors.js +34 -0
- package/dist/plugin/health.d.ts +1 -0
- package/dist/plugin/health.js +13 -0
- package/dist/plugin/image-handler.d.ts +18 -0
- package/dist/plugin/image-handler.js +82 -0
- package/dist/plugin/logger.d.ts +8 -0
- package/dist/plugin/logger.js +84 -0
- package/dist/plugin/models.d.ts +2 -0
- package/dist/plugin/models.js +11 -0
- package/dist/plugin/request.d.ts +9 -0
- package/dist/plugin/request.js +287 -0
- package/dist/plugin/response.d.ts +3 -0
- package/dist/plugin/response.js +97 -0
- package/dist/plugin/sdk-client.d.ts +4 -0
- package/dist/plugin/sdk-client.js +55 -0
- package/dist/plugin/storage/locked-operations.d.ts +5 -0
- package/dist/plugin/storage/locked-operations.js +103 -0
- package/dist/plugin/storage/migrations.d.ts +4 -0
- package/dist/plugin/storage/migrations.js +155 -0
- package/dist/plugin/storage/sqlite.d.ts +18 -0
- package/dist/plugin/storage/sqlite.js +166 -0
- package/dist/plugin/streaming/index.d.ts +2 -0
- package/dist/plugin/streaming/index.js +2 -0
- package/dist/plugin/streaming/openai-converter.d.ts +2 -0
- package/dist/plugin/streaming/openai-converter.js +81 -0
- package/dist/plugin/streaming/sdk-stream-transformer.d.ts +1 -0
- package/dist/plugin/streaming/sdk-stream-transformer.js +274 -0
- package/dist/plugin/streaming/stream-parser.d.ts +5 -0
- package/dist/plugin/streaming/stream-parser.js +136 -0
- package/dist/plugin/streaming/stream-state.d.ts +5 -0
- package/dist/plugin/streaming/stream-state.js +59 -0
- package/dist/plugin/streaming/stream-transformer.d.ts +1 -0
- package/dist/plugin/streaming/stream-transformer.js +295 -0
- package/dist/plugin/streaming/types.d.ts +25 -0
- package/dist/plugin/streaming/types.js +2 -0
- package/dist/plugin/sync/kiro-cli-parser.d.ts +8 -0
- package/dist/plugin/sync/kiro-cli-parser.js +72 -0
- package/dist/plugin/sync/kiro-cli-profile.d.ts +1 -0
- package/dist/plugin/sync/kiro-cli-profile.js +30 -0
- package/dist/plugin/sync/kiro-cli.d.ts +2 -0
- package/dist/plugin/sync/kiro-cli.js +212 -0
- package/dist/plugin/sync/stale-accounts.d.ts +9 -0
- package/dist/plugin/sync/stale-accounts.js +31 -0
- package/dist/plugin/token.d.ts +2 -0
- package/dist/plugin/token.js +78 -0
- package/dist/plugin/types.d.ts +125 -0
- package/dist/plugin/types.js +0 -0
- package/dist/plugin/usage.d.ts +3 -0
- package/dist/plugin/usage.js +79 -0
- package/dist/plugin.d.ts +174 -0
- package/dist/plugin.js +170 -0
- package/package.json +74 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { parseBracketToolCalls } from '../../infrastructure/transformers/tool-call-parser.js';
|
|
2
|
+
import { getContextWindowSize } from '../models.js';
|
|
3
|
+
import { estimateTokens } from '../response.js';
|
|
4
|
+
import { convertToOpenAI } from './openai-converter.js';
|
|
5
|
+
import { findRealTag } from './stream-parser.js';
|
|
6
|
+
import { createTextDeltaEvents, createThinkingDeltaEvents, stopBlock } from './stream-state.js';
|
|
7
|
+
import { THINKING_END_TAG, THINKING_START_TAG } from './types.js';
|
|
8
|
+
export async function* transformSdkStream(sdkResponse, model, conversationId) {
|
|
9
|
+
const thinkingRequested = true;
|
|
10
|
+
const streamState = {
|
|
11
|
+
thinkingRequested,
|
|
12
|
+
buffer: '',
|
|
13
|
+
inThinking: false,
|
|
14
|
+
thinkingExtracted: false,
|
|
15
|
+
thinkingBlockIndex: null,
|
|
16
|
+
textBlockIndex: null,
|
|
17
|
+
nextBlockIndex: 0,
|
|
18
|
+
stoppedBlocks: new Set()
|
|
19
|
+
};
|
|
20
|
+
let totalContent = '';
|
|
21
|
+
let textOnlyContent = '';
|
|
22
|
+
let outputTokens = 0;
|
|
23
|
+
let inputTokens = 0;
|
|
24
|
+
let contextUsagePercentage = null;
|
|
25
|
+
const toolCalls = [];
|
|
26
|
+
let currentToolCall = null;
|
|
27
|
+
const eventStream = sdkResponse.generateAssistantResponseResponse;
|
|
28
|
+
if (!eventStream) {
|
|
29
|
+
throw new Error('SDK response has no event stream');
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
for await (const event of eventStream) {
|
|
33
|
+
if (event.assistantResponseEvent?.content) {
|
|
34
|
+
const text = event.assistantResponseEvent.content;
|
|
35
|
+
totalContent += text;
|
|
36
|
+
textOnlyContent += text;
|
|
37
|
+
if (!thinkingRequested) {
|
|
38
|
+
for (const ev of createTextDeltaEvents(text, streamState)) {
|
|
39
|
+
{
|
|
40
|
+
const _c = convertToOpenAI(ev, conversationId, model);
|
|
41
|
+
if (_c !== null)
|
|
42
|
+
yield _c;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
streamState.buffer += text;
|
|
48
|
+
const deltaEvents = [];
|
|
49
|
+
while (streamState.buffer.length > 0) {
|
|
50
|
+
if (!streamState.inThinking && !streamState.thinkingExtracted) {
|
|
51
|
+
const startPos = findRealTag(streamState.buffer, THINKING_START_TAG);
|
|
52
|
+
if (startPos !== -1) {
|
|
53
|
+
const before = streamState.buffer.slice(0, startPos);
|
|
54
|
+
if (before) {
|
|
55
|
+
deltaEvents.push(...createTextDeltaEvents(before, streamState));
|
|
56
|
+
}
|
|
57
|
+
streamState.buffer = streamState.buffer.slice(startPos + THINKING_START_TAG.length);
|
|
58
|
+
streamState.inThinking = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const safeLen = Math.max(0, streamState.buffer.length - THINKING_START_TAG.length);
|
|
62
|
+
if (safeLen > 0) {
|
|
63
|
+
const safeText = streamState.buffer.slice(0, safeLen);
|
|
64
|
+
if (safeText) {
|
|
65
|
+
deltaEvents.push(...createTextDeltaEvents(safeText, streamState));
|
|
66
|
+
}
|
|
67
|
+
streamState.buffer = streamState.buffer.slice(safeLen);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
if (streamState.inThinking) {
|
|
72
|
+
const endPos = findRealTag(streamState.buffer, THINKING_END_TAG);
|
|
73
|
+
if (endPos !== -1) {
|
|
74
|
+
const thinkingPart = streamState.buffer.slice(0, endPos);
|
|
75
|
+
if (thinkingPart) {
|
|
76
|
+
deltaEvents.push(...createThinkingDeltaEvents(thinkingPart, streamState));
|
|
77
|
+
}
|
|
78
|
+
streamState.buffer = streamState.buffer.slice(endPos + THINKING_END_TAG.length);
|
|
79
|
+
streamState.inThinking = false;
|
|
80
|
+
streamState.thinkingExtracted = true;
|
|
81
|
+
deltaEvents.push(...createThinkingDeltaEvents('', streamState));
|
|
82
|
+
deltaEvents.push(...stopBlock(streamState.thinkingBlockIndex, streamState));
|
|
83
|
+
if (streamState.buffer.startsWith('\n\n')) {
|
|
84
|
+
streamState.buffer = streamState.buffer.slice(2);
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const safeLen = Math.max(0, streamState.buffer.length - THINKING_END_TAG.length);
|
|
89
|
+
if (safeLen > 0) {
|
|
90
|
+
const safeThinking = streamState.buffer.slice(0, safeLen);
|
|
91
|
+
if (safeThinking) {
|
|
92
|
+
deltaEvents.push(...createThinkingDeltaEvents(safeThinking, streamState));
|
|
93
|
+
}
|
|
94
|
+
streamState.buffer = streamState.buffer.slice(safeLen);
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
if (streamState.thinkingExtracted) {
|
|
99
|
+
const rest = streamState.buffer;
|
|
100
|
+
streamState.buffer = '';
|
|
101
|
+
if (rest) {
|
|
102
|
+
deltaEvents.push(...createTextDeltaEvents(rest, streamState));
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (const ev of deltaEvents) {
|
|
108
|
+
const chunk = convertToOpenAI(ev, conversationId, model);
|
|
109
|
+
if (chunk !== null)
|
|
110
|
+
yield chunk;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else if (event.toolUseEvent) {
|
|
114
|
+
const tc = event.toolUseEvent;
|
|
115
|
+
if (tc.name)
|
|
116
|
+
totalContent += tc.name;
|
|
117
|
+
if (tc.input)
|
|
118
|
+
totalContent += tc.input;
|
|
119
|
+
if (tc.name && tc.toolUseId) {
|
|
120
|
+
if (currentToolCall && currentToolCall.toolUseId === tc.toolUseId) {
|
|
121
|
+
currentToolCall.input += tc.input || '';
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
if (currentToolCall)
|
|
125
|
+
toolCalls.push(currentToolCall);
|
|
126
|
+
currentToolCall = {
|
|
127
|
+
toolUseId: tc.toolUseId,
|
|
128
|
+
name: tc.name,
|
|
129
|
+
input: tc.input || ''
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (tc.stop && currentToolCall) {
|
|
133
|
+
toolCalls.push(currentToolCall);
|
|
134
|
+
currentToolCall = null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else if (event.metadataEvent) {
|
|
139
|
+
if (event.metadataEvent.contextUsagePercentage) {
|
|
140
|
+
contextUsagePercentage = event.metadataEvent.contextUsagePercentage;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
else if (event.contextUsageEvent) {
|
|
144
|
+
const cue = event.contextUsageEvent;
|
|
145
|
+
if (cue.contextUsagePercentage) {
|
|
146
|
+
contextUsagePercentage = cue.contextUsagePercentage;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (currentToolCall) {
|
|
151
|
+
toolCalls.push(currentToolCall);
|
|
152
|
+
currentToolCall = null;
|
|
153
|
+
}
|
|
154
|
+
if (thinkingRequested && streamState.buffer) {
|
|
155
|
+
if (streamState.inThinking) {
|
|
156
|
+
for (const ev of createThinkingDeltaEvents(streamState.buffer, streamState)) {
|
|
157
|
+
const _c = convertToOpenAI(ev, conversationId, model);
|
|
158
|
+
if (_c !== null)
|
|
159
|
+
yield _c;
|
|
160
|
+
}
|
|
161
|
+
streamState.buffer = '';
|
|
162
|
+
for (const ev of createThinkingDeltaEvents('', streamState)) {
|
|
163
|
+
const _c = convertToOpenAI(ev, conversationId, model);
|
|
164
|
+
if (_c !== null)
|
|
165
|
+
yield _c;
|
|
166
|
+
}
|
|
167
|
+
for (const ev of stopBlock(streamState.thinkingBlockIndex, streamState)) {
|
|
168
|
+
const _c = convertToOpenAI(ev, conversationId, model);
|
|
169
|
+
if (_c !== null)
|
|
170
|
+
yield _c;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
for (const ev of createTextDeltaEvents(streamState.buffer, streamState)) {
|
|
175
|
+
const _c = convertToOpenAI(ev, conversationId, model);
|
|
176
|
+
if (_c !== null)
|
|
177
|
+
yield _c;
|
|
178
|
+
}
|
|
179
|
+
streamState.buffer = '';
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (const ev of stopBlock(streamState.textBlockIndex, streamState)) {
|
|
183
|
+
const _c = convertToOpenAI(ev, conversationId, model);
|
|
184
|
+
if (_c !== null)
|
|
185
|
+
yield _c;
|
|
186
|
+
}
|
|
187
|
+
const bracketToolCalls = parseBracketToolCalls(totalContent);
|
|
188
|
+
if (bracketToolCalls.length > 0) {
|
|
189
|
+
for (const btc of bracketToolCalls) {
|
|
190
|
+
toolCalls.push({
|
|
191
|
+
toolUseId: btc.toolUseId,
|
|
192
|
+
name: btc.name,
|
|
193
|
+
input: typeof btc.input === 'string' ? btc.input : JSON.stringify(btc.input)
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (toolCalls.length > 0) {
|
|
198
|
+
const baseIndex = streamState.nextBlockIndex;
|
|
199
|
+
for (let i = 0; i < toolCalls.length; i++) {
|
|
200
|
+
const tc = toolCalls[i];
|
|
201
|
+
if (!tc)
|
|
202
|
+
continue;
|
|
203
|
+
const blockIndex = baseIndex + i;
|
|
204
|
+
{
|
|
205
|
+
const _c = convertToOpenAI({
|
|
206
|
+
type: 'content_block_start',
|
|
207
|
+
index: blockIndex,
|
|
208
|
+
content_block: {
|
|
209
|
+
type: 'tool_use',
|
|
210
|
+
id: tc.toolUseId,
|
|
211
|
+
name: tc.name,
|
|
212
|
+
input: {}
|
|
213
|
+
}
|
|
214
|
+
}, conversationId, model);
|
|
215
|
+
if (_c !== null)
|
|
216
|
+
yield _c;
|
|
217
|
+
}
|
|
218
|
+
let inputJson;
|
|
219
|
+
try {
|
|
220
|
+
const parsed = JSON.parse(tc.input);
|
|
221
|
+
inputJson = JSON.stringify(parsed);
|
|
222
|
+
}
|
|
223
|
+
catch (e) {
|
|
224
|
+
inputJson = tc.input;
|
|
225
|
+
}
|
|
226
|
+
{
|
|
227
|
+
const _c = convertToOpenAI({
|
|
228
|
+
type: 'content_block_delta',
|
|
229
|
+
index: blockIndex,
|
|
230
|
+
delta: {
|
|
231
|
+
type: 'input_json_delta',
|
|
232
|
+
partial_json: inputJson
|
|
233
|
+
}
|
|
234
|
+
}, conversationId, model);
|
|
235
|
+
if (_c !== null)
|
|
236
|
+
yield _c;
|
|
237
|
+
}
|
|
238
|
+
{
|
|
239
|
+
const _c = convertToOpenAI({ type: 'content_block_stop', index: blockIndex }, conversationId, model);
|
|
240
|
+
if (_c !== null)
|
|
241
|
+
yield _c;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
outputTokens = estimateTokens(textOnlyContent);
|
|
246
|
+
if (contextUsagePercentage !== null && contextUsagePercentage > 0) {
|
|
247
|
+
const contextWindow = getContextWindowSize(model);
|
|
248
|
+
const totalTokens = Math.round((contextWindow * contextUsagePercentage) / 100);
|
|
249
|
+
inputTokens = Math.max(0, totalTokens - outputTokens);
|
|
250
|
+
}
|
|
251
|
+
{
|
|
252
|
+
const _c = convertToOpenAI({
|
|
253
|
+
type: 'message_delta',
|
|
254
|
+
delta: { stop_reason: toolCalls.length > 0 ? 'tool_use' : 'end_turn' },
|
|
255
|
+
usage: {
|
|
256
|
+
input_tokens: inputTokens,
|
|
257
|
+
output_tokens: outputTokens,
|
|
258
|
+
cache_creation_input_tokens: 0,
|
|
259
|
+
cache_read_input_tokens: 0
|
|
260
|
+
}
|
|
261
|
+
}, conversationId, model);
|
|
262
|
+
if (_c !== null)
|
|
263
|
+
yield _c;
|
|
264
|
+
}
|
|
265
|
+
{
|
|
266
|
+
const _c = convertToOpenAI({ type: 'message_stop' }, conversationId, model);
|
|
267
|
+
if (_c !== null)
|
|
268
|
+
yield _c;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
catch (e) {
|
|
272
|
+
throw e;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { parseEventLine } from '../../infrastructure/transformers/event-stream-parser.js';
|
|
2
|
+
export function parseStreamBuffer(buffer) {
|
|
3
|
+
const events = [];
|
|
4
|
+
let remaining = buffer;
|
|
5
|
+
let searchStart = 0;
|
|
6
|
+
while (true) {
|
|
7
|
+
const contentStart = remaining.indexOf('{"content":', searchStart);
|
|
8
|
+
const nameStart = remaining.indexOf('{"name":', searchStart);
|
|
9
|
+
const followupStart = remaining.indexOf('{"followupPrompt":', searchStart);
|
|
10
|
+
const inputStart = remaining.indexOf('{"input":', searchStart);
|
|
11
|
+
const stopStart = remaining.indexOf('{"stop":', searchStart);
|
|
12
|
+
const contextUsageStart = remaining.indexOf('{"contextUsagePercentage":', searchStart);
|
|
13
|
+
const candidates = [
|
|
14
|
+
contentStart,
|
|
15
|
+
nameStart,
|
|
16
|
+
followupStart,
|
|
17
|
+
inputStart,
|
|
18
|
+
stopStart,
|
|
19
|
+
contextUsageStart
|
|
20
|
+
].filter((pos) => pos >= 0);
|
|
21
|
+
if (candidates.length === 0)
|
|
22
|
+
break;
|
|
23
|
+
const jsonStart = Math.min(...candidates);
|
|
24
|
+
if (jsonStart < 0)
|
|
25
|
+
break;
|
|
26
|
+
let braceCount = 0;
|
|
27
|
+
let jsonEnd = -1;
|
|
28
|
+
let inString = false;
|
|
29
|
+
let escapeNext = false;
|
|
30
|
+
for (let i = jsonStart; i < remaining.length; i++) {
|
|
31
|
+
const char = remaining[i];
|
|
32
|
+
if (escapeNext) {
|
|
33
|
+
escapeNext = false;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (char === '\\') {
|
|
37
|
+
escapeNext = true;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (char === '"') {
|
|
41
|
+
inString = !inString;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!inString) {
|
|
45
|
+
if (char === '{') {
|
|
46
|
+
braceCount++;
|
|
47
|
+
}
|
|
48
|
+
else if (char === '}') {
|
|
49
|
+
braceCount--;
|
|
50
|
+
if (braceCount === 0) {
|
|
51
|
+
jsonEnd = i;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (jsonEnd < 0) {
|
|
58
|
+
remaining = remaining.substring(jsonStart);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
const jsonStr = remaining.substring(jsonStart, jsonEnd + 1);
|
|
62
|
+
const parsed = parseEventLine(jsonStr);
|
|
63
|
+
if (parsed) {
|
|
64
|
+
if (parsed.content !== undefined && !parsed.followupPrompt) {
|
|
65
|
+
events.push({ type: 'content', data: parsed.content });
|
|
66
|
+
}
|
|
67
|
+
else if (parsed.name && parsed.toolUseId) {
|
|
68
|
+
events.push({
|
|
69
|
+
type: 'toolUse',
|
|
70
|
+
data: {
|
|
71
|
+
name: parsed.name,
|
|
72
|
+
toolUseId: parsed.toolUseId,
|
|
73
|
+
input: parsed.input || '',
|
|
74
|
+
stop: parsed.stop || false
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else if (parsed.input !== undefined && !parsed.name) {
|
|
79
|
+
events.push({
|
|
80
|
+
type: 'toolUseInput',
|
|
81
|
+
data: {
|
|
82
|
+
input: parsed.input
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else if (parsed.stop !== undefined && parsed.contextUsagePercentage === undefined) {
|
|
87
|
+
events.push({
|
|
88
|
+
type: 'toolUseStop',
|
|
89
|
+
data: {
|
|
90
|
+
stop: parsed.stop
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
else if (parsed.contextUsagePercentage !== undefined) {
|
|
95
|
+
events.push({
|
|
96
|
+
type: 'contextUsage',
|
|
97
|
+
data: {
|
|
98
|
+
contextUsagePercentage: parsed.contextUsagePercentage
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
searchStart = jsonEnd + 1;
|
|
104
|
+
if (searchStart >= remaining.length) {
|
|
105
|
+
remaining = '';
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (searchStart > 0 && remaining.length > 0) {
|
|
110
|
+
remaining = remaining.substring(searchStart);
|
|
111
|
+
}
|
|
112
|
+
return { events, remaining };
|
|
113
|
+
}
|
|
114
|
+
export function findRealTag(buffer, tag) {
|
|
115
|
+
const codeBlockPattern = /```[\s\S]*?```/g;
|
|
116
|
+
const codeBlocks = [];
|
|
117
|
+
let match;
|
|
118
|
+
while ((match = codeBlockPattern.exec(buffer)) !== null) {
|
|
119
|
+
codeBlocks.push([match.index, match.index + match[0].length]);
|
|
120
|
+
}
|
|
121
|
+
let pos = 0;
|
|
122
|
+
while ((pos = buffer.indexOf(tag, pos)) !== -1) {
|
|
123
|
+
let inCodeBlock = false;
|
|
124
|
+
for (const [start, end] of codeBlocks) {
|
|
125
|
+
if (pos >= start && pos < end) {
|
|
126
|
+
inCodeBlock = true;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (!inCodeBlock) {
|
|
131
|
+
return pos;
|
|
132
|
+
}
|
|
133
|
+
pos += tag.length;
|
|
134
|
+
}
|
|
135
|
+
return -1;
|
|
136
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { StreamEvent, StreamState } from './types.js';
|
|
2
|
+
export declare function ensureBlockStart(blockType: 'thinking' | 'text', streamState: StreamState): StreamEvent[];
|
|
3
|
+
export declare function stopBlock(index: number | null, streamState: StreamState): StreamEvent[];
|
|
4
|
+
export declare function createTextDeltaEvents(text: string, streamState: StreamState): StreamEvent[];
|
|
5
|
+
export declare function createThinkingDeltaEvents(thinking: string, streamState: StreamState): StreamEvent[];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export function ensureBlockStart(blockType, streamState) {
|
|
2
|
+
if (blockType === 'thinking') {
|
|
3
|
+
if (streamState.thinkingBlockIndex != null)
|
|
4
|
+
return [];
|
|
5
|
+
const idx = streamState.nextBlockIndex++;
|
|
6
|
+
streamState.thinkingBlockIndex = idx;
|
|
7
|
+
return [
|
|
8
|
+
{
|
|
9
|
+
type: 'content_block_start',
|
|
10
|
+
index: idx,
|
|
11
|
+
content_block: { type: 'thinking', thinking: '' }
|
|
12
|
+
}
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
if (blockType === 'text') {
|
|
16
|
+
if (streamState.textBlockIndex != null)
|
|
17
|
+
return [];
|
|
18
|
+
const idx = streamState.nextBlockIndex++;
|
|
19
|
+
streamState.textBlockIndex = idx;
|
|
20
|
+
return [
|
|
21
|
+
{
|
|
22
|
+
type: 'content_block_start',
|
|
23
|
+
index: idx,
|
|
24
|
+
content_block: { type: 'text', text: '' }
|
|
25
|
+
}
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
export function stopBlock(index, streamState) {
|
|
31
|
+
if (index == null)
|
|
32
|
+
return [];
|
|
33
|
+
if (streamState.stoppedBlocks.has(index))
|
|
34
|
+
return [];
|
|
35
|
+
streamState.stoppedBlocks.add(index);
|
|
36
|
+
return [{ type: 'content_block_stop', index }];
|
|
37
|
+
}
|
|
38
|
+
export function createTextDeltaEvents(text, streamState) {
|
|
39
|
+
if (!text)
|
|
40
|
+
return [];
|
|
41
|
+
const events = [];
|
|
42
|
+
events.push(...ensureBlockStart('text', streamState));
|
|
43
|
+
events.push({
|
|
44
|
+
type: 'content_block_delta',
|
|
45
|
+
index: streamState.textBlockIndex,
|
|
46
|
+
delta: { type: 'text_delta', text }
|
|
47
|
+
});
|
|
48
|
+
return events;
|
|
49
|
+
}
|
|
50
|
+
export function createThinkingDeltaEvents(thinking, streamState) {
|
|
51
|
+
const events = [];
|
|
52
|
+
events.push(...ensureBlockStart('thinking', streamState));
|
|
53
|
+
events.push({
|
|
54
|
+
type: 'content_block_delta',
|
|
55
|
+
index: streamState.thinkingBlockIndex,
|
|
56
|
+
delta: { type: 'thinking_delta', thinking }
|
|
57
|
+
});
|
|
58
|
+
return events;
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function transformKiroStream(response: Response, model: string, conversationId: string): AsyncGenerator<any>;
|