Skip to content

Conversation

@2217173240
Copy link

🎯 功能概述

从官方仓库 PR #7 cherry-pick 了 OpenRouter Provider 功能,为 Kode SDK 添加多提供商支持。

来源: #7

✨ 新增功能

1. OpenRouter Provider

  • ✅ 完整实现 OpenRouterProvider
  • ✅ 支持 OpenAI 兼容的 Chat Completions API
  • ✅ 支持工具调用(Tool Calling)
  • ✅ 支持流式响应(Streaming)
  • ✅ 自动格式转换(OpenAI ↔ Anthropic 格式)

2. 提供商架构重构

  • ✅ 引入 createModelProvider 工厂函数
  • ✅ 将 AnthropicProvider 移至 src/infra/providers/ 目录
  • ✅ 统一提供商接口和导出方式

3. 新增示例

  • examples/openrouter-example.ts - 完整的使用示例
  • 演示了如何配置和使用 OpenRouter 提供商

📝 使用方法

基础用法

import { Agent, OpenRouterProvider } from '@shareai-lab/kode-sdk';

const agent = await Agent.create({
  templateId: 'my-template',
  model: new OpenRouterProvider(
    'YOUR_API_KEY',
    'anthropic/claude-3.5-sonnet',
    'https://openrouter.ai/api/v1'
  ),
}, deps);

使用工厂函数

import { Agent, createModelProvider } from '@shareai-lab/kode-sdk';

const agent = await Agent.create({
  templateId: 'my-template',
  modelConfig: {
    provider: 'openrouter',
    model: 'anthropic/claude-3.5-sonnet',
    apiKey: 'YOUR_API_KEY',
    baseUrl: 'https://openrouter.ai/api/v1',
  },
}, deps);

🔍 技术细节

文件改动

| 文件                              | 改动类型 | 说明                                    |
|-----------------------------------|----------|-----------------------------------------|
| src/infra/providers/openrouter.ts | 新增     | OpenRouter 提供商实现                   |
| src/infra/providers/anthropic.ts  | 新增     | Anthropic 提供商(从 provider.ts 迁移) |
| src/infra/providers/index.ts      | 新增     | 提供商模块导出                          |
| src/infra/provider.ts             | 重构     | 提取工厂函数,简化接口                  |
| src/core/agent.ts                 | 修改     | 支持多提供商                            |
| src/index.ts                      | 修改     | 导出新的提供商和工厂函数                |
| examples/openrouter-example.ts    | 新增     | 使用示例                                |
| package.json                      | 修改     | 更新依赖(如有)                        |

冲突解决

 cherry-pick 过程中遇到 src/index.ts 的导出冲突,已手动解决:
- 正确分离了 type exports  value exports
- 保持了与现有代码风格的一致性

 测试结果

-  所有 58 个单元测试通过 (100%)
-  TypeScript 编译成功
-   Breaking Changes

🔗 相关链接

- 官方 PR: https://github.com/shareAI-lab/Kode-agent-sdk/pull/7
- OpenRouter 文档: https://openrouter.ai/docs

📌 注意事项

1. API Key 配置: 使用 OpenRouter 需要在 https://openrouter.ai/ 获取 API Key
2. 模型命名: OpenRouter 使用 provider/model 格式(如 anthropic/claude-3.5-sonnet)
3. 计费: OpenRouter 按使用量计费,价格可能与直接使用 Anthropic API 不同

---
Cherry-picked from: 11a6fd1

Co-Authored-By: maidang1 liuwei.felix@bytedance.com

#### 方法二:使用 Git 命令行

