@8-/gemini-web-api 1.0.0 → 1.0.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/README.md CHANGED
@@ -30,9 +30,16 @@
30
30
 
31
31
  - **Zero Configuration**: As long as you are logged into [gemini.google.com](https://gemini.google.com) in Chrome (or Brave / Edge), the server automatically extracts and decrypts your tokens. No manual cookie copying or `.env` required!
32
32
  - **Dual Runtime Support**: Native ultra-high performance on **Bun** (`Bun.serve`) and seamless native support on **Node.js** (>= 22.5.0, via `node:sqlite` and Web Streams).
33
- - **Zero External Dependencies**: 100% built on standard Web APIs (`fetch`, `ReadableStream`, `crypto.subtle`) and built-in Node/Bun modules.
34
- - **OpenAI Drop-in Replacement**: Fully compatible with `/v1/chat/completions` and `/v1/models`. Works seamlessly with ChatGPT-Next-Web, LobeChat, Cherry Studio, OpenAI SDKs, etc.
35
- - **Reasoning & Thinking Models**: Full streaming support for `<think>` thoughts and `reasoning_content` deltas (compatible with DeepSeek/Gemini thinking UI renderers).
33
+ - **Zero External Dependencies**: 100% built on standard Web APIs (`fetch`, `ReadableStream`, `crypto.subtle`) and built-in Node/Bun modules. Zero `node_modules` required!
34
+ - **OpenAI Drop-in Replacement**: Fully compatible with `/v1/chat/completions` and `/v1/models`. Works seamlessly with ChatGPT-Next-Web, LobeChat, Cherry Studio, Cline, Roo Code, Cursor, Aider, and official OpenAI SDKs.
35
+ - **Robust 4-Tier Tool / Function Calling Engine**:
36
+ 1. **Tier 1 (Fenced Code Blocks)**: Parses ` ```tool_call `, ` ```tool_calls `, ` ```json `, or bare ` ``` ` blocks.
37
+ 2. **Tier 2 (Tool Call Arrays)**: Extracts structured `"tool_calls": [...]` arrays embedded in output.
38
+ 3. **Tier 3 (Unfenced / Broken Fences)**: Recovers missing opening backticks (e.g. `tool_call\n{...}````).
39
+ 4. **Tier 4 (Bare JSON Objects)**: Bracket-matched scanner for raw JSON objects with `name` and `arguments`/`args`/`input`.
40
+ - **Auto Syntax Healing**: Automatically cleans escaped markdown underscores (`subagent\_type` → `subagent_type`), trailing commas, unquoted keys, and invalid escapes.
41
+ - **Framework Agnostic**: Natively normalizes tool call formats from Vercel AI SDK, ZCode, LangChain, and OpenAI SDKs (`toolCalls`, `input`, `toolName`, `toolCallId`).
42
+ - **Reasoning & Thinking Models**: Full support for `<think>` thoughts and `reasoning_content` deltas in SSE streaming with an automatic tag closure state machine, as well as `reasoning_content` in non-streaming responses.
36
43
  - **Dynamic Model Discovery**: Automatically discovers available Gemini models (Flash, Pro, etc.) and capacity tiers directly from your web session.
37
44
  - **Image Proxy**: Built-in `/gemini-proxy/image` endpoint to safely render generated card images without CORS or anti-hotlinking issues.
38
45
 
@@ -101,7 +108,7 @@ PORT=8000 API_KEY=sk-mysecret ENABLE_THINKING=true gemini-web-api
101
108
 
102
109
  ### API Calling Examples
103
110
 
104
- #### Test with cURL (Streaming)
111
+ #### 1. Streaming Chat (cURL)
105
112
 
106
113
  ```bash
107
114
  curl -N http://127.0.0.1:7860/v1/chat/completions \
@@ -115,28 +122,48 @@ curl -N http://127.0.0.1:7860/v1/chat/completions \
115
122
  }'
116
123
  ```
117
124
 
118
- #### Use with OpenAI Python SDK
125
+ #### 2. Function Calling / Tool Calling (Python SDK)
119
126
 
120
127
  ```python
121
128
  from openai import OpenAI
122
129
 
123
130
  client = OpenAI(
124
131
  base_url="http://127.0.0.1:7860/v1",
125
- api_key="none" # Any string if API_KEY is not set
132
+ api_key="none"
126
133
  )
127
134
 
135
+ tools = [
136
+ {
137
+ "type": "function",
138
+ "function": {
139
+ "name": "get_weather",
140
+ "description": "Get current weather for a city.",
141
+ "parameters": {
142
+ "type": "object",
143
+ "properties": {
144
+ "city": {"type": "string"}
145
+ },
146
+ "required": ["city"]
147
+ }
148
+ }
149
+ }
150
+ ]
151
+
128
152
  response = client.chat.completions.create(
129
153
  model="gemini-flash",
130
- messages=[{"role": "user", "content": "Hello Gemini!"}],
131
- stream=True
154
+ messages=[{"role": "user", "content": "What is the weather in Tokyo?"}],
155
+ tools=tools,
156
+ tool_choice="auto"
132
157
  )
133
158
 
134
- for chunk in response:
135
- content = chunk.choices[0].delta.content or ""
136
- print(content, end="", flush=True)
159
+ message = response.choices[0].message
160
+ if message.tool_calls:
161
+ for tool_call in message.tool_calls:
162
+ print(f"Tool: {tool_call.function.name}")
163
+ print(f"Arguments: {tool_call.function.arguments}")
137
164
  ```
138
165
 
139
- #### Use with OpenAI Node.js SDK
166
+ #### 3. Streaming Chat with Node.js SDK
140
167
 
141
168
  ```javascript
142
169
  import OpenAI from "openai";
@@ -148,7 +175,7 @@ const openai = new OpenAI({
148
175
 
149
176
  const stream = await openai.chat.completions.create({
150
177
  model: "gemini-flash",
151
- messages: [{ role: "user", content: "Tell me a joke." }],
178
+ messages: [{ role: "user", content: "Tell me a short joke." }],
152
179
  stream: true,
153
180
  });
154
181
 
@@ -173,9 +200,16 @@ for await (const chunk of stream) {
173
200
 
174
201
  - **开箱即用,零手动配置**:自动探测 Chrome / Brave / Edge 浏览器会话,全自动解密并获取登录令牌,彻底告别手动抓包复制 Cookie 的烦恼!
175
202
  - **Bun 与 Node.js 双运行时支持**:在 **Bun** 下使用原生 `Bun.serve` 极速响应;在 **Node.js** (>= 22.5.0) 下利用内置 `node:sqlite` 与原生 Web Streams 同样流畅运行。
176
- - **零第三方外部依赖**:纯基于 Web 标准接口(`fetch`、`ReadableStream`、`crypto.subtle`)与运行时内置标准模块构建。
177
- - **无缝替代 OpenAI 接口**:标准 `/v1/chat/completions` 与 `/v1/models` 路由,直接兼容 ChatGPT-Next-Web、LobeChat、Cherry Studio 以及各类 OpenAI SDK。
178
- - **深度思考与推理模式**:完整支持 `<think>` 思考标签与 SSE `reasoning_content` 流式字段传输,兼容各类思考过程前端渲染。
203
+ - **零第三方外部依赖**:纯基于 Web 标准接口(`fetch`、`ReadableStream`、`crypto.subtle`)与运行时内置标准模块构建,零 `node_modules` 外部依赖!
204
+ - **无缝替代 OpenAI 接口**:标准 `/v1/chat/completions` 与 `/v1/models` 路由,直接兼容 ChatGPT-Next-Web、LobeChat、Cherry Studio、Cline、Roo Code、Cursor、Aider 以及官方 OpenAI SDK。
205
+ - **四级高鲁棒性 Function Calling / 工具调用引擎**:
206
+ 1. **第一级(围栏代码块)**:精确解析 ` ```tool_call `、` ```tool_calls `、` ```json ` 或裸反引号代码块。
207
+ 2. **第二级(数组提取)**:自动提取内嵌于回复中的 `"tool_calls": [...]` 结构体。
208
+ 3. **第三级(缺失开头围栏容错)**:支持缺失开头反引号等残缺输出自愈(如 `tool_call\n{...}````)。
209
+ 4. **第四级(无围栏裸 JSON 深度探测)**:括号平衡算法精确提取包含 `name` 与 `arguments`/`args`/`input` 的独立 JSON 对象,游标防跳步确保多个紧邻调用不遗漏。
210
+ - **语法自动修复**:自动反转义 Markdown 下划线(`subagent\_type` → `subagent_type`),智能修复未加引号的键名与尾随逗号。
211
+ - **框架协议泛化**:原生兼容 Vercel AI SDK、ZCode、LangChain 及各类 Agent 框架自定义字段(`toolCalls`、`input`、`toolName`、`toolCallId`)。
212
+ - **深度思考与推理模式**:完整支持 `<think>` 思考标签与 SSE `reasoning_content` 流式字段传输,内置状态机保证思考标签严格闭合,且在非流式调用中完整保留思考链。
179
213
  - **动态模型列表发现**:启动时自动从 Gemini 网页端动态拉取已解锁的可用模型及配额层级。
180
214
  - **图片安全代理**:内置 `/gemini-proxy/image` 接口,解决 Google 生成图片防盗链和跨域展示问题。
181
215
 
@@ -244,7 +278,7 @@ PORT=8000 API_KEY=sk-mysecret ENABLE_THINKING=true gemini-web-api
244
278
 
245
279
  ### 接口调用示例
246
280
 
247
- #### cURL 流式对话调用
281
+ #### 1. cURL 流式对话调用
248
282
 
249
283
  ```bash
250
284
  curl -N http://127.0.0.1:7860/v1/chat/completions \
@@ -258,7 +292,7 @@ curl -N http://127.0.0.1:7860/v1/chat/completions \
258
292
  }'
259
293
  ```
260
294
 
261
- #### Python (OpenAI SDK)
295
+ #### 2. 工具调用 / Function Calling (Python SDK)
262
296
 
263
297
  ```python
264
298
  from openai import OpenAI
@@ -268,18 +302,37 @@ client = OpenAI(
268
302
  api_key="none" # 未设置 API_KEY 时可填任意字符串
269
303
  )
270
304
 
305
+ tools = [
306
+ {
307
+ "type": "function",
308
+ "function": {
309
+ "name": "get_weather",
310
+ "description": "查询指定城市的天气温度",
311
+ "parameters": {
312
+ "type": "object",
313
+ "properties": {
314
+ "city": {"type": "string"}
315
+ },
316
+ "required": ["city"]
317
+ }
318
+ }
319
+ }
320
+ ]
321
+
271
322
  response = client.chat.completions.create(
272
323
  model="gemini-flash",
273
- messages=[{"role": "user", "content": "你好,Gemini!"}],
274
- stream=True
324
+ messages=[{"role": "user", "content": "查询东京的天气"}],
325
+ tools=tools,
326
+ tool_choice="auto"
275
327
  )
276
328
 
277
- for chunk in response:
278
- content = chunk.choices[0].delta.content or ""
279
- print(content, end="", flush=True)
329
+ message = response.choices[0].message
330
+ if message.tool_calls:
331
+ for tool_call in message.tool_calls:
332
+ print(f"触发工具: {tool_call.function.name}, 参数: {tool_call.function.arguments}")
280
333
  ```
281
334
 
282
- #### Node.js (OpenAI SDK)
335
+ #### 3. Node.js 流式对话 (OpenAI SDK)
283
336
 
284
337
  ```javascript
285
338
  import OpenAI from "openai";