FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
commandcode-api-proxy/src/config.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
/
config.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
123 lines (112 loc) · 3.88 KB
Breadcrumbs
commandcode-api-proxy
/
src
/
config.ts
Copy path
File metadata and controls
123 lines (112 loc) · 3.88 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import
{
readAuthKey
}
from
"@/auth.js"
;
interface
CliArgs
{
host
?:
string
;
port
?:
string
;
"api-key"
?:
string
;
"setup-opencode"
?:
boolean
;
}
export
interface
Config
{
host
:
string
;
port
:
number
;
apiKey
:
string
|
null
;
ccApiBase
:
string
;
ccVersion
:
string
;
logLevel
:
string
;
corsOrigin
:
string
;
/** Max wall-clock ms for upstream to send response headers + first byte. */
upstreamTimeoutMs
:
number
;
/** Max ms between consecutive chunks during streaming. 0 = disabled. */
idleTimeoutMs
:
number
;
}
/**
* Hardcoded CLI version fallback. The real CLI ships frequent releases, so
* `fetchLatestCliVersion()` should be used to refresh this at startup. CC's
* server actively blocks requests whose version looks stale or absent.
*/
export
const
DEFAULT_CC_VERSION
=
"0.40.3"
;
const
CC_VERSION_REFRESH_MS
=
24
*
60
*
60
*
1000
;
let
cachedVersion
:
string
|
null
=
null
;
let
lastFetchAt
=
0
;
/**
* Fetch the latest published `command-code` CLI version from the npm registry.
* Returns `null` on any failure (caller falls back to DEFAULT_CC_VERSION).
* Cached for CC_VERSION_REFRESH_MS.
*/
export
async
function
fetchLatestCliVersion
(
)
:
Promise
<
string
|
null
>
{
if
(
cachedVersion
&&
Date
.
now
(
)
-
lastFetchAt
<
CC_VERSION_REFRESH_MS
)
{
return
cachedVersion
;
}
try
{
const
res
=
await
fetch
(
"https://registry.npmjs.org/command-code/latest"
,
{
signal
:
AbortSignal
.
timeout
(
10_000
)
,
}
)
;
if
(
!
res
.
ok
)
return
null
;
const
pkg
=
(
await
res
.
json
(
)
)
as
{
version
?:
string
}
;
if
(
pkg
.
version
&&
typeof
pkg
.
version
===
"string"
)
{
cachedVersion
=
pkg
.
version
;
lastFetchAt
=
Date
.
now
(
)
;
return
cachedVersion
;
}
return
null
;
}
catch
{
return
null
;
}
}
function
parseCliArgs
(
)
:
CliArgs
{
const
args
=
process
.
argv
.
slice
(
2
)
;
const
map
:
CliArgs
=
{
}
;
for
(
let
i
=
0
;
i
<
args
.
length
;
i
++
)
{
const
arg
=
args
[
i
]
;
if
(
arg
.
startsWith
(
"--"
)
)
{
const
key
=
arg
.
slice
(
2
)
;
const
val
=
args
[
i
+
1
]
;
if
(
val
&&
!
val
.
startsWith
(
"--"
)
)
{
(
map
as
Record
<
string
,
string
|
undefined
>
)
[
key
]
=
val
;
i
++
;
}
else
{
(
map
as
Record
<
string
,
boolean
>
)
[
key
]
=
true
;
}
}
}
return
map
;
}
export
function
loadConfig
(
)
:
Config
{
const
cli
=
parseCliArgs
(
)
;
const
host
=
cli
.
host
||
process
.
env
.
HOST
||
"127.0.0.1"
;
const
port
=
parseInt
(
cli
.
port
||
process
.
env
.
PORT
||
"8787"
,
10
)
;
const
apiKey
=
cli
[
"api-key"
]
||
process
.
env
.
CC_API_KEY
||
readAuthKey
(
)
;
const
ccApiBase
=
process
.
env
.
CC_API_BASE
||
"https://api.commandcode.ai"
;
const
ccVersion
=
process
.
env
.
CC_CLI_VERSION
||
cachedVersion
||
DEFAULT_CC_VERSION
;
const
logLevel
=
process
.
env
.
LOG_LEVEL
||
"info"
;
// `*` is fine for a localhost proxy; restrict (e.g. to an origin or leave
// empty to disable) before exposing the proxy on a network.
const
corsOrigin
=
process
.
env
.
CORS_ORIGIN
??
"*"
;
// Upstream timeouts. The connection timeout covers the wall-clock time
// until the upstream returns response headers + first byte — bump it for
// slow reasoning models. The idle timeout catches stalled streams where
// the upstream opened the connection but stopped sending chunks
// mid-response (e.g. tool call hung on the upstream side). Set
// CC_IDLE_TIMEOUT_MS=0 to disable idle detection entirely.
const
upstreamTimeoutMs
=
parsePositiveInt
(
process
.
env
.
CC_UPSTREAM_TIMEOUT_MS
,
600_000
,
// 10 minutes — covers high-effort reasoning models
)
;
const
idleTimeoutMs
=
parsePositiveInt
(
process
.
env
.
CC_IDLE_TIMEOUT_MS
,
120_000
)
;
// 2 minutes
return
{
host
,
port
,
apiKey
,
ccApiBase
,
ccVersion
,
logLevel
,
corsOrigin
,
upstreamTimeoutMs
,
idleTimeoutMs
,
}
;
}
function
parsePositiveInt
(
raw
:
string
|
undefined
,
fallback
:
number
)
:
number
{
if
(
raw
==
null
||
raw
===
""
)
return
fallback
;
const
n
=
Number
(
raw
)
;
if
(
!
Number
.
isFinite
(
n
)
||
n
<
0
)
return
fallback
;
return
Math
.
floor
(
n
)
;
}
Back
|
FazBrowse Home
|
New Git URL