```bash
# 查看当前状态
git log --oneline -3

# 确认推送成功
git branch -vv

# 访问 GitHub 创建 PR

---
📚 Forking Workflow 实战经验

 成功的关键点

1. 正确配置远程仓库
git remote add upstream <官方仓库URL>
git fetch upstream
2. 使用 cherry-pick 而非直接 merge
  - 更精确地选择需要的功能
  - 保持提交历史清晰
  - 避免引入不需要的改动
3. 冲突解决的标准流程
# 1. 遇到冲突后,手动编辑文件
# 2. 标记冲突已解决
git add <冲突文件>
# 3. 继续操作
git cherry-pick --continue
4. 测试驱动的验证
  - 先运行测试,确保没有破坏现有功能
  - 再推送代码

🎓 学到的教训

1. 分离 type  value exports
// ✅ 正确:分离导出
export { Foo } from './foo';
export type { Bar } from './foo';

// ❌ 错误:混合导出
export type { Foo, Bar } from './foo';  // Foo 是类,不是类型
2. cherry-pick 冲突是正常的
  - 不要惊慌,逐个文件解决
  - 理解双方的改动意图
  - 保持代码风格一致
3. 始终测试再推送
npm run test:unit  # 运行测试
git push          # 测试通过后再推送

2217173240 and others added 6 commits October 14, 2025 02:53
- Convert interface/type exports to 'export type' syntax
- Fixes runtime import errors in SDKEventBridge.integration.test.ts
- Part of M8.1.fix type export cleanup
## 问题描述

在 Windows 环境下运行单元测试时,遇到 ENOENT 错误,无法创建目录。
经过深入分析,发现问题不是中文路径,而是标识符中的冒号(:)在 Windows
文件系统中不合法(通过 Node.js 创建时)。

## 根本原因

Windows 不允许文件/目录名包含冒号,但代码中多处使用 `:` 作为分隔符:
- Agent ID: agt:01KCGMTE419G1ED239ZJ2AKNW9
- Fork ID: fork:1765790723759
- Snapshot ID: sfp:0
- Checkpoint ID: agentId:timestamp

这些标识符被用作目录/文件名,导致在 Windows 上创建失败。

## 修复内容

### 1. Agent ID 格式 (src/utils/agent-id.ts, src/core/agent.ts)
- 修改前: \`agt:\${timePart}\${randomPart}\`
- 修改后: \`agt-\${timePart}\${randomPart}\`

### 2. Fork ID 格式 (src/core/agent.ts, src/utils/session-id.ts)
- 修改前: \`fork:\${timestamp}\`
- 修改后: \`fork-\${timestamp}\`

### 3. Snapshot ID 格式 (src/core/agent.ts, src/utils/session-id.ts)
- 修改前: \`sfp:\${index}\`
- 修改后: \`sfp-\${index}\`

### 4. Checkpoint ID 格式 (src/core/checkpointer.ts, src/core/checkpointers/*.ts)
- 修改前: \`\${agentId}:\${Date.now()}\`
- 修改后: \`\${agentId}-\${Date.now()}\`

### 5. TypeScript 编译错误修复

#### MCP 客户端 (src/tools/mcp.ts)
- 问题: ClientCapabilities 不支持 tools 属性
- 修复: 移除 capabilities 中的 tools: {}

#### 工具定义 (src/tools/tool.ts)
- 问题: ZodType 类型不兼容
- 修复: 添加 as any 类型断言

#### 类型推断 (src/tools/type-inference.ts)
- 问题: z.record() 需要 2 个参数
- 修复: z.record(z.string(), z.any())

### 6. 测试框架修复

#### Todo 测试 (tests/unit/tools/todo.test.ts)
- 更新测试以匹配框架错误处理策略
- 框架在 tool.ts 中捕获所有异常并返回错误对象
- 测试现在检查 {ok: false, error: ...} 而非期望抛出异常

#### 测试路径 (tests/helpers/fixtures.ts)
- 修改前: path.join(__dirname, '../.tmp')
- 修改后: path.join(process.cwd(), 'tests', '.tmp')
- 使用相对路径避免绝对路径中的编码问题

## 测试结果

✅ 所有 58 个单元测试通过 (100%)
- TypeScript 编译成功
- Windows 文件系统兼容
- 错误处理机制正确

## 影响范围

- 向后不兼容: 旧的存储格式无法直接迁移(ID 格式改变)
- 建议: 标记为 breaking change,发布新的主版本
- 受益: 所有 Windows 用户可以正常运行测试和使用 SDK

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add tests/.tmp/ to .gitignore
- Remove previously tracked test temporary files from git
- These files are generated during test execution and should not be version controlled

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@2217173240 2217173240 changed the title Feat/add-openrouter-provider Feat/solved #6 issue and sync with PR #7 Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants