mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 06:30:10 +08:00
⬆️ chore: migrate from Claude Code SDK to Claude Agent SDK v0.1.1
- Replace @anthropic-ai/claude-code with @anthropic-ai/claude-agent-sdk@0.1.1 - Update all import statements across 4 files - Migrate patch for Electron compatibility (fork vs spawn) - Handle breaking changes: replace appendSystemPrompt with systemPrompt preset - Add settingSources configuration for filesystem settings - Update vendor path in build scripts - Update package name mapping in CodeToolsService
This commit is contained in:
parent
51630f95fd
commit
422ba52093
@ -1,27 +1,27 @@
|
||||
diff --git a/sdk.mjs b/sdk.mjs
|
||||
index e2dbafb4e2faa1bf2b6b02f0009a2b9bbf57c757..ea333ae8c69fcd27a9f2d89b3dbfc0a3e4e4dec4 100755
|
||||
index 461e9a2ba246778261108a682762ffcf26f7224e..44bd667d9f591969d36a105ba5eb8b478c738dd8 100644
|
||||
--- a/sdk.mjs
|
||||
+++ b/sdk.mjs
|
||||
@@ -6213,7 +6213,7 @@ function createAbortController(maxListeners = DEFAULT_MAX_LISTENERS) {
|
||||
@@ -6215,7 +6215,7 @@ function createAbortController(maxListeners = DEFAULT_MAX_LISTENERS) {
|
||||
}
|
||||
|
||||
// src/transport/ProcessTransport.ts
|
||||
// ../src/transport/ProcessTransport.ts
|
||||
-import { spawn } from "child_process";
|
||||
+import { fork } from "child_process";
|
||||
import { createInterface } from "readline";
|
||||
|
||||
// src/utils/fsOperations.ts
|
||||
@@ -6452,13 +6452,12 @@ class ProcessTransport {
|
||||
// ../src/utils/fsOperations.ts
|
||||
@@ -6473,14 +6473,11 @@ class ProcessTransport {
|
||||
const errorMessage = isNativeBinary(pathToClaudeCodeExecutable) ? `Claude Code native binary not found at ${pathToClaudeCodeExecutable}. Please ensure Claude Code is installed via native installer or specify a valid path with options.pathToClaudeCodeExecutable.` : `Claude Code executable not found at ${pathToClaudeCodeExecutable}. Is options.pathToClaudeCodeExecutable set?`;
|
||||
throw new ReferenceError(errorMessage);
|
||||
}
|
||||
const isNative = isNativeBinary(pathToClaudeCodeExecutable);
|
||||
- const isNative = isNativeBinary(pathToClaudeCodeExecutable);
|
||||
- const spawnCommand = isNative ? pathToClaudeCodeExecutable : executable;
|
||||
- const spawnArgs = isNative ? args : [...executableArgs, pathToClaudeCodeExecutable, ...args];
|
||||
- this.logDebug(isNative ? `Spawning Claude Code native binary: ${pathToClaudeCodeExecutable} ${args.join(" ")}` : `Spawning Claude Code process: ${executable} ${[...executableArgs, pathToClaudeCodeExecutable, ...args].join(" ")}`);
|
||||
- this.logForDebugging(isNative ? `Spawning Claude Code native binary: ${pathToClaudeCodeExecutable} ${args.join(" ")}` : `Spawning Claude Code process: ${executable} ${[...executableArgs, pathToClaudeCodeExecutable, ...args].join(" ")}`);
|
||||
+ this.logDebug(`Forking Claude Code Node.js process: ${pathToClaudeCodeExecutable} ${args.join(" ")}`);
|
||||
const stderrMode = env.DEBUG || stderr ? "pipe" : "ignore";
|
||||
- this.child = spawn(spawnCommand, spawnArgs, {
|
||||
+
|
||||
+ this.logDebug(`Forking Claude Code Node.js process: ${pathToClaudeCodeExecutable} ${args.join(" ")}`);
|
||||
+ this.child = fork(pathToClaudeCodeExecutable, args, {
|
||||
cwd,
|
||||
- stdio: ["pipe", "pipe", stderrMode],
|
||||
@ -78,7 +78,7 @@
|
||||
"release:aicore": "yarn workspace @cherrystudio/ai-core version patch --immediate && yarn workspace @cherrystudio/ai-core npm publish --access public"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/claude-code": "patch:@anthropic-ai/claude-code@npm%3A1.0.118#~/.yarn/patches/@anthropic-ai-claude-code-npm-1.0.118-bbf4e9e59f.patch",
|
||||
"@anthropic-ai/claude-agent-sdk": "patch:@anthropic-ai/claude-agent-sdk@npm%3A0.1.1#~/.yarn/patches/@anthropic-ai-claude-agent-sdk-npm-0.1.1-d937b73fed.patch",
|
||||
"@libsql/client": "0.14.0",
|
||||
"@libsql/win32-x64-msvc": "^0.4.7",
|
||||
"@napi-rs/system-ocr": "patch:@napi-rs/system-ocr@npm%3A1.0.2#~/.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch",
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import type { SDKMessage } from '@anthropic-ai/claude-code'
|
||||
import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk'
|
||||
import type { ContentBlockParam } from '@anthropic-ai/sdk/resources/messages'
|
||||
|
||||
export type ClaudeCodeRawValue =
|
||||
|
||||
@ -35,7 +35,7 @@ const allX64 = {
|
||||
'@napi-rs/system-ocr-win32-x64-msvc': '1.0.2'
|
||||
}
|
||||
|
||||
const claudeCodeVenderPath = '@anthropic-ai/claude-code/vendor'
|
||||
const claudeCodeVenderPath = '@anthropic-ai/claude-agent-sdk/vendor'
|
||||
const claudeCodeVenders = ['arm64-darwin', 'arm64-linux', 'x64-darwin', 'x64-linux', 'x64-win32']
|
||||
|
||||
const platformToArch = {
|
||||
|
||||
@ -73,7 +73,7 @@ class CodeToolsService {
|
||||
public async getPackageName(cliTool: string) {
|
||||
switch (cliTool) {
|
||||
case codeTools.claudeCode:
|
||||
return '@anthropic-ai/claude-code'
|
||||
return '@anthropic-ai/claude-agent-sdk'
|
||||
case codeTools.geminiCli:
|
||||
return '@google/gemini-cli'
|
||||
case codeTools.openaiCodex:
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import type { SDKMessage } from '@anthropic-ai/claude-code'
|
||||
import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { ClaudeStreamState, transformSDKMessageToStreamParts } from '../transform'
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
import { EventEmitter } from 'node:events'
|
||||
import { createRequire } from 'node:module'
|
||||
|
||||
import { McpHttpServerConfig, Options, query, SDKMessage } from '@anthropic-ai/claude-code'
|
||||
import { McpHttpServerConfig, Options, query, SDKMessage } from '@anthropic-ai/claude-agent-sdk'
|
||||
import { loggerService } from '@logger'
|
||||
import { config as apiConfigService } from '@main/apiServer/config'
|
||||
import { validateModelId } from '@main/apiServer/utils'
|
||||
@ -27,7 +27,7 @@ class ClaudeCodeService implements AgentServiceInterface {
|
||||
|
||||
constructor() {
|
||||
// Resolve Claude Code CLI robustly (works in dev and in asar)
|
||||
this.claudeExecutablePath = require_.resolve('@anthropic-ai/claude-code/cli.js')
|
||||
this.claudeExecutablePath = require_.resolve('@anthropic-ai/claude-agent-sdk/cli.js')
|
||||
if (app.isPackaged) {
|
||||
this.claudeExecutablePath = this.claudeExecutablePath.replace(/\.asar([\\/])/, '.asar.unpacked$1')
|
||||
}
|
||||
@ -106,7 +106,10 @@ class ClaudeCodeService implements AgentServiceInterface {
|
||||
logger.warn('claude stderr', { chunk })
|
||||
errorChunks.push(chunk)
|
||||
},
|
||||
appendSystemPrompt: session.instructions,
|
||||
systemPrompt: session.instructions
|
||||
? session.instructions
|
||||
: { type: 'preset', preset: 'claude_code' },
|
||||
settingSources: ['project', ],
|
||||
includePartialMessages: true,
|
||||
permissionMode: session.configuration?.permission_mode,
|
||||
maxTurns: session.configuration?.max_turns,
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
* emitting `text-*` parts and a synthetic `finish-step`.
|
||||
*/
|
||||
|
||||
import { SDKMessage } from '@anthropic-ai/claude-code'
|
||||
import { SDKMessage } from '@anthropic-ai/claude-agent-sdk'
|
||||
import type { BetaStopReason } from '@anthropic-ai/sdk/resources/beta/messages/messages.mjs'
|
||||
import { loggerService } from '@logger'
|
||||
import type { FinishReason, LanguageModelUsage, ProviderMetadata, TextStreamPart } from 'ai'
|
||||
|
||||
41
yarn.lock
41
yarn.lock
@ -397,9 +397,9 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@anthropic-ai/claude-code@npm:1.0.118":
|
||||
version: 1.0.118
|
||||
resolution: "@anthropic-ai/claude-code@npm:1.0.118"
|
||||
"@anthropic-ai/claude-agent-sdk@npm:^0.1.1":
|
||||
version: 0.1.1
|
||||
resolution: "@anthropic-ai/claude-agent-sdk@npm:0.1.1"
|
||||
dependencies:
|
||||
"@img/sharp-darwin-arm64": "npm:^0.33.5"
|
||||
"@img/sharp-darwin-x64": "npm:^0.33.5"
|
||||
@ -420,38 +420,7 @@ __metadata:
|
||||
optional: true
|
||||
"@img/sharp-win32-x64":
|
||||
optional: true
|
||||
bin:
|
||||
claude: cli.js
|
||||
checksum: 10c0/90e9feac857bfcbdaa3797c70cccfad8588ed45bc9ea6c88664e2f08fa71d069cfb28ce17c20a67ddd841d9a8c7a50e61281687975f7d5ebde9b39f63551cefb
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@anthropic-ai/claude-code@patch:@anthropic-ai/claude-code@npm%3A1.0.118#~/.yarn/patches/@anthropic-ai-claude-code-npm-1.0.118-bbf4e9e59f.patch":
|
||||
version: 1.0.118
|
||||
resolution: "@anthropic-ai/claude-code@patch:@anthropic-ai/claude-code@npm%3A1.0.118#~/.yarn/patches/@anthropic-ai-claude-code-npm-1.0.118-bbf4e9e59f.patch::version=1.0.118&hash=d20711"
|
||||
dependencies:
|
||||
"@img/sharp-darwin-arm64": "npm:^0.33.5"
|
||||
"@img/sharp-darwin-x64": "npm:^0.33.5"
|
||||
"@img/sharp-linux-arm": "npm:^0.33.5"
|
||||
"@img/sharp-linux-arm64": "npm:^0.33.5"
|
||||
"@img/sharp-linux-x64": "npm:^0.33.5"
|
||||
"@img/sharp-win32-x64": "npm:^0.33.5"
|
||||
dependenciesMeta:
|
||||
"@img/sharp-darwin-arm64":
|
||||
optional: true
|
||||
"@img/sharp-darwin-x64":
|
||||
optional: true
|
||||
"@img/sharp-linux-arm":
|
||||
optional: true
|
||||
"@img/sharp-linux-arm64":
|
||||
optional: true
|
||||
"@img/sharp-linux-x64":
|
||||
optional: true
|
||||
"@img/sharp-win32-x64":
|
||||
optional: true
|
||||
bin:
|
||||
claude: cli.js
|
||||
checksum: 10c0/6a6261562c25ceb83b7c8c976fddadb782f03e50bb73580446f0ca49ac8e9f9a6158fa1b5bf93f070be49bf3a0e8e06adefdea49f71706258e4c9200a3fa52e7
|
||||
checksum: 10c0/6b6e34eb4e871fc5d0120c311054b757831dfb953110f9f9d7af0202f26a16c9059e7d0a1c002dc581afb50ccf20f100670f0b3a6682696f6b4ddeeea1d0d8d0
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@ -14202,7 +14171,7 @@ __metadata:
|
||||
"@ai-sdk/mistral": "npm:^2.0.14"
|
||||
"@ai-sdk/perplexity": "npm:^2.0.9"
|
||||
"@ant-design/v5-patch-for-react-19": "npm:^1.0.3"
|
||||
"@anthropic-ai/claude-code": "patch:@anthropic-ai/claude-code@npm%3A1.0.118#~/.yarn/patches/@anthropic-ai-claude-code-npm-1.0.118-bbf4e9e59f.patch"
|
||||
"@anthropic-ai/claude-agent-sdk": "npm:^0.1.1"
|
||||
"@anthropic-ai/sdk": "npm:^0.41.0"
|
||||
"@anthropic-ai/vertex-sdk": "patch:@anthropic-ai/vertex-sdk@npm%3A0.11.4#~/.yarn/patches/@anthropic-ai-vertex-sdk-npm-0.11.4-c19cb41edb.patch"
|
||||
"@aws-sdk/client-bedrock": "npm:^3.840.0"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user