Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/models.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
"name": "openai",
"driver": "openai",
"protocol": "openai-responses",
"base_url": "https://api.openai.com/v1",
"api_key_env": "OPENAI_API_KEY",
"default_model": "gpt-5.5",
Expand Down Expand Up @@ -86,6 +87,7 @@
{
"name": "gemini",
"driver": "gemini",
"protocol": "gemini",
"base_url": "https://generativelanguage.googleapis.com/v1beta",
"api_key_env": "GEMINI_API_KEY",
"default_model": "gemini-3.1-pro-preview",
Expand Down Expand Up @@ -143,6 +145,7 @@
{
"name": "deepseek",
"driver": "deepseek",
"protocol": "deepseek-chat",
"base_url": "https://api.deepseek.com",
"api_key_env": "DEEPSEEK_API_KEY",
"default_model": "deepseek-v4-pro",
Expand Down Expand Up @@ -180,6 +183,7 @@
{
"name": "kimi",
"driver": "openai",
"protocol": "openai-chat",
"base_url": "https://api.moonshot.cn/v1",
"api_key_env": "KIMI_API_KEY",
"default_model": "kimi-k2.6",
Expand Down Expand Up @@ -211,6 +215,7 @@
{
"name": "qwen",
"driver": "openai",
"protocol": "openai-chat",
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"api_key_env": "QWEN_API_KEY",
"default_model": "qwen3.6-plus",
Expand Down Expand Up @@ -253,6 +258,7 @@
{
"name": "glm",
"driver": "openai",
"protocol": "openai-chat",
"base_url": "https://api.z.ai/api/paas/v4",
"api_key_env": "GLM_API_KEY",
"default_model": "glm-5.1",
Expand Down Expand Up @@ -284,6 +290,7 @@
{
"name": "mimo",
"driver": "openai",
"protocol": "openai-chat",
"base_url": "https://token-plan-cn.xiaomimimo.com/v1",
"api_key_env": "MIMO_API_KEY",
"default_model": "mimo-v2.5-pro",
Expand Down Expand Up @@ -315,6 +322,7 @@
{
"name": "minimax",
"driver": "openai",
"protocol": "openai-chat",
"base_url": "https://api.minimax.chat/v1/text/chatcompletion_v2",
"api_key_env": "MINIMAX_API_KEY",
"default_model": "MiniMax-M2.7",
Expand All @@ -336,6 +344,7 @@
{
"name": "sansen",
"driver": "openai",
"protocol": "openai-chat",
"base_url": "https://token.sensenova.cn/v1",
"api_key_env": "SANSE_API_KEY",
"default_model": "deepseek-v4-flash",
Expand Down
30 changes: 27 additions & 3 deletions packages/codingcode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ export const AgentLayer = Layer.effect(
const llmMessages = [...history.right];

let content = '';
let reasoningText = '';
let reasoningProvider = '';
const toolCalls: ToolCall[] = [];
let responded: ResponseMeta = {};

Expand All @@ -437,7 +439,11 @@ export const AgentLayer = Layer.effect(
abortSignal
)) {
if (abortSignal?.aborted) break;
if (part.type === 'text') {
if (part.type === 'reasoning') {
reasoningText += part.text;
reasoningProvider = part.provider;
emit({ family: 'event', event: { type: 'reasoning_delta', text: part.text } });
} else if (part.type === 'text') {
content += part.text;
emit({ family: 'event', event: { type: 'text_delta', text: part.text } });
} else if (part.type === 'tool_call') {
Expand Down Expand Up @@ -473,6 +479,17 @@ export const AgentLayer = Layer.effect(
// 流结束帧(携带 usage):在流外统一投递
yield* turn.transition(sid, { kind: 'running', responded });

const reasoning = reasoningText
? {
displayText: reasoningText,
provider: reasoningProvider || 'unknown',
replayData: { kind: 'full_text' as const, value: reasoningText },
...(responded.usage?.reasoningTokens !== undefined
? { reasoningTokens: responded.usage.reasoningTokens }
: {}),
}
: undefined;

if (toolCalls.length === 0) {
if (content.trim() === '' && !abortSignal?.aborted) {
const detail =
Expand All @@ -492,7 +509,13 @@ export const AgentLayer = Layer.effect(
return Result.err(emptyErr);
}

const assistantEv = yield* session.recordAssistant(state, content, [], responded.usage);
const assistantEv = yield* session.recordAssistant(
state,
content,
[],
responded.usage,
reasoning
);
yield* context.absorb(sessionRef, [assistantEv]);
const stopDecision = yield* hooks.emitDecision('agent.turn.stop', {
sessionId: sid,
Expand Down Expand Up @@ -542,7 +565,8 @@ export const AgentLayer = Layer.effect(
state,
content,
toolCalls,
responded.usage
responded.usage,
reasoning
);
yield* context.absorb(sessionRef, [assistantToolEv]);

Expand Down
24 changes: 23 additions & 1 deletion packages/codingcode/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ export function buildContextMessages(
break;
case 'assistant': {
const ev = event as AssistantEvent;
const msg: Message = { role: 'assistant', content: [textPart(event.content)] };
const msg: Message = {
role: 'assistant',
content: [textPart(event.content)],
reasoning: ev.reasoning,
};
if (event.toolCalls && event.toolCalls.length > 0) {
msg.tool_calls = event.toolCalls.map((tc) => ({
id: tc.id,
Expand Down Expand Up @@ -197,6 +201,24 @@ export function buildContextMessages(
if (curr.role === 'assistant' && curr.tool_calls && curr.tool_calls.length > 0) continue;
// parts 数组本身就是分隔符,providers 侧按 part 逐个送出
prev.content = [...prev.content, ...curr.content];
if (curr.role === 'assistant' && curr.reasoning) {
if (prev.reasoning) {
prev.reasoning = {
...prev.reasoning,
displayText: `${prev.reasoning.displayText}\n${curr.reasoning.displayText}`,
...(prev.reasoning.replayData && curr.reasoning.replayData
? {
replayData: {
kind: 'full_text' as const,
value: `${prev.reasoning.replayData.value}\n${curr.reasoning.replayData.value}`,
},
}
: {}),
};
} else {
prev.reasoning = curr.reasoning;
}
}
filtered.splice(i, 1);
}
}
Expand Down
29 changes: 29 additions & 0 deletions packages/codingcode/src/infra/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ import { loadConfig, updateActiveModel } from './config.js';
export interface ModelCapabilitiesEntry {
vision?: 'supported' | 'unsupported';
audio?: 'supported' | 'unsupported';
reasoning?: 'supported' | 'unsupported';
thinking_levels?: string[];
default_thinking_level?: string;
thinking_force_enabled?: boolean;
}

export interface ModelReasoningCapability {
supported: boolean;
mode: 'optional' | 'always';
levels: string[];
defaultLevel?: string;
}

/** 准入要看的能力位。 */
export interface ModelCapabilities {
vision: boolean;
audio: boolean;
reasoning?: ModelReasoningCapability;
}

/** 模型清单里一条可被会话选中的模型。 */
Expand All @@ -24,6 +36,7 @@ export interface SelectableModel {
model: string;
base_url: string;
api_key_env: string;
protocol?: string;
context_window: number;
capabilities: ModelCapabilities;
}
Expand All @@ -41,6 +54,7 @@ export interface ProviderEntry {
base_url: string;
api_key_env: string;
default_model: string;
protocol?: string;
models: ModelDescriptor[];
}

Expand Down Expand Up @@ -83,10 +97,25 @@ export function flattenModels(cat: ProviderCatalog): SelectableModel[] {
model: m.id,
base_url: p.base_url,
api_key_env: p.api_key_env,
protocol:
p.protocol ??
(p.name === 'openai'
? 'openai-responses'
: p.name === 'deepseek'
? 'deepseek-chat'
: 'openai-chat'),
context_window: m.context_window ?? DEFAULT_CONTEXT_WINDOW,
capabilities: {
vision: m.capabilities?.vision === 'supported',
audio: m.capabilities?.audio === 'supported',
reasoning: {
supported: m.capabilities?.reasoning === 'supported',
mode: m.capabilities?.thinking_force_enabled ? 'always' : 'optional',
levels: [...(m.capabilities?.thinking_levels ?? [])],
...(m.capabilities?.default_thinking_level
? { defaultLevel: m.capabilities.default_thinking_level }
: {}),
},
},
});
}
Expand Down
6 changes: 5 additions & 1 deletion packages/codingcode/src/llm/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ function clientFor(entry: SelectableModel): Effect.Effect<LLMClient, AgentError>
baseURL: entry.base_url,
apiKey,
});
return new OpenAIProvider(provider.chat(entry.model), entry);
const model =
entry.protocol === 'openai-responses'
? provider.responses(entry.model)
: provider.chat(entry.model);
return new OpenAIProvider(model, entry);
}
case 'deepseek': {
const { createDeepSeek } = yield* Effect.tryPromise({
Expand Down
28 changes: 28 additions & 0 deletions packages/codingcode/src/llm/providers/deepseek.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import type { SelectableModel } from '../../infra/models.js';

import { convertMessages, convertTools, toTokenUsage } from './shared.js';

function reasoningOptions(req: LLMRequest, entry: SelectableModel): Record<string, any> | undefined {
const capability = entry.capabilities.reasoning;
if (!capability?.supported) return undefined;
const mode = req.reasoning?.mode;
const level = req.reasoning?.level ?? capability.defaultLevel;
const thinkingType = mode === 'disabled' ? 'disabled' : mode === 'auto' ? 'adaptive' : 'enabled';
return {
deepseek: {
thinking: { type: thinkingType },
...(level ? { reasoningEffort: level } : {}),
},
};
}

export class DeepSeekProvider implements LLMClient {
constructor(
private model: LanguageModelV3,
Expand All @@ -32,12 +46,21 @@ export class DeepSeekProvider implements LLMClient {
system: req.system,
messages: convertMessages(req.messages),
tools: convertTools(req.tools),
providerOptions: reasoningOptions(req, this.entry),
stopWhen: req.maxSteps ? stepCountIs(req.maxSteps) : undefined,
abortSignal: signal,
});

return {
content: result.text,
reasoning:
typeof result.reasoning === 'string' && result.reasoning
? {
displayText: result.reasoning,
provider: this.entry.provider,
replayData: { kind: 'full_text', value: result.reasoning },
}
: undefined,
toolCalls:
result.toolCalls.length > 0
? result.toolCalls.map((tc) => ({
Expand All @@ -54,18 +77,23 @@ export class DeepSeekProvider implements LLMClient {
}

completeStream(req: LLMRequest, signal?: AbortSignal): AsyncIterable<LLMStreamPart> {
const providerName = this.entry.provider;
const result = streamText({
model: this.model,
system: req.system,
messages: convertMessages(req.messages),
tools: convertTools(req.tools),
providerOptions: reasoningOptions(req, this.entry),
stopWhen: req.maxSteps ? stepCountIs(req.maxSteps) : undefined,
abortSignal: signal,
});

return (async function* () {
for await (const part of result.fullStream) {
switch (part.type) {
case 'reasoning-delta':
yield { type: 'reasoning', text: part.text, provider: providerName };
break;
case 'text-delta':
yield { type: 'text', text: part.text };
break;
Expand Down
24 changes: 24 additions & 0 deletions packages/codingcode/src/llm/providers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import type { SelectableModel } from '../../infra/models.js';

import { convertMessages, convertTools, toTokenUsage } from './shared.js';

function reasoningOptions(req: LLMRequest, entry: SelectableModel): Record<string, any> | undefined {
const capability = entry.capabilities.reasoning;
if (entry.provider !== 'openai' || !capability?.supported) return undefined;
const level = req.reasoning?.level ?? capability.defaultLevel;
const options: Record<string, unknown> = {};
if (level) options.reasoningEffort = level;
if (entry.protocol === 'openai-responses') options.reasoningSummary = 'auto';
return Object.keys(options).length > 0 ? { openai: options } : undefined;
}

export class OpenAIProvider implements LLMClient {
constructor(
private model: LanguageModelV3,
Expand All @@ -32,12 +42,21 @@ export class OpenAIProvider implements LLMClient {
system: req.system,
messages: convertMessages(req.messages),
tools: convertTools(req.tools),
providerOptions: reasoningOptions(req, this.entry),
stopWhen: req.maxSteps ? stepCountIs(req.maxSteps) : undefined,
abortSignal: signal,
});

return {
content: result.text,
reasoning:
typeof result.reasoning === 'string' && result.reasoning
? {
displayText: result.reasoning,
provider: this.entry.provider,
replayData: { kind: 'full_text', value: result.reasoning },
}
: undefined,
toolCalls:
result.toolCalls.length > 0
? result.toolCalls.map((tc) => ({
Expand All @@ -54,6 +73,7 @@ export class OpenAIProvider implements LLMClient {
}

completeStream(req: LLMRequest, signal?: AbortSignal): AsyncIterable<LLMStreamPart> {
const providerName = this.entry.provider;
if (this.entry.provider === 'sansen' && req.tools && req.tools.length > 0) {
const complete = this.complete(req, signal);
return (async function* () {
Expand All @@ -73,13 +93,17 @@ export class OpenAIProvider implements LLMClient {
system: req.system,
messages: convertMessages(req.messages),
tools: convertTools(req.tools),
providerOptions: reasoningOptions(req, this.entry),
stopWhen: req.maxSteps ? stepCountIs(req.maxSteps) : undefined,
abortSignal: signal,
});

return (async function* () {
for await (const part of result.fullStream) {
switch (part.type) {
case 'reasoning-delta':
yield { type: 'reasoning', text: part.text, provider: providerName };
break;
case 'text-delta':
yield { type: 'text', text: part.text };
break;
Expand Down
Loading
Loading