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
1 change: 0 additions & 1 deletion packages/codingcode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
".": "./src/layer.ts",
"./server": "./src/server/index.ts",
"./approval/types": "./src/approval/types.ts",
"./agent/profile": "./src/agent/profile.ts",
"./sink/types": "./src/sink/types.ts",
"./llm/types": "./src/llm/types.ts",
"./todo/types": "./src/todo/types.ts",
Expand Down
147 changes: 82 additions & 65 deletions packages/codingcode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AgentError } from '../util/error.js';
import { Result } from '../util/result.js';
import { AgentService, ToolEnvPort } from './port.js';
import type { RunTurnOptions } from './port.js';
import { ApprovalService, getToolNames } from '../approval/port.js';
import { ApprovalService } from '../approval/port.js';
import { CheckpointService } from '../checkpoint/port.js';
import { ContextService } from '../context/port.js';
import { EventSinkService } from '../sink/port.js';
Expand All @@ -17,12 +17,13 @@ import { RulesService } from '../rules/port.js';
import { SessionService } from '../session/port.js';
import { SkillService } from '../skills/port.js';
import { ToolExecutorService } from '../tools/port.js';
import { buildSystemPrompt, renderSkillBlock } from './prompt.js';
import { BUILD_PROMPT, buildSystemPrompt, renderSkillBlock } from './prompt.js';
import type { FrameBody, FrameError, ResponseMeta, ToolOutcome } from '../sink/types.js';
import { isTurnEnd } from '../sink/types.js';
import type { SessionRef } from '../session/types.js';
import type { TodoItem } from '../todo/types.js';
import type { PermissionMode } from '../util/enums.js';
import type { SandboxMode, SandboxPolicy } from '../sandbox/types.js';
import type { ToolCatalog, ToolResult } from '../tools/types.js';
import { mediaKindOf, textOf, textPart, type IncomingPart } from '../llm/types.js';
import type { ToolCall } from '../llm/types.js';
Expand All @@ -31,8 +32,6 @@ import { sniffMediaMime } from '../util/media.js';
import { loadConfig } from '../infra/config.js';
import { createLogger } from '../infra/logger.js';
import { normalizePath } from '../util/path.js';
import { resolveProfile } from './profile.js';
import type { AgentProfile } from './profile.js';

