add claude code model selection (opus, sonnet, haiku)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,15 +2,11 @@ import type { ModelInfo } from './types';
|
||||
import { PI_CONFIG_DIR } from '../../data-path';
|
||||
import { logger } from './logger';
|
||||
|
||||
const CLAUDE_CODE_MODEL: ModelInfo = {
|
||||
id: 'claude-code',
|
||||
name: 'claude-code',
|
||||
provider: 'claude-code',
|
||||
contextWindow: 200000,
|
||||
maxTokens: 16000,
|
||||
reasoning: true,
|
||||
images: true,
|
||||
};
|
||||
const CLAUDE_CODE_MODELS: ModelInfo[] = [
|
||||
{ id: 'claude-code/opus', name: 'opus', provider: 'claude-code', contextWindow: 200000, maxTokens: 16000, reasoning: true, images: true },
|
||||
{ id: 'claude-code/sonnet', name: 'sonnet', provider: 'claude-code', contextWindow: 200000, maxTokens: 16000, reasoning: true, images: true },
|
||||
{ id: 'claude-code/haiku', name: 'haiku', provider: 'claude-code', contextWindow: 200000, maxTokens: 8192, reasoning: false, images: true },
|
||||
];
|
||||
|
||||
const CACHE_TTL_MS = 60_000;
|
||||
|
||||
@@ -36,7 +32,7 @@ const parseSize = (s?: string): number => {
|
||||
|
||||
export async function listPiModels(): Promise<ModelInfo[]> {
|
||||
if (cachedModels && Date.now() - cacheTimestamp < CACHE_TTL_MS) {
|
||||
return [...cachedModels, CLAUDE_CODE_MODEL];
|
||||
return [...cachedModels, ...CLAUDE_CODE_MODELS];
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -59,11 +55,11 @@ export async function listPiModels(): Promise<ModelInfo[]> {
|
||||
if (proc.exitCode !== 0) {
|
||||
const stderrText = proc.stderr.toString();
|
||||
logger.error('pi --list-models failed', { exitCode: proc.exitCode, stderr: stderrText.trim(), piBin });
|
||||
return [CLAUDE_CODE_MODEL];
|
||||
return [...CLAUDE_CODE_MODELS];
|
||||
}
|
||||
|
||||
const lines = output.trim().split('\n');
|
||||
if (lines.length < 2) return [CLAUDE_CODE_MODEL];
|
||||
if (lines.length < 2) return [...CLAUDE_CODE_MODELS];
|
||||
|
||||
// Parse fixed-width table: provider, model, context, max-out, thinking, images
|
||||
const header = lines[0]!;
|
||||
@@ -114,9 +110,9 @@ export async function listPiModels(): Promise<ModelInfo[]> {
|
||||
logger.info('pi --list-models returned', { count: models.length });
|
||||
cachedModels = models;
|
||||
cacheTimestamp = Date.now();
|
||||
return [...models, CLAUDE_CODE_MODEL];
|
||||
return [...models, ...CLAUDE_CODE_MODELS];
|
||||
} catch (err) {
|
||||
logger.error('Failed to run pi --list-models', { error: String(err) });
|
||||
return [CLAUDE_CODE_MODEL];
|
||||
return [...CLAUDE_CODE_MODELS];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,7 +275,7 @@ async function handleChat(
|
||||
userDefault,
|
||||
});
|
||||
|
||||
if (model === 'claude-code') {
|
||||
if (model.startsWith('claude-code')) {
|
||||
return handleClaudeCodeChat(ws, sessionId, model, msg);
|
||||
}
|
||||
|
||||
@@ -424,6 +424,7 @@ async function handleClaudeCodeChat(
|
||||
prompt: msg.prompt,
|
||||
sessionKey: sessionId,
|
||||
cwd,
|
||||
model,
|
||||
onEvent,
|
||||
});
|
||||
|
||||
@@ -553,7 +554,7 @@ async function handleStop(ws: ServerWebSocket<WSData>): Promise<void> {
|
||||
|
||||
if (session?.piProcess) {
|
||||
try {
|
||||
if (session.model === 'claude-code') {
|
||||
if (session.model.startsWith('claude-code')) {
|
||||
// Claude Code: kill the process directly
|
||||
session.piProcess.kill();
|
||||
logger.info('Killed Claude Code process', { sessionId });
|
||||
|
||||
@@ -104,13 +104,14 @@ export async function sendAndAwait(params: SendAndAwaitParams): Promise<SendAndA
|
||||
const override = channelModelOverrides.get(sessionId);
|
||||
const resolvedModel = params.model ?? override ?? (await getUserDefaultModel(params.userId)) ?? DEFAULT_MODEL;
|
||||
|
||||
if (resolvedModel === 'claude-code') {
|
||||
if (resolvedModel.startsWith('claude-code')) {
|
||||
return await sendClaudeCode({
|
||||
userId: params.userId,
|
||||
email: params.email,
|
||||
username: params.username,
|
||||
prompt: params.prompt,
|
||||
sessionKey: sessionId,
|
||||
model: resolvedModel,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ type ClaudeCodeParams = {
|
||||
username: string;
|
||||
prompt: string;
|
||||
sessionKey: string;
|
||||
model?: string;
|
||||
};
|
||||
|
||||
type ClaudeCodeResult = {
|
||||
@@ -151,6 +152,9 @@ export async function sendClaudeCode(params: ClaudeCodeParams): Promise<ClaudeCo
|
||||
|
||||
const claudeArgs = [CLAUDE_BIN, '-p', prompt, '--dangerously-skip-permissions', '--output-format', 'json'];
|
||||
|
||||
const subModel = params.model?.split('/')[1];
|
||||
if (subModel) claudeArgs.push('--model', subModel);
|
||||
|
||||
const existingSession = claudeCodeSessions.get(sessionKey);
|
||||
if (existingSession) {
|
||||
claudeArgs.push('--resume', existingSession);
|
||||
@@ -268,6 +272,7 @@ type ClaudeCodeStreamingParams = {
|
||||
prompt: string;
|
||||
sessionKey: string;
|
||||
cwd?: string;
|
||||
model?: string;
|
||||
onEvent: (event: PiEvent) => void;
|
||||
};
|
||||
|
||||
@@ -293,6 +298,9 @@ export async function sendClaudeCodeStreaming(params: ClaudeCodeStreamingParams)
|
||||
'--include-partial-messages',
|
||||
];
|
||||
|
||||
const subModel = params.model?.split('/')[1];
|
||||
if (subModel) claudeArgs.push('--model', subModel);
|
||||
|
||||
const existingSession = claudeCodeSessions.get(sessionKey);
|
||||
if (existingSession) {
|
||||
claudeArgs.push('--resume', existingSession);
|
||||
|
||||
Reference in New Issue
Block a user