@leoustc/service-llm 0.2.0 → 0.2.1
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 +201 -0
- package/README.md +151 -35
- package/VERSION +1 -1
- package/dist/auth.js +28 -1
- package/dist/cli.js +34 -6
- package/dist/http.js +22 -4
- package/dist/index.js +1 -1
- package/package.json +3 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,79 +1,195 @@
|
|
|
1
|
-
#
|
|
1
|
+
# service-llm
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Use your ChatGPT Codex subscription through OpenAI-compatible HTTP APIs or MCP.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
`service-llm` supports Chat Completions, Responses, SSE streaming, function calls, and an MCP `ask` tool. It returns model tool calls but never executes them.
|
|
6
6
|
|
|
7
7
|
## Requirements
|
|
8
8
|
|
|
9
9
|
- Node.js 22.19 or newer
|
|
10
|
-
- A ChatGPT
|
|
10
|
+
- A ChatGPT account with Codex access
|
|
11
11
|
|
|
12
|
-
## Install
|
|
12
|
+
## Install
|
|
13
13
|
|
|
14
14
|
```sh
|
|
15
15
|
npm install --global @leoustc/service-llm
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Log in
|
|
19
|
+
|
|
20
|
+
```sh
|
|
16
21
|
service-llm login
|
|
17
22
|
```
|
|
18
23
|
|
|
19
|
-
For a headless
|
|
24
|
+
For a headless or remote machine:
|
|
20
25
|
|
|
21
26
|
```sh
|
|
22
27
|
service-llm login --device-code
|
|
23
28
|
```
|
|
24
29
|
|
|
25
|
-
|
|
30
|
+
Credentials are stored securely at `~/.service-llm/auth.json`. The available Codex models are recorded at `~/.service-llm/models.json`.
|
|
31
|
+
|
|
32
|
+
If you start the service before logging in, the first model request returns a device code and verification URL. Complete the browser login, then retry the request.
|
|
33
|
+
|
|
34
|
+
## Choose a model
|
|
35
|
+
|
|
36
|
+
List supported models and show the current default:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
service-llm model
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The default is marked with `*`. Set a different default with:
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
service-llm model set gpt-5.4-mini
|
|
46
|
+
```
|
|
26
47
|
|
|
27
|
-
|
|
48
|
+
An explicit valid `model` in an API request overrides this default for that request.
|
|
28
49
|
|
|
29
|
-
## Start the
|
|
50
|
+
## Start the server
|
|
30
51
|
|
|
31
52
|
```sh
|
|
32
|
-
service-llm
|
|
53
|
+
service-llm serve
|
|
33
54
|
```
|
|
34
55
|
|
|
35
|
-
The
|
|
56
|
+
The server listens on `127.0.0.1:7060` by default.
|
|
36
57
|
|
|
37
|
-
|
|
58
|
+
At startup it prints the version, listening URL, authentication state, default model, supported-model count, endpoint URLs, and debug status. API key values and OAuth credentials are never displayed.
|
|
38
59
|
|
|
39
|
-
|
|
60
|
+
Choose a different address or port:
|
|
40
61
|
|
|
41
62
|
```sh
|
|
42
|
-
|
|
43
|
-
make run PORT=8080 HOST=0.0.0.0 API_KEYS=secret-one,secret-two
|
|
63
|
+
service-llm serve --host 0.0.0.0 --port 8080
|
|
44
64
|
```
|
|
45
65
|
|
|
46
|
-
|
|
66
|
+
Protect the API with one or more client keys:
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
service-llm serve --api-key secret-one,secret-two
|
|
70
|
+
```
|
|
47
71
|
|
|
48
|
-
|
|
72
|
+
Clients may send a key through either header:
|
|
73
|
+
|
|
74
|
+
```http
|
|
75
|
+
Authorization: Bearer secret-one
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```http
|
|
79
|
+
X-API-Key: secret-one
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Do not expose the server on a public interface without authentication and appropriate network controls.
|
|
83
|
+
|
|
84
|
+
## Chat Completions
|
|
49
85
|
|
|
50
86
|
```sh
|
|
51
|
-
|
|
52
|
-
|
|
87
|
+
curl http://127.0.0.1:7060/v1/chat/completions \
|
|
88
|
+
-H 'Content-Type: application/json' \
|
|
89
|
+
-d '{
|
|
90
|
+
"model": "gpt-5.4",
|
|
91
|
+
"messages": [
|
|
92
|
+
{"role": "user", "content": "Write a short story."}
|
|
93
|
+
]
|
|
94
|
+
}'
|
|
53
95
|
```
|
|
54
96
|
|
|
55
|
-
|
|
97
|
+
Add `"stream": true` to receive OpenAI-compatible SSE chunks.
|
|
56
98
|
|
|
57
|
-
##
|
|
99
|
+
## Responses API
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
curl http://127.0.0.1:7060/v1/responses \
|
|
103
|
+
-H 'Content-Type: application/json' \
|
|
104
|
+
-d '{
|
|
105
|
+
"model": "gpt-5.4",
|
|
106
|
+
"input": "Write a short story."
|
|
107
|
+
}'
|
|
108
|
+
```
|
|
58
109
|
|
|
59
|
-
|
|
60
|
-
- `POST /v1/chat/completions`, including `stream: true` SSE responses
|
|
61
|
-
- `POST /v1/responses`, including typed SSE responses
|
|
62
|
-
- `POST /mcp`, providing the MCP `ask` tool. `ask` accepts a prompt plus optional `model`, `tools`, and `tool_choice`, and returns model tool calls without executing them.
|
|
110
|
+
Add `"stream": true` to receive typed Responses SSE events.
|
|
63
111
|
|
|
64
|
-
|
|
112
|
+
## Function calls
|
|
65
113
|
|
|
66
|
-
|
|
114
|
+
Supply OpenAI-compatible tools and `tool_choice`. The model may return a function call, but `service-llm` will not execute it.
|
|
67
115
|
|
|
68
116
|
```sh
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
117
|
+
curl http://127.0.0.1:7060/v1/responses \
|
|
118
|
+
-H 'Content-Type: application/json' \
|
|
119
|
+
-d '{
|
|
120
|
+
"model": "gpt-5.4",
|
|
121
|
+
"input": "Write a short story and submit it with save_story.",
|
|
122
|
+
"tools": [{
|
|
123
|
+
"type": "function",
|
|
124
|
+
"name": "save_story",
|
|
125
|
+
"description": "Save a generated story",
|
|
126
|
+
"parameters": {
|
|
127
|
+
"type": "object",
|
|
128
|
+
"properties": {
|
|
129
|
+
"title": {"type": "string"},
|
|
130
|
+
"story": {"type": "string"}
|
|
131
|
+
},
|
|
132
|
+
"required": ["title", "story"]
|
|
133
|
+
}
|
|
134
|
+
}],
|
|
135
|
+
"tool_choice": "required"
|
|
136
|
+
}'
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## MCP
|
|
140
|
+
|
|
141
|
+
Connect an MCP client to:
|
|
142
|
+
|
|
143
|
+
```text
|
|
144
|
+
http://127.0.0.1:7060/mcp
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
The server provides one tool:
|
|
148
|
+
|
|
149
|
+
- `ask`: accepts `prompt` and optional `model`, `tools`, and `tool_choice` arguments.
|
|
150
|
+
|
|
151
|
+
Example JSON-RPC request:
|
|
152
|
+
|
|
153
|
+
```sh
|
|
154
|
+
curl http://127.0.0.1:7060/mcp \
|
|
155
|
+
-H 'Content-Type: application/json' \
|
|
156
|
+
-d '{
|
|
157
|
+
"jsonrpc": "2.0",
|
|
158
|
+
"id": 1,
|
|
159
|
+
"method": "tools/call",
|
|
160
|
+
"params": {
|
|
161
|
+
"name": "ask",
|
|
162
|
+
"arguments": {"prompt": "Write a short story."}
|
|
163
|
+
}
|
|
164
|
+
}'
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Model selection
|
|
168
|
+
|
|
169
|
+
Specify a model with the request's `model` field. If it is omitted or unavailable, the service uses your configured default. On first login, the lowest-cost available model is selected with low reasoning effort. Responses always report the model actually used.
|
|
170
|
+
|
|
171
|
+
## Debug logging
|
|
172
|
+
|
|
173
|
+
Enable request, response, and SSE logs with:
|
|
174
|
+
|
|
175
|
+
```sh
|
|
176
|
+
DEBUG=service-llm service-llm serve
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Authentication headers are redacted. Prompts and model output are not redacted, so avoid debug logging for sensitive requests.
|
|
180
|
+
|
|
181
|
+
## Health check
|
|
182
|
+
|
|
183
|
+
```sh
|
|
184
|
+
curl http://127.0.0.1:7060/health
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Expected response:
|
|
188
|
+
|
|
189
|
+
```json
|
|
190
|
+
{"status":"ok"}
|
|
74
191
|
```
|
|
75
192
|
|
|
76
|
-
|
|
193
|
+
## License
|
|
77
194
|
|
|
78
|
-
|
|
79
|
-
The suite sends the live `Write a short story.` prompt through Chat Completions, Responses, and MCP `ask`. Override the HTTP request model with `make test TEST_MODEL=gpt-5.4-mini`.
|
|
195
|
+
Apache License 2.0. See [LICENSE](LICENSE).
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.2.
|
|
1
|
+
0.2.1
|
package/dist/auth.js
CHANGED
|
@@ -27,9 +27,16 @@ async function saveModelCatalog() {
|
|
|
27
27
|
const models = getModels("openai-codex");
|
|
28
28
|
const fallback = smallestModel(models);
|
|
29
29
|
if (!fallback) throw new Error("No OpenAI Codex models are available");
|
|
30
|
+
let previousDefault;
|
|
31
|
+
try {
|
|
32
|
+
previousDefault = JSON.parse(await readFile(modelsPath, "utf8")).default_model;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error?.code !== "ENOENT") console.error(`Unable to preserve model selection: ${error.message}`);
|
|
35
|
+
}
|
|
36
|
+
const availableIds = new Set(models.map((model) => model.id));
|
|
30
37
|
const catalog = {
|
|
31
38
|
updated_at: new Date().toISOString(),
|
|
32
|
-
default_model: fallback.id,
|
|
39
|
+
default_model: availableIds.has(previousDefault) ? previousDefault : fallback.id,
|
|
33
40
|
default_reasoning_effort: "low",
|
|
34
41
|
models: models.map((model) => ({ id: model.id, name: model.name })),
|
|
35
42
|
};
|
|
@@ -37,6 +44,26 @@ async function saveModelCatalog() {
|
|
|
37
44
|
return catalog;
|
|
38
45
|
}
|
|
39
46
|
|
|
47
|
+
export async function listModels() {
|
|
48
|
+
try {
|
|
49
|
+
return JSON.parse(await readFile(modelsPath, "utf8"));
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error?.code !== "ENOENT") throw new Error(`Unable to read ${modelsPath}: ${error.message}`);
|
|
52
|
+
return saveModelCatalog();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function setDefaultModel(modelId) {
|
|
57
|
+
const catalog = await listModels();
|
|
58
|
+
if (!catalog.models.some((model) => model.id === modelId)) {
|
|
59
|
+
throw new Error(`Unsupported model: ${modelId}. Run 'service-llm model' to list supported models.`);
|
|
60
|
+
}
|
|
61
|
+
catalog.default_model = modelId;
|
|
62
|
+
catalog.updated_at = new Date().toISOString();
|
|
63
|
+
await writeSecureJson(modelsPath, catalog);
|
|
64
|
+
return catalog;
|
|
65
|
+
}
|
|
66
|
+
|
|
40
67
|
async function writeSecureJson(path, data) {
|
|
41
68
|
const operation = writeQueue.then(async () => {
|
|
42
69
|
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
package/dist/cli.js
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { login } from "./auth.js";
|
|
2
|
+
import { listModels, login, setDefaultModel } from "./auth.js";
|
|
3
3
|
import { startServer } from "./http.js";
|
|
4
4
|
|
|
5
5
|
function usage() {
|
|
6
6
|
console.error(`Usage:
|
|
7
7
|
service-llm login [--device-code]
|
|
8
|
-
service-llm
|
|
8
|
+
service-llm model [list]
|
|
9
|
+
service-llm model set <model-id>
|
|
10
|
+
service-llm serve [--port 7060] [--host 127.0.0.1] [--api-key key1,key2]`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function modelCommand(args) {
|
|
14
|
+
if (!args[1] || args[1] === "list") {
|
|
15
|
+
const catalog = await listModels();
|
|
16
|
+
console.log(`Supported Codex models (${catalog.models.length}):`);
|
|
17
|
+
for (const model of catalog.models) {
|
|
18
|
+
console.log(`${model.id === catalog.default_model ? "*" : " "} ${model.id}${model.name ? ` — ${model.name}` : ""}`);
|
|
19
|
+
}
|
|
20
|
+
console.log(`\n* default (${catalog.default_reasoning_effort ?? "low"} reasoning)`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (args[1] === "set") {
|
|
24
|
+
const modelId = args[2];
|
|
25
|
+
if (!modelId || args[3]) throw new Error("Usage: service-llm model set <model-id>");
|
|
26
|
+
await setDefaultModel(modelId);
|
|
27
|
+
console.log(`Default model set to ${modelId}`);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
throw new Error("Usage: service-llm model [list|set <model-id>]");
|
|
9
31
|
}
|
|
10
32
|
|
|
11
33
|
function valueAfter(args, names, fallback) {
|
|
@@ -21,9 +43,15 @@ if (args.includes("--help") || args.includes("-h")) {
|
|
|
21
43
|
usage();
|
|
22
44
|
} else if (args[0] === "login") {
|
|
23
45
|
await login({ deviceCode: args.includes("--device-code") });
|
|
24
|
-
} else {
|
|
25
|
-
|
|
46
|
+
} else if (args[0] === "model") {
|
|
47
|
+
await modelCommand(args);
|
|
48
|
+
} else if (args[0] === "serve") {
|
|
49
|
+
const serveArgs = args.slice(1);
|
|
50
|
+
const port = Number(valueAfter(serveArgs, ["--port"], "7060"));
|
|
26
51
|
if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("--port must be between 1 and 65535");
|
|
27
|
-
const apiKeyValue = valueAfter(
|
|
28
|
-
startServer({ port, host: valueAfter(
|
|
52
|
+
const apiKeyValue = valueAfter(serveArgs, ["--api-key", "-api_key"], process.env.SERVICE_LLM_API_KEYS ?? "");
|
|
53
|
+
startServer({ port, host: valueAfter(serveArgs, ["--host"], "127.0.0.1"), apiKeys: apiKeyValue.split(",").map((key) => key.trim()) });
|
|
54
|
+
} else {
|
|
55
|
+
usage();
|
|
56
|
+
process.exitCode = args.length ? 1 : 0;
|
|
29
57
|
}
|
package/dist/http.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createServer } from "node:http";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { DeviceLoginRequiredError } from "./auth.js";
|
|
3
|
+
import { DeviceLoginRequiredError, listModels } from "./auth.js";
|
|
4
4
|
import { version } from "./version.js";
|
|
5
5
|
import {
|
|
6
6
|
contentFromMessage,
|
|
@@ -231,9 +231,27 @@ export function startServer({ port = 7060, host = "127.0.0.1", apiKeys = [] } =
|
|
|
231
231
|
sendJson(response, error.status ?? 500, { error: { message: error.message ?? String(error), type: "service_error" } });
|
|
232
232
|
}
|
|
233
233
|
});
|
|
234
|
-
server.listen(port, host, () => {
|
|
235
|
-
|
|
236
|
-
|
|
234
|
+
server.listen(port, host, async () => {
|
|
235
|
+
const baseUrl = `http://${host}:${port}`;
|
|
236
|
+
console.error(`service-llm v${version}`);
|
|
237
|
+
console.error(`Listening: ${baseUrl}`);
|
|
238
|
+
console.error(`Client authentication: ${allowedKeys.size ? `enabled (${allowedKeys.size} key${allowedKeys.size === 1 ? "" : "s"})` : "disabled"}`);
|
|
239
|
+
try {
|
|
240
|
+
const catalog = await listModels();
|
|
241
|
+
console.error(`Default model: ${catalog.default_model} (${catalog.default_reasoning_effort ?? "low"} reasoning)`);
|
|
242
|
+
console.error(`Supported models: ${catalog.models.length} (run 'service-llm model' to list)`);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.error(`Model catalog: unavailable (${error.message ?? String(error)})`);
|
|
245
|
+
}
|
|
246
|
+
console.error("Endpoints:");
|
|
247
|
+
console.error(` Health: ${baseUrl}/health`);
|
|
248
|
+
console.error(` Chat Completions: ${baseUrl}/v1/chat/completions`);
|
|
249
|
+
console.error(` Responses: ${baseUrl}/v1/responses`);
|
|
250
|
+
console.error(` MCP: ${baseUrl}/mcp`);
|
|
251
|
+
console.error(`Debug logging: ${debugEnabled ? "enabled" : "disabled"}`);
|
|
252
|
+
if (!allowedKeys.size && !["127.0.0.1", "localhost", "::1"].includes(host)) {
|
|
253
|
+
console.error("WARNING: The service is listening beyond localhost without client authentication.");
|
|
254
|
+
}
|
|
237
255
|
});
|
|
238
256
|
return server;
|
|
239
257
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { authPath, getAccessToken, login } from "./auth.js";
|
|
1
|
+
export { authPath, getAccessToken, listModels, login, modelsPath, setDefaultModel } from "./auth.js";
|
|
2
2
|
export { defaultModel } from "./backend.js";
|
|
3
3
|
export { startServer } from "./http.js";
|
|
4
4
|
export { version } from "./version.js";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leoustc/service-llm",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Standalone CLI service exposing a Codex subscription through OpenAI-compatible and MCP endpoints.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"bin": {
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
|
-
"license": "
|
|
31
|
+
"license": "Apache-2.0"
|
|
32
32
|
}
|