@codeproxy/core 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/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # @codeproxy/core
2
+
3
+ > **中文版** → [README.zh-CN.md](./README.zh-CN.md)
4
+
5
+ **@codeproxy/core** is a zero-dependency TypeScript library that converts upstream LLM responses in **Chat Completions** or **Anthropic Messages** format into the **OpenAI Responses API** format, so that AI coding agents like Codex and Claude Code can work with any language model.
6
+
7
+ The goal: **let any language model work directly in Codex or Claude Code**, no matter what API format the upstream provider speaks.
8
+
9
+ ## Quick Start
10
+
11
+ ```ts
12
+ import OpenAI from 'openai';
13
+ import { createResponsesFetch } from '@codeproxy/core';
14
+
15
+ const client = new OpenAI({
16
+ fetch: createResponsesFetch({
17
+ upstreamFormat: 'openai-chat',
18
+ baseUrl: 'https://api.deepseek.com/v1',
19
+ defaultHeaders: {
20
+ authorization: `Bearer ${process.env.DEEPSEEK_API_KEY}`,
21
+ },
22
+ }),
23
+ });
24
+
25
+ const response = await client.responses.create({
26
+ model: 'deepseek-v4-pro',
27
+ input: 'Hello!',
28
+ });
29
+ ```
30
+
31
+ ## How it works
32
+
33
+ ```
34
+ Upstream API (Chat Completions / Anthropic Messages)
35
+
36
+ ▼ createResponsesFetch translates automatically
37
+
38
+ OpenAI Responses API ←── consumed by Codex / Claude Code
39
+ ```
40
+
41
+ ## Install
42
+
43
+ ```bash
44
+ npm install @codeproxy/core
45
+ ```
46
+
47
+ ## API
48
+
49
+ ### `createResponsesFetch(options)`
50
+
51
+ Creates a `fetch` wrapper that translates Responses API traffic to the configured upstream format.
52
+
53
+ | Option | Type | Description |
54
+ |---|---|---|
55
+ | `upstreamFormat` | `'anthropic'` \| `'openai-chat'` | Upstream API format (inferred from `baseUrl` if omitted) |
56
+ | `baseUrl` | `string` | Upstream endpoint URL |
57
+ | `apiVersion` | `string` | Override Anthropic version header |
58
+ | `model` | `string` | Override the model for all requests |
59
+ | `defaultHeaders` | `Record<string, string>` | Extra headers sent to upstream |
60
+ | `dropImages` | `boolean` | Strip image parts from messages (text-only models) |
61
+ | `timeoutMs` | `number` | Upstream request timeout |
62
+ | `onCacheStats` | `(stats) => void` | Receive cache usage stats |
63
+ | `fallbackUpstream` | `object` | When `dropImages: true` and the request contains images, automatically route to this upstream instead (e.g. a vision-capable model) |
64
+
65
+ ### Translators
66
+
67
+ Low-level translators are also available by namespace:
68
+
69
+ - `anthropic.translateRequest` / `anthropic.translateResponse` / `anthropic.translateStream`
70
+ - `openai.translateRequest` / `openai.translateResponse` / `openai.translateStream`
71
+ - `translate.*` — unified re-exports
72
+
73
+ ### Types
74
+
75
+ All Responses API, Anthropic, and OpenAI Chat types are exported from the package root.
76
+
77
+ ### Utilities
78
+
79
+ - `parseSseStream(stream)` — Consume `ReadableStream` → parsed SSE messages
80
+ - `encodeSseEvent(event, data)` — Serialize SSE event to wire format
81
+ - `makeId(prefix)` — Generate monotonic-ish IDs
82
+
83
+ ## License
84
+
85
+ MIT
@@ -0,0 +1,85 @@
1
+ # @codeproxy/core
2
+
3
+ > **English** → [README.md](./README.md)
4
+
5
+ **@codeproxy/core** 是一个零依赖的 TypeScript 库,将上游 LLM 的 Chat Completions 或 Anthropic Messages 格式**转换为 OpenAI Responses API 格式**,让 Codex、Claude Code 等 AI 编码代理能够直接调用任意语言模型。
6
+
7
+ 目标:**让任何语言模型都能直接在 Codex 或 Claude Code 中使用**,无论上游提供商使用什么 API 格式。
8
+
9
+ ## 快速开始
10
+
11
+ ```ts
12
+ import OpenAI from 'openai';
13
+ import { createResponsesFetch } from '@codeproxy/core';
14
+
15
+ const client = new OpenAI({
16
+ fetch: createResponsesFetch({
17
+ upstreamFormat: 'openai-chat',
18
+ baseUrl: 'https://api.deepseek.com/v1',
19
+ defaultHeaders: {
20
+ authorization: `Bearer ${process.env.DEEPSEEK_API_KEY}`,
21
+ },
22
+ }),
23
+ });
24
+
25
+ const response = await client.responses.create({
26
+ model: 'deepseek-v4-pro',
27
+ input: 'Hello!',
28
+ });
29
+ ```
30
+
31
+ ## 工作原理
32
+
33
+ ```
34
+ 上游 API (Chat Completions / Anthropic Messages)
35
+
36
+ ▼ createResponsesFetch 自动转换
37
+
38
+ OpenAI Responses API ←── 供 Codex / Claude Code 消费
39
+ ```
40
+
41
+ ## 安装
42
+
43
+ ```bash
44
+ npm install @codeproxy/core
45
+ ```
46
+
47
+ ## API
48
+
49
+ ### `createResponsesFetch(options)`
50
+
51
+ 创建一个 `fetch` 包装器,自动将 Responses API 流量转换到配置的上游格式。
52
+
53
+ | 参数 | 类型 | 说明 |
54
+ |---|---|---|
55
+ | `upstreamFormat` | `'anthropic'` \| `'openai-chat'` | 上游 API 格式(省略时从 baseUrl 推断) |
56
+ | `baseUrl` | `string` | 上游端点 URL |
57
+ | `apiVersion` | `string` | 覆盖 Anthropic 版本头 |
58
+ | `model` | `string` | 为所有请求覆盖模型名 |
59
+ | `defaultHeaders` | `Record<string, string>` | 发送给上游的额外请求头 |
60
+ | `dropImages` | `boolean` | 从消息中移除图片部分(纯文本模型) |
61
+ | `timeoutMs` | `number` | 上游请求超时 |
62
+ | `onCacheStats` | `(stats) => void` | 接收缓存使用统计 |
63
+ | `fallbackUpstream` | `object` | 当 `dropImages: true` 且请求包含图片时,自动切换到该上游(如支持视觉的模型) |
64
+
65
+ ### 转换器
66
+
67
+ 底层转换器也按命名空间导出:
68
+
69
+ - `anthropic.translateRequest` / `anthropic.translateResponse` / `anthropic.translateStream`
70
+ - `openai.translateRequest` / `openai.translateResponse` / `openai.translateStream`
71
+ - `translate.*` — 统一重新导出
72
+
73
+ ### 类型
74
+
75
+ 所有 Responses API、Anthropic 和 OpenAI Chat 类型均从包根目录导出。
76
+
77
+ ### 工具函数
78
+
79
+ - `parseSseStream(stream)` — 消费 `ReadableStream` → 解析后的 SSE 消息
80
+ - `encodeSseEvent(event, data)` — 序列化 SSE 事件为线格式
81
+ - `makeId(prefix)` — 生成单调递增的 ID
82
+
83
+ ## 协议
84
+
85
+ MIT