@moxxy/plugin-provider-claude-code 0.43.0 → 0.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/account-limit.d.ts +8 -0
- package/dist/account-limit.d.ts.map +1 -0
- package/dist/account-limit.js +23 -0
- package/dist/account-limit.js.map +1 -0
- package/dist/agent-provider.d.ts +61 -0
- package/dist/agent-provider.d.ts.map +1 -0
- package/dist/agent-provider.js +195 -0
- package/dist/agent-provider.js.map +1 -0
- package/dist/agent-session.d.ts +145 -0
- package/dist/agent-session.d.ts.map +1 -0
- package/dist/agent-session.js +700 -0
- package/dist/agent-session.js.map +1 -0
- package/dist/background-work.d.ts +42 -0
- package/dist/background-work.d.ts.map +1 -0
- package/dist/background-work.js +140 -0
- package/dist/background-work.js.map +1 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +2 -2
- package/dist/constants.js.map +1 -1
- package/dist/executable.d.ts +15 -0
- package/dist/executable.d.ts.map +1 -0
- package/dist/executable.js +30 -0
- package/dist/executable.js.map +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -16
- package/dist/index.js.map +1 -1
- package/dist/login.d.ts +3 -0
- package/dist/login.d.ts.map +1 -1
- package/dist/login.js +8 -1
- package/dist/login.js.map +1 -1
- package/dist/offered-models.d.ts +13 -0
- package/dist/offered-models.d.ts.map +1 -0
- package/dist/offered-models.js +45 -0
- package/dist/offered-models.js.map +1 -0
- package/dist/own-settings.d.ts +12 -0
- package/dist/own-settings.d.ts.map +1 -0
- package/dist/own-settings.js +31 -0
- package/dist/own-settings.js.map +1 -0
- package/dist/questions.d.ts +6 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +38 -0
- package/dist/questions.js.map +1 -0
- package/dist/scheduled-wakeups.d.ts +10 -0
- package/dist/scheduled-wakeups.d.ts.map +1 -0
- package/dist/scheduled-wakeups.js +30 -0
- package/dist/scheduled-wakeups.js.map +1 -0
- package/dist/sdk-install.d.ts +25 -0
- package/dist/sdk-install.d.ts.map +1 -0
- package/dist/sdk-install.js +94 -0
- package/dist/sdk-install.js.map +1 -0
- package/dist/sdk-types.d.ts +310 -0
- package/dist/sdk-types.d.ts.map +1 -0
- package/dist/sdk-types.js +8 -0
- package/dist/sdk-types.js.map +1 -0
- package/dist/subagents.d.ts +39 -0
- package/dist/subagents.d.ts.map +1 -0
- package/dist/subagents.js +114 -0
- package/dist/subagents.js.map +1 -0
- package/dist/task-list.d.ts +21 -0
- package/dist/task-list.d.ts.map +1 -0
- package/dist/task-list.js +68 -0
- package/dist/task-list.js.map +1 -0
- package/dist/translate.d.ts +57 -0
- package/dist/translate.d.ts.map +1 -0
- package/dist/translate.js +276 -0
- package/dist/translate.js.map +1 -0
- package/package.json +14 -9
- package/src/account-limit.ts +26 -0
- package/src/agent-cli.test.ts +83 -0
- package/src/agent-provider.test.ts +1156 -0
- package/src/agent-provider.ts +223 -0
- package/src/agent-session.ts +790 -0
- package/src/background-work.ts +163 -0
- package/src/constants.ts +2 -3
- package/src/event-coverage.test.ts +105 -0
- package/src/executable.test.ts +60 -0
- package/src/executable.ts +34 -0
- package/src/fake-claude.testkit.ts +376 -0
- package/src/fixtures/claude-agent-sdk-0.3.296.json +43 -0
- package/src/fixtures/claude-supported-models-0.3.296.json +218 -0
- package/src/index.test.ts +49 -0
- package/src/index.ts +61 -18
- package/src/login.ts +10 -1
- package/src/offered-models.test.ts +73 -0
- package/src/offered-models.ts +57 -0
- package/src/own-settings.test.ts +68 -0
- package/src/own-settings.ts +33 -0
- package/src/questions.test.ts +68 -0
- package/src/questions.ts +45 -0
- package/src/scheduled-wakeups.test.ts +41 -0
- package/src/scheduled-wakeups.ts +34 -0
- package/src/sdk-install.test.ts +124 -0
- package/src/sdk-install.ts +107 -0
- package/src/sdk-types.ts +277 -0
- package/src/subagents.ts +123 -0
- package/src/task-list.ts +65 -0
- package/src/translate.test.ts +439 -0
- package/src/translate.ts +280 -0
- package/dist/process.d.ts +0 -34
- package/dist/process.d.ts.map +0 -1
- package/dist/process.js +0 -197
- package/dist/process.js.map +0 -1
- package/dist/protocol.d.ts +0 -30
- package/dist/protocol.d.ts.map +0 -1
- package/dist/protocol.js +0 -368
- package/dist/protocol.js.map +0 -1
- package/dist/provider.d.ts +0 -53
- package/dist/provider.d.ts.map +0 -1
- package/dist/provider.js +0 -229
- package/dist/provider.js.map +0 -1
- package/src/process.test.ts +0 -208
- package/src/process.ts +0 -224
- package/src/protocol.ts +0 -390
- package/src/provider.test.ts +0 -671
- package/src/provider.ts +0 -267
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AgentAccountLimit } from '@moxxy/sdk';
|
|
2
|
+
import type { ClaudeRateLimitInfo } from './sdk-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Claude's plan limit as Moxxy's account limit. A used-up window Claude goes on past with extra
|
|
5
|
+
* usage is near, not reached: the person can still work.
|
|
6
|
+
*/
|
|
7
|
+
export declare function accountLimitFrom(info: ClaudeRateLimitInfo): AgentAccountLimit;
|
|
8
|
+
//# sourceMappingURL=account-limit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-limit.d.ts","sourceRoot":"","sources":["../src/account-limit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAU1D;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,mBAAmB,GAAG,iBAAiB,CAU7E"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const WINDOW_MINUTES = {
|
|
2
|
+
five_hour: 300,
|
|
3
|
+
seven_day: 10080,
|
|
4
|
+
seven_day_opus: 10080,
|
|
5
|
+
seven_day_sonnet: 10080,
|
|
6
|
+
seven_day_overage_included: 10080,
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Claude's plan limit as Moxxy's account limit. A used-up window Claude goes on past with extra
|
|
10
|
+
* usage is near, not reached: the person can still work.
|
|
11
|
+
*/
|
|
12
|
+
export function accountLimitFrom(info) {
|
|
13
|
+
const extraUsage = info.isUsingOverage === true || info.overageStatus === 'allowed' || info.overageStatus === 'allowed_warning';
|
|
14
|
+
const state = info.status === 'allowed' ? 'ok' : info.status === 'rejected' && !extraUsage ? 'reached' : 'near';
|
|
15
|
+
const windowMinutes = info.rateLimitType ? WINDOW_MINUTES[info.rateLimitType] : undefined;
|
|
16
|
+
return {
|
|
17
|
+
state,
|
|
18
|
+
...(info.resetsAt !== undefined ? { resetsAt: info.resetsAt * 1000 } : {}),
|
|
19
|
+
...(windowMinutes !== undefined ? { windowMinutes } : {}),
|
|
20
|
+
...(info.utilization !== undefined ? { usedPercent: Math.round(info.utilization * 100) } : {}),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=account-limit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-limit.js","sourceRoot":"","sources":["../src/account-limit.ts"],"names":[],"mappings":"AAGA,MAAM,cAAc,GAAqC;IACvD,SAAS,EAAE,GAAG;IACd,SAAS,EAAE,KAAK;IAChB,cAAc,EAAE,KAAK;IACrB,gBAAgB,EAAE,KAAK;IACvB,0BAA0B,EAAE,KAAK;CAClC,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAyB;IACxD,MAAM,UAAU,GAAG,IAAI,CAAC,cAAc,KAAK,IAAI,IAAI,IAAI,CAAC,aAAa,KAAK,SAAS,IAAI,IAAI,CAAC,aAAa,KAAK,iBAAiB,CAAC;IAChI,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,UAAU,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC;IAChH,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,cAAc,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC1F,OAAO;QACL,KAAK;QACL,GAAG,CAAC,IAAI,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC1E,GAAG,CAAC,aAAa,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACzD,GAAG,CAAC,IAAI,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC/F,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type AgentStepEvent, type AgentStepRequest, type AgentActivity, type AgentTurnStart, type LLMProvider, type ModelDescriptor, type ProviderEvent, type ProviderRequest } from '@moxxy/sdk';
|
|
2
|
+
import { AgentSessionPool } from '@moxxy/agent-connection-kit';
|
|
3
|
+
import { ClaudeAgentSession } from './agent-session.js';
|
|
4
|
+
import type { ClaudeAgentSdk } from './sdk-types.js';
|
|
5
|
+
export declare const CLAUDE_CODE_DEFAULT_MODEL = "claude-sonnet-5-5";
|
|
6
|
+
/** What a new conversation starts on while the person chose no default: light, so it spares their plan. */
|
|
7
|
+
export declare const CLAUDE_CODE_STARTER_MODEL = "claude-haiku-5-5";
|
|
8
|
+
/** Models Claude Code offers on a Claude subscription, used until Claude's own `supportedModels()` answered; Claude Code decides what the account may use. */
|
|
9
|
+
export declare const claudeCodeModels: ReadonlyArray<ModelDescriptor>;
|
|
10
|
+
export interface ClaudeAgentProviderConfig {
|
|
11
|
+
/** The person's Claude Code CLI; resolved from PATH when left out. */
|
|
12
|
+
readonly executable?: string;
|
|
13
|
+
/** Persisted provider-item model (`plugins.provider.items.claude-code.model`). */
|
|
14
|
+
readonly model?: string;
|
|
15
|
+
readonly defaultModel?: string;
|
|
16
|
+
/** Runner workspace, supplied on activation. */
|
|
17
|
+
readonly cwd?: string;
|
|
18
|
+
/** Where the Agent SDK comes from; installs it into `~/.moxxy` by default. */
|
|
19
|
+
readonly loadSdk?: () => Promise<ClaudeAgentSdk>;
|
|
20
|
+
/** The live Claude sessions; shared across the process by default. */
|
|
21
|
+
readonly sessions?: AgentSessionPool<ClaudeAgentSession>;
|
|
22
|
+
}
|
|
23
|
+
/** The live Claude sessions of this process, one per Moxxy conversation. */
|
|
24
|
+
export declare function claudeSessions(): AgentSessionPool<ClaudeAgentSession>;
|
|
25
|
+
/**
|
|
26
|
+
* The Claude agent connection: Claude Code does each turn itself through the
|
|
27
|
+
* Claude Agent SDK, unmodified (its preset, its settings, tools, skills and
|
|
28
|
+
* MCP servers), on the person's Claude sign-in. Moxxy adds only its own MCP
|
|
29
|
+
* server beside Claude's, and nothing to the prompt.
|
|
30
|
+
*/
|
|
31
|
+
export declare class ClaudeAgentProvider implements LLMProvider {
|
|
32
|
+
private readonly config;
|
|
33
|
+
readonly name = "claude-code";
|
|
34
|
+
readonly agentName = "Claude";
|
|
35
|
+
/** Its own plan mode and goal: Moxxy shows the plan and ends the goal when the agent says so. */
|
|
36
|
+
readonly agentOwns: {
|
|
37
|
+
readonly plan: true;
|
|
38
|
+
readonly goal: true;
|
|
39
|
+
};
|
|
40
|
+
readonly models: readonly ModelDescriptor[];
|
|
41
|
+
readonly starterModel = "claude-haiku-5-5";
|
|
42
|
+
private readonly defaultModel;
|
|
43
|
+
private readonly loadSdk;
|
|
44
|
+
constructor(config?: ClaudeAgentProviderConfig);
|
|
45
|
+
agentStep(req: AgentStepRequest): AsyncIterable<AgentStepEvent>;
|
|
46
|
+
/** Claude takes up a turn by itself once a task it ran in the background finished. */
|
|
47
|
+
onAgentTurn(listener: (start: AgentTurnStart) => void): () => void;
|
|
48
|
+
/** What Claude's background work does between turns, recorded at once. */
|
|
49
|
+
onAgentActivity(listener: (activity: AgentActivity) => void): () => void;
|
|
50
|
+
/** Stop one work Claude runs in the background in the conversation. */
|
|
51
|
+
stopBackgroundWork(sessionId: string, workId: string): Promise<boolean>;
|
|
52
|
+
private sessions;
|
|
53
|
+
/** One-off text (summaries, skill drafts): Claude with no tools and no MCP servers, keeping no session. */
|
|
54
|
+
stream(req: ProviderRequest): AsyncIterable<ProviderEvent>;
|
|
55
|
+
countTokens(req: Pick<ProviderRequest, 'model' | 'messages' | 'system' | 'tools'>): Promise<number>;
|
|
56
|
+
/** The conversation's running Claude, or a new one (resuming Claude's own session) when there is none that fits. */
|
|
57
|
+
private lease;
|
|
58
|
+
private settingsFor;
|
|
59
|
+
}
|
|
60
|
+
export declare function createClaudeCodeClient(config?: ClaudeAgentProviderConfig): LLMProvider;
|
|
61
|
+
//# sourceMappingURL=agent-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-provider.d.ts","sourceRoot":"","sources":["../src/agent-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,eAAe,EAErB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,gBAAgB,EAAsD,MAAM,6BAA6B,CAAC;AAEnH,OAAO,EAAE,kBAAkB,EAAiE,MAAM,oBAAoB,CAAC;AAGvH,OAAO,KAAK,EAAE,cAAc,EAAsC,MAAM,gBAAgB,CAAC;AAGzF,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,2GAA2G;AAC3G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e,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI;IAIxE,uEAAuE;IACjE,kBAAkB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAK7E,OAAO,CAAC,QAAQ;IAIhB,2GAA2G;IACpG,MAAM,CAAC,GAAG,EAAE,eAAe,GAAG,aAAa,CAAC,aAAa,CAAC;IA0C3D,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,OAAO,GAAG,UAAU,GAAG,QAAQ,GAAG,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IAazG,oHAAoH;YACtG,KAAK;IAanB,OAAO,CAAC,WAAW;CAiBpB;AAED,wBAAgB,sBAAsB,CAAC,MAAM,GAAE,yBAA8B,GAAG,WAAW,CAE1F"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { estimateTextTokens, } from '@moxxy/sdk';
|
|
2
|
+
import { AgentSessionPool, agentModeFor, oneOffPrompt } from '@moxxy/agent-connection-kit';
|
|
3
|
+
import { CLAUDE_AGENT_NAME, CLAUDE_CODE_PROVIDER_ID } from './constants.js';
|
|
4
|
+
import { ClaudeAgentSession, claudeEnv, startingPermissionMode } from './agent-session.js';
|
|
5
|
+
import { ownPermissionMode } from './own-settings.js';
|
|
6
|
+
import { ensureClaudeAgentSdk } from './sdk-install.js';
|
|
7
|
+
import { toTokenUsage } from './translate.js';
|
|
8
|
+
export const CLAUDE_CODE_DEFAULT_MODEL = 'claude-sonnet-5-5';
|
|
9
|
+
/** What a new conversation starts on while the person chose no default: light, so it spares their plan. */
|
|
10
|
+
export const CLAUDE_CODE_STARTER_MODEL = 'claude-haiku-5-5';
|
|
11
|
+
const NON_TEXT_BLOCK_TOKENS = 256;
|
|
12
|
+
const MAX_LIVE_CLAUDE_SESSIONS = 8;
|
|
13
|
+
const agentCapabilities = {
|
|
14
|
+
supportsTools: true,
|
|
15
|
+
supportsStreaming: true,
|
|
16
|
+
supportsImages: true,
|
|
17
|
+
supportsDocuments: true,
|
|
18
|
+
supportsAudio: false,
|
|
19
|
+
supportsReasoning: true,
|
|
20
|
+
hostedTools: ['web_search'],
|
|
21
|
+
};
|
|
22
|
+
/** Models Claude Code offers on a Claude subscription, used until Claude's own `supportedModels()` answered; Claude Code decides what the account may use. */
|
|
23
|
+
export const claudeCodeModels = [
|
|
24
|
+
{ id: 'claude-fable-5-1', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...agentCapabilities },
|
|
25
|
+
{ id: 'claude-opus-5-5', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...agentCapabilities },
|
|
26
|
+
{ id: CLAUDE_CODE_DEFAULT_MODEL, contextWindow: 1_000_000, maxOutputTokens: 128_000, ...agentCapabilities },
|
|
27
|
+
{ id: 'claude-haiku-5-5', contextWindow: 1_000_000, maxOutputTokens: 128_000, ...agentCapabilities },
|
|
28
|
+
];
|
|
29
|
+
let sharedSessions;
|
|
30
|
+
/** The live Claude sessions of this process, one per Moxxy conversation. */
|
|
31
|
+
export function claudeSessions() {
|
|
32
|
+
sharedSessions ??= new AgentSessionPool({ limit: MAX_LIVE_CLAUDE_SESSIONS });
|
|
33
|
+
return sharedSessions;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The Claude agent connection: Claude Code does each turn itself through the
|
|
37
|
+
* Claude Agent SDK, unmodified (its preset, its settings, tools, skills and
|
|
38
|
+
* MCP servers), on the person's Claude sign-in. Moxxy adds only its own MCP
|
|
39
|
+
* server beside Claude's, and nothing to the prompt.
|
|
40
|
+
*/
|
|
41
|
+
export class ClaudeAgentProvider {
|
|
42
|
+
config;
|
|
43
|
+
name = CLAUDE_CODE_PROVIDER_ID;
|
|
44
|
+
agentName = CLAUDE_AGENT_NAME;
|
|
45
|
+
/** Its own plan mode and goal: Moxxy shows the plan and ends the goal when the agent says so. */
|
|
46
|
+
agentOwns = { plan: true, goal: true };
|
|
47
|
+
models = claudeCodeModels;
|
|
48
|
+
starterModel = CLAUDE_CODE_STARTER_MODEL;
|
|
49
|
+
defaultModel;
|
|
50
|
+
loadSdk;
|
|
51
|
+
constructor(config = {}) {
|
|
52
|
+
this.config = config;
|
|
53
|
+
this.defaultModel = config.model ?? config.defaultModel ?? CLAUDE_CODE_DEFAULT_MODEL;
|
|
54
|
+
this.loadSdk = config.loadSdk ?? (() => ensureClaudeAgentSdk());
|
|
55
|
+
}
|
|
56
|
+
async *agentStep(req) {
|
|
57
|
+
let lease;
|
|
58
|
+
try {
|
|
59
|
+
lease = await this.lease(req);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
if (!req.signal.aborted)
|
|
63
|
+
yield { type: 'error', message: messageOf(err), retryable: false };
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
yield* lease.session.turn(req);
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
if (lease.session.isAlive)
|
|
71
|
+
lease.release();
|
|
72
|
+
else
|
|
73
|
+
await lease.discard();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Claude takes up a turn by itself once a task it ran in the background finished. */
|
|
77
|
+
onAgentTurn(listener) {
|
|
78
|
+
return this.sessions().onAgentTurn(listener);
|
|
79
|
+
}
|
|
80
|
+
/** What Claude's background work does between turns, recorded at once. */
|
|
81
|
+
onAgentActivity(listener) {
|
|
82
|
+
return this.sessions().onActivity(listener);
|
|
83
|
+
}
|
|
84
|
+
/** Stop one work Claude runs in the background in the conversation. */
|
|
85
|
+
async stopBackgroundWork(sessionId, workId) {
|
|
86
|
+
const live = this.sessions().running(sessionId);
|
|
87
|
+
return live ? live.stopWork(workId) : false;
|
|
88
|
+
}
|
|
89
|
+
sessions() {
|
|
90
|
+
return this.config.sessions ?? claudeSessions();
|
|
91
|
+
}
|
|
92
|
+
/** One-off text (summaries, skill drafts): Claude with no tools and no MCP servers, keeping no session. */
|
|
93
|
+
async *stream(req) {
|
|
94
|
+
const model = req.model || this.defaultModel;
|
|
95
|
+
yield { type: 'message_start', model };
|
|
96
|
+
let usage;
|
|
97
|
+
try {
|
|
98
|
+
const sdk = await this.loadSdk();
|
|
99
|
+
const { system, prompt } = oneOffPrompt(req);
|
|
100
|
+
const query = sdk.query({
|
|
101
|
+
prompt,
|
|
102
|
+
options: {
|
|
103
|
+
cwd: this.config.cwd ?? process.cwd(),
|
|
104
|
+
model,
|
|
105
|
+
systemPrompt: system ?? '',
|
|
106
|
+
...(this.config.executable ? { pathToClaudeCodeExecutable: this.config.executable } : {}),
|
|
107
|
+
tools: [],
|
|
108
|
+
strictMcpConfig: true,
|
|
109
|
+
persistSession: false,
|
|
110
|
+
maxTurns: 1,
|
|
111
|
+
env: claudeEnv(),
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
req.signal?.addEventListener('abort', () => query.close(), { once: true });
|
|
115
|
+
for await (const message of query) {
|
|
116
|
+
if (message.type === 'assistant' && 'message' in message) {
|
|
117
|
+
for (const block of message.message.content) {
|
|
118
|
+
if (block.type === 'text' && 'text' in block && typeof block.text === 'string')
|
|
119
|
+
yield { type: 'text_delta', delta: block.text };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if (message.type === 'result' && 'is_error' in message) {
|
|
123
|
+
if (message.usage)
|
|
124
|
+
usage = toTokenUsage(message.usage);
|
|
125
|
+
if (message.is_error) {
|
|
126
|
+
yield { type: 'error', message: `Claude Code: ${message.errors?.join('; ') ?? message.result ?? message.subtype}`, retryable: false };
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
if (!req.signal?.aborted)
|
|
134
|
+
yield { type: 'error', message: messageOf(err), retryable: false };
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
yield { type: 'message_end', stopReason: 'end_turn', ...(usage ? { usage } : {}) };
|
|
138
|
+
}
|
|
139
|
+
async countTokens(req) {
|
|
140
|
+
let text = req.system ?? '';
|
|
141
|
+
let nonTextBlocks = 0;
|
|
142
|
+
for (const message of req.messages) {
|
|
143
|
+
for (const block of message.content) {
|
|
144
|
+
if (block.type === 'text')
|
|
145
|
+
text += block.text;
|
|
146
|
+
else
|
|
147
|
+
nonTextBlocks += 1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
for (const tool of req.tools ?? [])
|
|
151
|
+
text += tool.name + tool.description;
|
|
152
|
+
return estimateTextTokens(text) + nonTextBlocks * NON_TEXT_BLOCK_TOKENS;
|
|
153
|
+
}
|
|
154
|
+
/** The conversation's running Claude, or a new one (resuming Claude's own session) when there is none that fits. */
|
|
155
|
+
async lease(req) {
|
|
156
|
+
const sdk = await this.loadSdk();
|
|
157
|
+
// The person's own Claude is asked only when their recorded switch is off: then a default that acts without asking must not win.
|
|
158
|
+
const ownMode = req.autoApproveRecorded && !req.autoApprove ? await ownPermissionMode(sdk, req.cwd) : undefined;
|
|
159
|
+
const settings = this.settingsFor(req, sdk, ownMode);
|
|
160
|
+
const sessions = this.sessions();
|
|
161
|
+
const create = () => ClaudeAgentSession.start(settings);
|
|
162
|
+
const lease = await sessions.acquire(req.sessionId, { signal: req.signal, create });
|
|
163
|
+
if (lease.session.isAlive && lease.session.fits(settings))
|
|
164
|
+
return lease;
|
|
165
|
+
await lease.discard();
|
|
166
|
+
return sessions.acquire(req.sessionId, { signal: req.signal, create });
|
|
167
|
+
}
|
|
168
|
+
settingsFor(req, sdk, ownMode) {
|
|
169
|
+
const effort = effortOf(req.reasoning);
|
|
170
|
+
const permissionMode = startingPermissionMode(agentModeFor(req.mode), req.autoApprove, ownMode);
|
|
171
|
+
return {
|
|
172
|
+
sdk,
|
|
173
|
+
cwd: req.cwd,
|
|
174
|
+
model: req.model || this.defaultModel,
|
|
175
|
+
...(effort ? { effort } : {}),
|
|
176
|
+
...(this.config.executable ? { executable: this.config.executable } : {}),
|
|
177
|
+
...(req.resumeSessionId ? { resume: req.resumeSessionId } : {}),
|
|
178
|
+
...(permissionMode ? { permissionMode } : {}),
|
|
179
|
+
tools: req.allTools ?? req.tools,
|
|
180
|
+
...(req.skills ? { skills: req.skills } : {}),
|
|
181
|
+
onAgentTurn: (name, workId) => this.sessions().agentTurn({ sessionId: req.sessionId, name, ...(workId ? { workId } : {}) }),
|
|
182
|
+
onActivity: (events) => this.sessions().activity({ sessionId: req.sessionId, events }),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
export function createClaudeCodeClient(config = {}) {
|
|
187
|
+
return new ClaudeAgentProvider(config);
|
|
188
|
+
}
|
|
189
|
+
function effortOf(reasoning) {
|
|
190
|
+
return typeof reasoning === 'object' && reasoning.effort ? reasoning.effort : undefined;
|
|
191
|
+
}
|
|
192
|
+
function messageOf(err) {
|
|
193
|
+
return err instanceof Error ? err.message : String(err);
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=agent-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-provider.js","sourceRoot":"","sources":["../src/agent-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,GAUnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,YAAY,EAA0B,MAAM,6BAA6B,CAAC;AACnH,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAC5E,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,sBAAsB,EAA8B,MAAM,oBAAoB,CAAC;AACvH,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAExD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,CAAC,MAAM,yBAAyB,GAAG,mBAAmB,CAAC;AAC7D,2GAA2G;A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GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { AgentStepEvent, AgentStepRequest, Skill, ToolDef } from '@moxxy/sdk';
|
|
2
|
+
import { type AgentMode, type LiveSession } from '@moxxy/agent-connection-kit';
|
|
3
|
+
import type { ClaudeAgentSdk, ClaudeEffort, ClaudePermissionMode } from './sdk-types.js';
|
|
4
|
+
export interface ClaudeSessionSettings {
|
|
5
|
+
readonly sdk: ClaudeAgentSdk;
|
|
6
|
+
readonly cwd: string;
|
|
7
|
+
readonly model: string;
|
|
8
|
+
readonly effort?: ClaudeEffort;
|
|
9
|
+
/** The person's own Claude Code CLI. */
|
|
10
|
+
readonly executable?: string;
|
|
11
|
+
readonly resume?: string;
|
|
12
|
+
readonly permissionMode?: ClaudePermissionMode;
|
|
13
|
+
/** Every Moxxy tool of the conversation: Claude reads the list as it starts, and again on a notice. */
|
|
14
|
+
readonly tools?: ReadonlyArray<ToolDef>;
|
|
15
|
+
/** The conversation's Moxxy skills: Claude reads those meant for it through one more Moxxy tool. */
|
|
16
|
+
readonly skills?: ReadonlyArray<Skill>;
|
|
17
|
+
/** Claude took up a turn by itself while no Moxxy turn runs; `name` says what for, `workId` which background work ended. */
|
|
18
|
+
readonly onAgentTurn?: (name: string, workId?: string) => void;
|
|
19
|
+
/** What Claude's background work did, for the conversation to record at once: its list, a command's end, a helper's steps between turns. */
|
|
20
|
+
readonly onActivity?: (events: ReadonlyArray<AgentStepEvent>) => void;
|
|
21
|
+
}
|
|
22
|
+
export declare function toolsLentToClaude(tools: ReadonlyArray<ToolDef>): ToolDef[];
|
|
23
|
+
/** Modes in which Claude acts without asking the person: `auto` lets its own classifier decide. */
|
|
24
|
+
export declare function runsWithoutAsking(mode: ClaudePermissionMode | undefined): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* The permission mode a new Claude starts in; `undefined` keeps the mode from
|
|
27
|
+
* the person's Claude settings. `ownMode` is that mode when the conversation
|
|
28
|
+
* recorded auto-approve off: one that acts without asking is set to ask, so the
|
|
29
|
+
* person's switch wins over their own default.
|
|
30
|
+
*/
|
|
31
|
+
export declare function startingPermissionMode(mode: AgentMode, autoApprove: boolean, ownMode?: ClaudePermissionMode): ClaudePermissionMode | undefined;
|
|
32
|
+
/** The plan file a Write or Edit of Claude's touches, if it is one. */
|
|
33
|
+
export declare function claudePlanFileOf(name: string, input: unknown): string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* One running Claude Code (one Agent SDK `query()`) for one Moxxy
|
|
36
|
+
* conversation. Every message the person sends is the next user message of
|
|
37
|
+
* the same query, so Claude keeps its context, tools and goal between turns.
|
|
38
|
+
* Everything Claude asks of Moxxy (approvals, the PreToolUse rule check,
|
|
39
|
+
* Moxxy tools over MCP) is queued with its messages and handled in order, so
|
|
40
|
+
* the log shows a call before the decision about it.
|
|
41
|
+
*/
|
|
42
|
+
export declare class ClaudeAgentSession implements LiveSession {
|
|
43
|
+
readonly settings: ClaudeSessionSettings;
|
|
44
|
+
private readonly mcp;
|
|
45
|
+
/** Claude's session is new while the conversation may already have run: its first message carries the conversation so far. */
|
|
46
|
+
private newSession;
|
|
47
|
+
private readonly input;
|
|
48
|
+
private readonly inbox;
|
|
49
|
+
private readonly query;
|
|
50
|
+
private alive;
|
|
51
|
+
private closed;
|
|
52
|
+
private inTurn;
|
|
53
|
+
/** This turn handed Claude's plan to the plan card through ExitPlanMode. */
|
|
54
|
+
private planHandedOver;
|
|
55
|
+
/** A turn ended before Claude's `result` (its reader stopped early); the next turn waits it out. */
|
|
56
|
+
private owesResult;
|
|
57
|
+
/** Claude took up a turn by itself and no Moxxy turn has read it yet. */
|
|
58
|
+
private agentTurnPending;
|
|
59
|
+
/** When Claude set itself to come back, as its last `Stop` said. */
|
|
60
|
+
private wakeups;
|
|
61
|
+
/** Claude's tasks by id, named as they started, to say which one finished. */
|
|
62
|
+
private readonly taskNames;
|
|
63
|
+
/** The task whose end Claude takes up next, once it finished in the background. */
|
|
64
|
+
private finishedTask;
|
|
65
|
+
/** That task as Moxxy's background work (`AgentBackgroundWork.id`). */
|
|
66
|
+
private finishedWork;
|
|
67
|
+
/** What Claude runs in the background, as its list says. */
|
|
68
|
+
private readonly background;
|
|
69
|
+
/** The last turn's host: Moxxy's rules for a call made while no turn runs (a helper in the background). */
|
|
70
|
+
private lastHost;
|
|
71
|
+
private permissionMode;
|
|
72
|
+
/** Claude's task list, which its tools change one task at a time across turns. */
|
|
73
|
+
private readonly tasks;
|
|
74
|
+
/** Claude's subagents; one in the background ends after the turn that started it. */
|
|
75
|
+
private readonly subagents;
|
|
76
|
+
/** Reads a background helper's messages while no turn runs; the next turn reads them again and shows nothing twice. */
|
|
77
|
+
private readonly between;
|
|
78
|
+
/** The conversation's auto-approve as this turn knows it: as it began, then as Claude's mode changed it. */
|
|
79
|
+
private switchedOn;
|
|
80
|
+
private model;
|
|
81
|
+
private goalActive;
|
|
82
|
+
private stderr;
|
|
83
|
+
/** The Moxxy tools Claude is shown: every one of the conversation, the same in every mode. */
|
|
84
|
+
private listed;
|
|
85
|
+
/** The Moxxy skills Claude is offered. */
|
|
86
|
+
private offered;
|
|
87
|
+
static start(settings: ClaudeSessionSettings): Promise<ClaudeAgentSession>;
|
|
88
|
+
private constructor();
|
|
89
|
+
get isAlive(): boolean;
|
|
90
|
+
/** Whether this Claude was started with what the turn needs (settings a running Claude cannot change). */
|
|
91
|
+
fits(settings: Pick<ClaudeSessionSettings, 'effort' | 'executable' | 'cwd'>): boolean;
|
|
92
|
+
turn(req: AgentStepRequest): AsyncGenerator<AgentStepEvent>;
|
|
93
|
+
/** Stop one work Claude runs in the background (`AgentBackgroundWork.id`); `false` when Claude runs no such work. */
|
|
94
|
+
stopWork(workId: string): Promise<boolean>;
|
|
95
|
+
close(): Promise<void>;
|
|
96
|
+
/** What Claude asked of Moxxy, answered in order with the turn's host. */
|
|
97
|
+
private handle;
|
|
98
|
+
private decide;
|
|
99
|
+
private answerQuestion;
|
|
100
|
+
/** Model, mode and goal as this turn wants them, changed on the running Claude. */
|
|
101
|
+
private align;
|
|
102
|
+
private setPermissionMode;
|
|
103
|
+
/** After a goal turn: ask `/goal` once whether the goal is still open. Its answer is not shown. */
|
|
104
|
+
private goalStatus;
|
|
105
|
+
/** Send a command and read through to its result without showing anything. */
|
|
106
|
+
/** Claude's own skills and commands, not its built-in ones: a Moxxy skill of the same name is left to Claude's. */
|
|
107
|
+
/** Claude's own MCP servers that are on: Moxxy's server of the same name is left to Claude's. */
|
|
108
|
+
private ownMcpServerNames;
|
|
109
|
+
private ownSkillNames;
|
|
110
|
+
private silently;
|
|
111
|
+
/** Wait out the result of a turn whose reader stopped early, so it does not leak into this one. */
|
|
112
|
+
private settlePrevious;
|
|
113
|
+
private send;
|
|
114
|
+
private userMessages;
|
|
115
|
+
private pump;
|
|
116
|
+
private onCanUseTool;
|
|
117
|
+
/** Claude ended a turn: what it set itself to come back for is said again, whenever it changed. */
|
|
118
|
+
private onStop;
|
|
119
|
+
private setWakeups;
|
|
120
|
+
private onPreToolUse;
|
|
121
|
+
/** A call made while no turn runs (a helper in the background) still meets Moxxy's rules. */
|
|
122
|
+
private checkRulesBetweenTurns;
|
|
123
|
+
/**
|
|
124
|
+
* Watch what Claude sends whether or not a turn runs: which task finished,
|
|
125
|
+
* and Claude taking up a turn by itself (its `init` while Moxxy is idle),
|
|
126
|
+
* which the conversation opens a turn for.
|
|
127
|
+
*/
|
|
128
|
+
private notice;
|
|
129
|
+
private report;
|
|
130
|
+
/**
|
|
131
|
+
* Answer whatever is still queued once no turn can: Claude must never wait on
|
|
132
|
+
* Moxxy forever. What is left of a turn whose reader stopped early is dropped,
|
|
133
|
+
* up to its result; what Claude sent after it is its own (a helper in the
|
|
134
|
+
* background, a turn it took up) and stays for the next reader.
|
|
135
|
+
*/
|
|
136
|
+
private dismissQueued;
|
|
137
|
+
private endedError;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The environment for Claude Code: Moxxy's own, without the markers that make
|
|
141
|
+
* Claude refuse to start as a nested session when Moxxy itself was launched
|
|
142
|
+
* from Claude Code. The SDK replaces the environment, so it is spread here.
|
|
143
|
+
*/
|
|
144
|
+
export declare function claudeEnv(): Record<string, string | undefined>;
|
|
145
|
+
//# sourceMappingURL=agent-session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-session.d.ts","sourceRoot":"","sources":["../src/agent-session.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAwB,cAAc,EAAiB,gBAAgB,EAAmC,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACzJ,OAAO,EAUL,KAAK,SAAS,EACd,KAAK,WAAW,EAKjB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EAGZ,oBAAoB,EAOrB,MAAM,gBAAgB,CAAC;AAexB,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,GAAG,EAAE,cAAc,CAAC;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;IAC/B,wCAAwC;IACxC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,cAAc,CAAC,EAAE,oBAAoB,CAAC;IAC/C,uGAAuG;IACvG,QAAQ,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IACxC,oGAAoG;IACpG,QAAQ,CAAC,MAAM,CAAC,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;IACvC,4HAA4H;IAC5H,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,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