function toolOutcomeOf(result: ToolResult): ToolOutcome {
return result.status === 'denied'
Expand Down Expand Up @@ -135,11 +134,11 @@ export const AgentLayer = Layer.effect(
let parentSessionId = opts.parentSessionId;
const model = opts.model;
if (!sessionId) {
if (!opts.activeProfile || !opts.permissionMode) {
if (!opts.permissionMode) {
return yield* Effect.fail(
new AgentError(
'CONFIG_MISSING',
'new session requires activeProfile and permissionMode'
'new session requires permissionMode'
)
);
}
Expand All @@ -148,7 +147,6 @@ export const AgentLayer = Layer.effect(
{
model,
title: textOf(input) || 'New session',
activeProfile: opts.activeProfile,
permissionMode: opts.permissionMode,
},
{
Expand Down Expand Up @@ -185,7 +183,6 @@ export const AgentLayer = Layer.effect(
}

return yield* Effect.gen(function* () {
const profileName = opts.activeProfile ?? state.activeProfile;
const effectivePerm = opts.permissionMode ?? state.permissionMode;
if (opts.permissionMode) {
yield* session.setPermissionMode(
Expand All @@ -196,24 +193,10 @@ export const AgentLayer = Layer.effect(
opts.parentChain
);
}
if (opts.activeProfile) {
yield* session.setActiveProfile(
normalizedCwd,
sid,
opts.activeProfile,
parentSessionId,
opts.parentChain
);
}

state.memorySnapshot = yield* memory.loadMemoryForPrompt(state.cwd);

const profile: AgentProfile | undefined = profileName
? resolveProfile(profileName)
: undefined;

const mcpTools = yield* mcp.listProjectMcpTools(normalizedCwd);
const catalog = yield* executor.prepare(getToolNames(profileName), mcpTools);
const catalog = yield* executor.prepare(mcpTools);

const toolEnv = yield* toolEnvPort.getToolEnv();

Expand Down Expand Up @@ -247,13 +230,14 @@ export const AgentLayer = Layer.effect(
{
state,
model,
profile,
catalog,
rulesText,
abortSignal: opts.signal,
sid,
projectPath: state.cwd,
permissionMode: effectivePerm,
sandboxMode: opts.sandboxMode,
sandboxPolicy: opts.sandboxPolicy,
parentChain: opts.parentChain ?? [],
systemPrompt: opts.systemPrompt,
},
Expand Down Expand Up @@ -307,13 +291,14 @@ export const AgentLayer = Layer.effect(
opts: {
state: any;
model: string;
profile: AgentProfile | undefined;
abortSignal: AbortSignal | undefined;
catalog: ToolCatalog;
rulesText: string;
sid: string;
projectPath: string;
permissionMode: PermissionMode;
sandboxMode?: SandboxMode;
sandboxPolicy?: SandboxPolicy;
parentChain: readonly string[];
systemPrompt?: string;
},
Expand All @@ -322,7 +307,6 @@ export const AgentLayer = Layer.effect(
const {
state,
model,
profile,
abortSignal,
catalog,
rulesText,
Expand All @@ -339,7 +323,7 @@ export const AgentLayer = Layer.effect(
platform: process.platform,
shell: process.env.SHELL || process.env.ComSpec || 'bash',
rules: rulesText,
profileSystemPrompt: opts.systemPrompt ?? profile?.systemPrompt,
customSystemPrompt: opts.systemPrompt ?? BUILD_PROMPT,
});

const memoryBlock = state.memorySnapshot;
Expand Down Expand Up @@ -570,51 +554,84 @@ export const AgentLayer = Layer.effect(
);
yield* context.absorb(sessionRef, [assistantToolEv]);

const approvedCalls: any[] = [];
const deniedResults: any[] = [];
for (const tc of toolCalls) {
const decision = yield* approval.evaluate({
tool: tc.name,
input: tc.arguments ?? {},
callId: tc.id,
const preparedBatch = yield* executor.prepareBatch(
toolCalls,
state.sessionId,
{
turnId: state.currentTurnId,
projectPath,
signal: abortSignal,
permissionMode,
sandboxMode: opts.sandboxMode,
parentPolicy: opts.sandboxPolicy,
model,
parentChain,
agentName: state.agentName,
},
toolLookup
);
const escalationDecisions = yield* approval.evaluateEscalationBatch(
preparedBatch.escalations.map(({ request }) => request)
);
const escalatedCalls = preparedBatch.escalations
.filter((_, index) => escalationDecisions[index]?.type === 'allow')
.map(({ prepared }) => prepared);
const decisions = yield* approval.evaluateBatch(
preparedBatch.prepared.map((prepared) => ({
tool: prepared.call.name,
input: prepared.parsedArgs as Record<string, unknown>,
prepared,
callId: prepared.call.id,
sessionId: state.sessionId,
projectPath,
permissionMode,
profile: profile?.name,
});
if (decision.type === 'deny') {
deniedResults.push({
status: 'denied',
id: tc.id,
name: tc.name,
reason: decision.reason,
});
} else {
approvedCalls.push(tc);
}
}))
);
const approvedCalls = [
...preparedBatch.prepared.filter((_, index) => decisions[index]?.type === 'allow'),
...escalatedCalls,
];
const deniedResults: ToolResult[] = [
...preparedBatch.denied,
...preparedBatch.prepared.flatMap((prepared, index) => {
const decision = decisions[index];
return decision?.type === 'deny'
? [{ status: 'denied' as const, id: prepared.call.id, name: prepared.call.name, reason: decision.reason }]
: [];
}),
...preparedBatch.escalations.flatMap(({ prepared }, index) => {
const decision = escalationDecisions[index];
return decision?.type === 'deny'
? [{
status: 'denied' as const,
id: prepared.call.id,
name: prepared.call.name,
reason: decision.reason,
}]
: [];
}),
];

if (
approvedCalls.length > 0 &&
!state.parentSessionId
) {
yield* checkpoint.ensureBaseline(state.cwd, sid, state.currentTurnId);
}

const approvedResults =
approvedCalls.length > 0
? yield* executor.executeBatch(
approvedCalls,
state.sessionId,
{
turnId: state.currentTurnId,
projectPath,
signal: abortSignal,
activeProfile: profile?.name,
model,
parentChain,
agentName: state.agentName,
onBeforeMutation: checkpoint.snapshotBeforeWrite(
state.cwd,
sid,
state.currentTurnId
),
},
toolLookup
)
? yield* executor.executeBatch(approvedCalls, state.sessionId, {
turnId: state.currentTurnId,
projectPath,
signal: abortSignal,
permissionMode,
sandboxMode: opts.sandboxMode,
parentPolicy: opts.sandboxPolicy,
model,
parentChain,
agentName: state.agentName,
})
: [];

const resultsById = new Map<string, any>();
Expand Down
7 changes: 5 additions & 2 deletions packages/codingcode/src/agent/port.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Context } from 'effect';
import type { Effect } from 'effect';
import type { FrameBody } from '../sink/types.js';
import type { ProfileName, PermissionMode } from '../util/enums.js';
import type { PermissionMode } from '../util/enums.js';
import type { SandboxMode } from '../sandbox/types.js';
import type { SandboxPolicy } from '../sandbox/types.js';
import type { IncomingPart } from '../llm/types.js';

import type { AgentError } from '../util/error.js';
Expand All @@ -11,8 +13,9 @@ export interface RunTurnOptions {
cwd: string;
signal?: AbortSignal;
permissionMode?: PermissionMode;
sandboxMode?: SandboxMode;
sandboxPolicy?: SandboxPolicy;
model: string;
activeProfile?: ProfileName;
parentSessionId?: string;
agentName?: string;
/** 子智能体用途描述;仅新建会话时写入 meta */
Expand Down
Loading
Loading