FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
commandcode-api-proxy/src/stream.ts at main · thaolaptrinh/commandcode-api-proxy · GitHub
thaolaptrinh
/
commandcode-api-proxy
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
18
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
commandcode-api-proxy
/
src
/
stream.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
66 lines (55 loc) · 1.82 KB
Breadcrumbs
commandcode-api-proxy
/
src
/
stream.ts
Copy path
File metadata and controls
66 lines (55 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import
type
{
CCEvent
}
from
"@/translate/types.js"
;
/**
* Result from parsing a single line of CC NDJSON data.
*/
export
interface
ParsedChunk
{
type
:
"event"
|
"done"
|
"ping"
|
"unknown"
;
event
?:
CCEvent
;
}
/**
* Parse a single NDJSON line from the CC stream.
*/
export
function
parseCCLine
(
line
:
string
)
:
ParsedChunk
{
const
trimmed
=
line
.
trim
(
)
;
if
(
!
trimmed
||
trimmed
===
""
)
return
{
type
:
"ping"
}
;
// The CC API sends JSON lines prefixed with "data: "
// Handle both with and without prefix
const
dataStr
=
trimmed
.
startsWith
(
"data: "
)
?
trimmed
.
slice
(
6
)
:
trimmed
;
if
(
dataStr
===
"[DONE]"
)
return
{
type
:
"done"
}
;
try
{
const
parsed
=
JSON
.
parse
(
dataStr
)
;
// CC API format 1 (flat): { type: "text-delta", id: "txt-0", text: "4" }
// CC API format 2 (nested): { type: "text-delta", data: { text: "Hello" }
}
// Merge all non-type fields into data.
if
(
parsed
&&
typeof
parsed
===
"object"
&&
parsed
.
type
)
{
const
{
type
,
id
:
_id
,
...
rest
}
=
parsed
;
const
data
=
parsed
.
data
&&
typeof
parsed
.
data
===
"object"
?
parsed
.
data
:
rest
;
return
{
type
:
"event"
,
event
:
{
type
,
data
}
,
}
;
}
return
{
type
:
"unknown"
}
;
}
catch
{
// If we can't parse as JSON, it might be a ping or keepalive
return
{
type
:
"ping"
}
;
}
}
/**
* Format an object as an SSE message string.
*/
export
function
formatSSE
(
data
:
object
)
:
string
{
return
`data:
${
JSON
.
stringify
(
data
)
}
\n\n`
;
}
/**
* Format the [DONE] signal for non-streaming mode.
*/
export
function
formatSSEDone
(
)
:
string
{
return
"data: [DONE]\n\n"
;
}
export
function
formatAnthropicSSE
(
eventType
:
string
,
data
:
unknown
)
:
string
{
if
(
eventType
===
"message_stop"
)
{
return
"event: message_stop\ndata: {}\n\n"
;
}
return
`event:
${
eventType
}
\ndata:
${
JSON
.
stringify
(
data
)
}
\n\n`
;
}
Back
|
FazBrowse Home
|
New Git URL