@adhd/agent-mcp 2.0.1 → 2.1.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/AGENTS.md +236 -0
- package/CHANGELOG.md +115 -0
- package/LICENSE +21 -0
- package/README.md +201 -0
- package/docs/architecture-and-security.md +405 -0
- package/docs/marketing/.catalog/capabilities.md +143 -0
- package/docs/marketing/.catalog/consumer.md +111 -0
- package/docs/marketing/.catalog/distribution.md +223 -0
- package/docs/marketing/.catalog/doc-conformance.md +237 -0
- package/docs/marketing/.catalog/doc-ops.md +98 -0
- package/docs/marketing/.catalog/metrics.md +119 -0
- package/docs/marketing/.catalog/required-tooling.md +224 -0
- package/docs/marketing/.catalog/review.md +164 -0
- package/docs/provider-call-audit.md +254 -0
- package/llms.txt +133 -0
- package/package.json +30 -51
- package/src/__tests__/integration/harness.d.ts +871 -39
- package/src/__tests__/integration/harness.d.ts.map +1 -1
- package/src/__tests__/integration/harness.js +93 -87
- package/src/__tests__/integration/harness.js.map +1 -1
- package/src/__tests__/integration/scripted-provider.d.ts +1 -2
- package/src/__tests__/integration/scripted-provider.d.ts.map +1 -1
- package/src/__tests__/integration/scripted-provider.js +1 -3
- package/src/__tests__/integration/scripted-provider.js.map +1 -1
- package/src/config.d.ts +6 -69
- package/src/config.d.ts.map +1 -1
- package/src/config.js +1 -50
- package/src/config.js.map +1 -1
- package/src/db/client.d.ts +1516 -2
- package/src/db/client.d.ts.map +1 -1
- package/src/db/client.js +4 -2
- package/src/db/client.js.map +1 -1
- package/src/db/migrate-runner.d.ts +0 -28
- package/src/db/migrate-runner.d.ts.map +1 -1
- package/src/db/migrate-runner.js +0 -24
- package/src/db/migrate-runner.js.map +1 -1
- package/src/db/migrate.d.ts +0 -7
- package/src/db/migrate.d.ts.map +1 -1
- package/src/db/migrate.js +0 -7
- package/src/db/migrate.js.map +1 -1
- package/src/db/schema.d.ts +0 -1260
- package/src/db/schema.d.ts.map +1 -1
- package/src/db/schema.js +2 -179
- package/src/db/schema.js.map +1 -1
- package/src/index.d.ts +3 -42
- package/src/index.d.ts.map +1 -1
- package/src/index.js +32 -123
- package/src/index.js.map +1 -1
- package/src/logger.d.ts +0 -8
- package/src/logger.d.ts.map +1 -1
- package/src/logger.js +1 -12
- package/src/logger.js.map +1 -1
- package/src/scripts/agent-mcp-tail.d.ts +3 -0
- package/src/scripts/agent-mcp-tail.d.ts.map +1 -0
- package/src/scripts/agent-mcp-tail.js +227 -0
- package/src/scripts/agent-mcp-tail.js.map +1 -0
- package/src/server.d.ts +13 -34
- package/src/server.d.ts.map +1 -1
- package/src/server.js +234 -326
- package/src/server.js.map +1 -1
- package/src/store/agent-store.d.ts +3 -16
- package/src/store/agent-store.d.ts.map +1 -1
- package/src/store/agent-store.js +38 -41
- package/src/store/agent-store.js.map +1 -1
- package/src/streaming/chat-gateway.d.ts +17 -0
- package/src/streaming/chat-gateway.d.ts.map +1 -0
- package/src/streaming/chat-gateway.js +336 -0
- package/src/streaming/chat-gateway.js.map +1 -0
- package/src/streaming/event-bus.d.ts +4 -0
- package/src/streaming/event-bus.d.ts.map +1 -1
- package/src/streaming/event-bus.js +14 -1
- package/src/streaming/event-bus.js.map +1 -1
- package/src/streaming/sse-server.d.ts +3 -14
- package/src/streaming/sse-server.d.ts.map +1 -1
- package/src/streaming/sse-server.js +37 -37
- package/src/streaming/sse-server.js.map +1 -1
- package/src/utils/load-env.d.ts +0 -14
- package/src/utils/load-env.d.ts.map +1 -1
- package/src/utils/load-env.js +0 -18
- package/src/utils/load-env.js.map +1 -1
- package/drizzle/0000_nifty_sasquatch.sql +0 -55
- package/drizzle/0001_task_usage.sql +0 -16
- package/drizzle/0002_lumpy_silver_centurion.sql +0 -2
- package/drizzle/0003_nifty_shriek.sql +0 -2
- package/drizzle/0004_brief_shocker.sql +0 -4
- package/drizzle/0005_clear_lenny_balinger.sql +0 -25
- package/drizzle/0006_composed_prompts_cache.sql +0 -24
- package/drizzle/meta/0000_snapshot.json +0 -387
- package/drizzle/meta/0001_snapshot.json +0 -495
- package/drizzle/meta/0002_snapshot.json +0 -509
- package/drizzle/meta/0003_snapshot.json +0 -523
- package/drizzle/meta/0004_snapshot.json +0 -551
- package/drizzle/meta/0005_snapshot.json +0 -545
- package/drizzle/meta/0006_snapshot.json +0 -651
- package/drizzle/meta/_journal.json +0 -55
- package/src/clients/http-client.d.ts +0 -24
- package/src/clients/http-client.d.ts.map +0 -1
- package/src/clients/http-client.js +0 -98
- package/src/clients/http-client.js.map +0 -1
- package/src/clients/in-process.d.ts +0 -27
- package/src/clients/in-process.d.ts.map +0 -1
- package/src/clients/in-process.js +0 -37
- package/src/clients/in-process.js.map +0 -1
- package/src/clients/index.d.ts +0 -7
- package/src/clients/index.d.ts.map +0 -1
- package/src/clients/index.js +0 -5
- package/src/clients/index.js.map +0 -1
- package/src/clients/registry.d.ts +0 -54
- package/src/clients/registry.d.ts.map +0 -1
- package/src/clients/registry.js +0 -164
- package/src/clients/registry.js.map +0 -1
- package/src/clients/stdio-client.d.ts +0 -16
- package/src/clients/stdio-client.d.ts.map +0 -1
- package/src/clients/stdio-client.js +0 -97
- package/src/clients/stdio-client.js.map +0 -1
- package/src/clients/tool-naming.d.ts +0 -44
- package/src/clients/tool-naming.d.ts.map +0 -1
- package/src/clients/tool-naming.js +0 -65
- package/src/clients/tool-naming.js.map +0 -1
- package/src/clients/types.d.ts +0 -18
- package/src/clients/types.d.ts.map +0 -1
- package/src/clients/types.js +0 -2
- package/src/clients/types.js.map +0 -1
- package/src/engine/dag-engine.d.ts +0 -62
- package/src/engine/dag-engine.d.ts.map +0 -1
- package/src/engine/dag-engine.js +0 -158
- package/src/engine/dag-engine.js.map +0 -1
- package/src/engine/hooks.d.ts +0 -24
- package/src/engine/hooks.d.ts.map +0 -1
- package/src/engine/hooks.js +0 -50
- package/src/engine/hooks.js.map +0 -1
- package/src/engine/orchestrator.d.ts +0 -40
- package/src/engine/orchestrator.d.ts.map +0 -1
- package/src/engine/orchestrator.js +0 -507
- package/src/engine/orchestrator.js.map +0 -1
- package/src/engine/policy.d.ts +0 -66
- package/src/engine/policy.d.ts.map +0 -1
- package/src/engine/policy.js +0 -112
- package/src/engine/policy.js.map +0 -1
- package/src/engine/prompt-resolver.d.ts +0 -89
- package/src/engine/prompt-resolver.d.ts.map +0 -1
- package/src/engine/prompt-resolver.js +0 -96
- package/src/engine/prompt-resolver.js.map +0 -1
- package/src/engine/queue.d.ts +0 -29
- package/src/engine/queue.d.ts.map +0 -1
- package/src/engine/queue.js +0 -56
- package/src/engine/queue.js.map +0 -1
- package/src/plugins/index.d.ts +0 -2
- package/src/plugins/index.d.ts.map +0 -1
- package/src/plugins/index.js +0 -2
- package/src/plugins/index.js.map +0 -1
- package/src/plugins/loader.d.ts +0 -65
- package/src/plugins/loader.d.ts.map +0 -1
- package/src/plugins/loader.js +0 -222
- package/src/plugins/loader.js.map +0 -1
- package/src/plugins/usage-plugin.d.ts +0 -27
- package/src/plugins/usage-plugin.d.ts.map +0 -1
- package/src/plugins/usage-plugin.js +0 -148
- package/src/plugins/usage-plugin.js.map +0 -1
- package/src/providers/anthropic.d.ts +0 -13
- package/src/providers/anthropic.d.ts.map +0 -1
- package/src/providers/anthropic.js +0 -260
- package/src/providers/anthropic.js.map +0 -1
- package/src/providers/claudecli.d.ts +0 -153
- package/src/providers/claudecli.d.ts.map +0 -1
- package/src/providers/claudecli.js +0 -510
- package/src/providers/claudecli.js.map +0 -1
- package/src/providers/factory.d.ts +0 -10
- package/src/providers/factory.d.ts.map +0 -1
- package/src/providers/factory.js +0 -24
- package/src/providers/factory.js.map +0 -1
- package/src/providers/index.d.ts +0 -6
- package/src/providers/index.d.ts.map +0 -1
- package/src/providers/index.js +0 -5
- package/src/providers/index.js.map +0 -1
- package/src/providers/openai.d.ts +0 -14
- package/src/providers/openai.d.ts.map +0 -1
- package/src/providers/openai.js +0 -162
- package/src/providers/openai.js.map +0 -1
- package/src/providers/types.d.ts +0 -36
- package/src/providers/types.d.ts.map +0 -1
- package/src/providers/types.js +0 -2
- package/src/providers/types.js.map +0 -1
- package/src/store/composed-prompt-store.d.ts +0 -24
- package/src/store/composed-prompt-store.d.ts.map +0 -1
- package/src/store/composed-prompt-store.js +0 -75
- package/src/store/composed-prompt-store.js.map +0 -1
- package/src/store/index.d.ts +0 -5
- package/src/store/index.d.ts.map +0 -1
- package/src/store/index.js +0 -5
- package/src/store/index.js.map +0 -1
- package/src/store/session-store.d.ts +0 -45
- package/src/store/session-store.d.ts.map +0 -1
- package/src/store/session-store.js +0 -197
- package/src/store/session-store.js.map +0 -1
- package/src/store/task-store.d.ts +0 -55
- package/src/store/task-store.d.ts.map +0 -1
- package/src/store/task-store.js +0 -186
- package/src/store/task-store.js.map +0 -1
- package/src/tools/agent-crud.d.ts +0 -15
- package/src/tools/agent-crud.d.ts.map +0 -1
- package/src/tools/agent-crud.js +0 -32
- package/src/tools/agent-crud.js.map +0 -1
- package/src/tools/session.d.ts +0 -33
- package/src/tools/session.d.ts.map +0 -1
- package/src/tools/session.js +0 -73
- package/src/tools/session.js.map +0 -1
- package/src/tools/task.d.ts +0 -86
- package/src/tools/task.d.ts.map +0 -1
- package/src/tools/task.js +0 -406
- package/src/tools/task.js.map +0 -1
- package/src/tools/usage.d.ts +0 -60
- package/src/tools/usage.d.ts.map +0 -1
- package/src/tools/usage.js +0 -174
- package/src/tools/usage.js.map +0 -1
- package/src/types/agent.d.ts +0 -2
- package/src/types/agent.d.ts.map +0 -1
- package/src/types/agent.js +0 -3
- package/src/types/agent.js.map +0 -1
- package/src/types/execution.d.ts +0 -2
- package/src/types/execution.d.ts.map +0 -1
- package/src/types/execution.js +0 -3
- package/src/types/execution.js.map +0 -1
- package/src/types/index.d.ts +0 -2
- package/src/types/index.d.ts.map +0 -1
- package/src/types/index.js +0 -2
- package/src/types/index.js.map +0 -1
- package/src/types/mcp.d.ts +0 -2
- package/src/types/mcp.d.ts.map +0 -1
- package/src/types/mcp.js +0 -3
- package/src/types/mcp.js.map +0 -1
- package/src/types/message.d.ts +0 -2
- package/src/types/message.d.ts.map +0 -1
- package/src/types/message.js +0 -3
- package/src/types/message.js.map +0 -1
- package/src/types/session.d.ts +0 -2
- package/src/types/session.d.ts.map +0 -1
- package/src/types/session.js +0 -3
- package/src/types/session.js.map +0 -1
- package/src/types/task.d.ts +0 -2
- package/src/types/task.d.ts.map +0 -1
- package/src/types/task.js +0 -3
- package/src/types/task.js.map +0 -1
- package/src/utils/ids.d.ts +0 -2
- package/src/utils/ids.d.ts.map +0 -1
- package/src/utils/ids.js +0 -5
- package/src/utils/ids.js.map +0 -1
- package/src/utils/logger.d.ts +0 -2
- package/src/utils/logger.d.ts.map +0 -1
- package/src/utils/logger.js +0 -5
- package/src/utils/logger.js.map +0 -1
- package/src/utils/timestamps.d.ts +0 -2
- package/src/utils/timestamps.d.ts.map +0 -1
- package/src/utils/timestamps.js +0 -4
- package/src/utils/timestamps.js.map +0 -1
- package/src/validation/agent.d.ts +0 -400
- package/src/validation/agent.d.ts.map +0 -1
- package/src/validation/agent.js +0 -251
- package/src/validation/agent.js.map +0 -1
- package/src/validation/agents.d.ts +0 -2
- package/src/validation/agents.d.ts.map +0 -1
- package/src/validation/agents.js +0 -3
- package/src/validation/agents.js.map +0 -1
- package/src/validation/errors.d.ts +0 -32
- package/src/validation/errors.d.ts.map +0 -1
- package/src/validation/errors.js +0 -42
- package/src/validation/errors.js.map +0 -1
- package/src/validation/execution.d.ts +0 -86
- package/src/validation/execution.d.ts.map +0 -1
- package/src/validation/execution.js +0 -28
- package/src/validation/execution.js.map +0 -1
- package/src/validation/index.d.ts +0 -9
- package/src/validation/index.d.ts.map +0 -1
- package/src/validation/index.js +0 -9
- package/src/validation/index.js.map +0 -1
- package/src/validation/mcp.d.ts +0 -42
- package/src/validation/mcp.d.ts.map +0 -1
- package/src/validation/mcp.js +0 -28
- package/src/validation/mcp.js.map +0 -1
- package/src/validation/message.d.ts +0 -43
- package/src/validation/message.d.ts.map +0 -1
- package/src/validation/message.js +0 -23
- package/src/validation/message.js.map +0 -1
- package/src/validation/session.d.ts +0 -49
- package/src/validation/session.d.ts.map +0 -1
- package/src/validation/session.js +0 -34
- package/src/validation/session.js.map +0 -1
- package/src/validation/task.d.ts +0 -146
- package/src/validation/task.d.ts.map +0 -1
- package/src/validation/task.js +0 -95
- package/src/validation/task.js.map +0 -1
- package/src/validation/tasks.d.ts +0 -2
- package/src/validation/tasks.d.ts.map +0 -1
- package/src/validation/tasks.js +0 -3
- package/src/validation/tasks.js.map +0 -1
- package/src/validation/usage.d.ts +0 -89
- package/src/validation/usage.d.ts.map +0 -1
- package/src/validation/usage.js +0 -98
- package/src/validation/usage.js.map +0 -1
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
-
import pRetry from "p-retry";
|
|
3
|
-
import { generateId } from "../utils/ids.js";
|
|
4
|
-
import { nowIso } from "../utils/timestamps.js";
|
|
5
|
-
import { resolveToolCallName } from "../clients/tool-naming.js";
|
|
6
|
-
import { ToolError } from "../validation/errors.js";
|
|
7
|
-
import { logger } from "../logger.js";
|
|
8
|
-
import { config } from "../config.js";
|
|
9
|
-
// ─── Model max-output token table ────────────────────────────────────────────
|
|
10
|
-
// Maps model-name prefix → documented max output tokens (standard sync API,
|
|
11
|
-
// no beta headers). Checked longest-prefix-first so "claude-opus-4-5" (64k)
|
|
12
|
-
// wins over "claude-opus-4" (128k). Source: docs.anthropic.com/models/overview.
|
|
13
|
-
// config.server.defaultMaxTokens is used as the fallback for unknown / future models.
|
|
14
|
-
const MODEL_MAX_TOKENS = [
|
|
15
|
-
// Claude 5 (GA 2026-06)
|
|
16
|
-
["claude-fable-5", 128_000],
|
|
17
|
-
["claude-mythos-5", 128_000],
|
|
18
|
-
// Claude 4 — Opus (8/7/6 = 128k; 5 = 64k; 1/0 = 32k)
|
|
19
|
-
["claude-opus-4-8", 128_000],
|
|
20
|
-
["claude-opus-4-7", 128_000],
|
|
21
|
-
["claude-opus-4-6", 128_000],
|
|
22
|
-
["claude-opus-4-5", 64_000],
|
|
23
|
-
["claude-opus-4-1", 32_000],
|
|
24
|
-
["claude-opus-4-0", 32_000],
|
|
25
|
-
["claude-opus-4", 128_000], // catch future claude-opus-4-N ≥ 4-6 tier
|
|
26
|
-
// Claude 4 — Sonnet (all known variants: 64k)
|
|
27
|
-
["claude-sonnet-4", 64_000],
|
|
28
|
-
// Claude 4 — Haiku (all known variants: 64k)
|
|
29
|
-
["claude-haiku-4", 64_000],
|
|
30
|
-
// Claude 3.5 family
|
|
31
|
-
["claude-3-5-sonnet", 8_192],
|
|
32
|
-
["claude-3-5-haiku", 8_192],
|
|
33
|
-
// Claude 3 family
|
|
34
|
-
["claude-3-opus", 4_096],
|
|
35
|
-
["claude-3-sonnet", 4_096],
|
|
36
|
-
["claude-3-haiku", 4_096],
|
|
37
|
-
];
|
|
38
|
-
function defaultMaxTokens(model) {
|
|
39
|
-
for (const [prefix, maxTokens] of MODEL_MAX_TOKENS) {
|
|
40
|
-
if (model.startsWith(prefix))
|
|
41
|
-
return maxTokens;
|
|
42
|
-
}
|
|
43
|
-
// Unknown / future model — fall back to the global default
|
|
44
|
-
return config.server.defaultMaxTokens;
|
|
45
|
-
}
|
|
46
|
-
function buildAnthropicClient(secret, timeoutMs) {
|
|
47
|
-
if (secret.startsWith("sk-ant-api")) {
|
|
48
|
-
return {
|
|
49
|
-
client: new Anthropic({ apiKey: secret, timeout: timeoutMs }),
|
|
50
|
-
useOauthIdentity: false,
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
// OAuth / subscription token — requires the oauth-2025-04-20 beta header and
|
|
54
|
-
// the Claude Code identity block in the system prompt (see below).
|
|
55
|
-
const isOauth = secret.startsWith("sk-ant-oat");
|
|
56
|
-
return {
|
|
57
|
-
client: new Anthropic({
|
|
58
|
-
authToken: secret,
|
|
59
|
-
timeout: timeoutMs,
|
|
60
|
-
...(isOauth ? { defaultHeaders: { "anthropic-beta": "oauth-2025-04-20" } } : {}),
|
|
61
|
-
}),
|
|
62
|
-
useOauthIdentity: isOauth,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
function toAnthropicTools(tools) {
|
|
66
|
-
if (!tools)
|
|
67
|
-
return undefined;
|
|
68
|
-
return tools.map(tool => ({
|
|
69
|
-
name: tool.name,
|
|
70
|
-
description: tool.description || "",
|
|
71
|
-
input_schema: (tool.inputSchema || {
|
|
72
|
-
type: "object",
|
|
73
|
-
properties: {},
|
|
74
|
-
}),
|
|
75
|
-
}));
|
|
76
|
-
}
|
|
77
|
-
function toAnthropicMessages(messages) {
|
|
78
|
-
// Filter out system messages — they're passed separately in the `system` field
|
|
79
|
-
const nonSystem = messages.filter(m => m.role !== "system");
|
|
80
|
-
return nonSystem.map((message) => {
|
|
81
|
-
if (message.role === "tool") {
|
|
82
|
-
const toolResult = message.toolResults?.[0];
|
|
83
|
-
if (!toolResult) {
|
|
84
|
-
throw new Error("Tool message missing tool result");
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
role: "user",
|
|
88
|
-
content: [
|
|
89
|
-
{
|
|
90
|
-
type: "tool_result",
|
|
91
|
-
tool_use_id: toolResult.toolCallId,
|
|
92
|
-
content: JSON.stringify(toolResult.result),
|
|
93
|
-
is_error: toolResult.isError,
|
|
94
|
-
},
|
|
95
|
-
],
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
if (message.role === "assistant" && message.toolCalls?.length) {
|
|
99
|
-
const contentBlocks = [];
|
|
100
|
-
if (message.content) {
|
|
101
|
-
contentBlocks.push({ type: "text", text: message.content });
|
|
102
|
-
}
|
|
103
|
-
for (const tc of message.toolCalls) {
|
|
104
|
-
contentBlocks.push({
|
|
105
|
-
type: "tool_use",
|
|
106
|
-
id: tc.id,
|
|
107
|
-
name: tc.tool,
|
|
108
|
-
input: tc.arguments,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
return { role: "assistant", content: contentBlocks };
|
|
112
|
-
}
|
|
113
|
-
if (message.role === "assistant") {
|
|
114
|
-
return { role: "assistant", content: message.content || "" };
|
|
115
|
-
}
|
|
116
|
-
if (message.role === "user") {
|
|
117
|
-
return { role: "user", content: message.content || "" };
|
|
118
|
-
}
|
|
119
|
-
// Should never reach here after filtering system messages
|
|
120
|
-
throw new Error(`Unsupported message role for Anthropic: ${message.role}`);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
// Subscription OAuth tokens (sk-ant-oat…) only work against the Messages API when
|
|
124
|
-
// BOTH of these hold (verified by direct-API bisect — see chat()):
|
|
125
|
-
// 1. the request carries the `anthropic-beta: oauth-2025-04-20` header, and
|
|
126
|
-
// 2. THIS identity is sent as the first `system` block in ARRAY form — not
|
|
127
|
-
// concatenated with the agent's system prompt into a single string.
|
|
128
|
-
// Bisect: identity-only string → 200; identity+prompt joined into one string → 429;
|
|
129
|
-
// [identity block, prompt block] array → 200. Either omission yields a *misleading*
|
|
130
|
-
// `429 rate_limit_error` (no rate-limit headers), which is why OAuth was silently
|
|
131
|
-
// broken for any agent that had its own system prompt. Plain API keys need neither.
|
|
132
|
-
const CLAUDE_CODE_IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude.";
|
|
133
|
-
export class AnthropicProvider {
|
|
134
|
-
client;
|
|
135
|
-
providerConfig;
|
|
136
|
-
/** True when the resolved secret is a sk-ant-oat… OAuth token. */
|
|
137
|
-
useOauthIdentity;
|
|
138
|
-
constructor(providerConfig) {
|
|
139
|
-
this.providerConfig = providerConfig;
|
|
140
|
-
// Resolve the single unified secret via config (§3 / §3c)
|
|
141
|
-
const resolved = config.getProviderConfig({
|
|
142
|
-
provider: "anthropic",
|
|
143
|
-
secret: providerConfig.env?.secret,
|
|
144
|
-
model: providerConfig.env?.model,
|
|
145
|
-
inlineModel: providerConfig.model,
|
|
146
|
-
});
|
|
147
|
-
if (!resolved.secret) {
|
|
148
|
-
// No secret found and we're not a localhost server (anthropic has no localhost exemption)
|
|
149
|
-
throw new ToolError("PROVIDER_AUTH_ERROR", `Anthropic requires a credential. ` +
|
|
150
|
-
`Set ADHD_AGENT_ANTHROPIC_SECRET in your ~/.adhd/.env ` +
|
|
151
|
-
`(run \`claude setup-token\` to obtain an OAuth access token, ` +
|
|
152
|
-
`or use your console.anthropic.com API key).`);
|
|
153
|
-
}
|
|
154
|
-
const { client, useOauthIdentity } = buildAnthropicClient(resolved.secret, providerConfig.timeoutMs);
|
|
155
|
-
this.client = client;
|
|
156
|
-
this.useOauthIdentity = useOauthIdentity;
|
|
157
|
-
}
|
|
158
|
-
async chat(request) {
|
|
159
|
-
const retryConfig = this.providerConfig.retryConfig;
|
|
160
|
-
const systemMessages = request.messages.filter(m => m.role === "system");
|
|
161
|
-
const systemPrompt = systemMessages.map(m => m.content || "").join("\n") || undefined;
|
|
162
|
-
const run = async () => {
|
|
163
|
-
// sk-ant-oat OAuth tokens require the Claude Code identity as a DISTINCT
|
|
164
|
-
// first system block (array form) — NOT concatenated into one string.
|
|
165
|
-
// Anthropic's OAuth gate rejects a single system string that isn't exactly
|
|
166
|
-
// the identity with a misleading 429 rate_limit_error, so any agent that
|
|
167
|
-
// has its own system prompt fails unless the identity is its own block.
|
|
168
|
-
// (Proven by bisect: array form → 200; identity+prompt joined string → 429.)
|
|
169
|
-
// API-key mode keeps the plain string.
|
|
170
|
-
const effectiveSystem = this.useOauthIdentity
|
|
171
|
-
? [
|
|
172
|
-
{ type: "text", text: CLAUDE_CODE_IDENTITY },
|
|
173
|
-
...(systemPrompt
|
|
174
|
-
? [{ type: "text", text: systemPrompt }]
|
|
175
|
-
: []),
|
|
176
|
-
]
|
|
177
|
-
: systemPrompt;
|
|
178
|
-
const effectiveModel = this.providerConfig.model ?? "";
|
|
179
|
-
if (!effectiveModel) {
|
|
180
|
-
logger.warn("AnthropicProvider: no model configured; API will likely reject");
|
|
181
|
-
}
|
|
182
|
-
// Use streaming to avoid the SDK's synchronous "Streaming is required
|
|
183
|
-
// for operations that may take longer than 10 minutes" throw that fires
|
|
184
|
-
// when max_tokens is large (e.g. 64k on haiku-4-5) with messages.create().
|
|
185
|
-
const stream = this.client.messages.stream({
|
|
186
|
-
model: effectiveModel,
|
|
187
|
-
system: effectiveSystem,
|
|
188
|
-
temperature: this.providerConfig.temperature,
|
|
189
|
-
max_tokens: this.providerConfig.maxTokens ?? defaultMaxTokens(effectiveModel),
|
|
190
|
-
messages: toAnthropicMessages(request.messages),
|
|
191
|
-
tools: toAnthropicTools(request.tools),
|
|
192
|
-
}, { signal: request.signal });
|
|
193
|
-
const response = await stream.finalMessage();
|
|
194
|
-
const toolCalls = [];
|
|
195
|
-
const contentParts = [];
|
|
196
|
-
for (const block of response.content) {
|
|
197
|
-
if (block.type === "text") {
|
|
198
|
-
contentParts.push(block.text);
|
|
199
|
-
}
|
|
200
|
-
if (block.type === "tool_use") {
|
|
201
|
-
// Resolve qualified or bare tool names against the advertised set
|
|
202
|
-
// (a bare `agent` → `agent-mcp__agent` when unambiguous; DEBT-004).
|
|
203
|
-
const { server, tool } = resolveToolCallName(block.name, (request.tools ?? []).map((t) => t.name));
|
|
204
|
-
toolCalls.push({
|
|
205
|
-
id: block.id,
|
|
206
|
-
server,
|
|
207
|
-
tool,
|
|
208
|
-
arguments: block.input,
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
const message = {
|
|
213
|
-
id: generateId(),
|
|
214
|
-
sessionId: "",
|
|
215
|
-
role: "assistant",
|
|
216
|
-
content: contentParts.length > 0 ? contentParts.join("\n") : undefined,
|
|
217
|
-
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
218
|
-
createdAt: nowIso(),
|
|
219
|
-
};
|
|
220
|
-
const sdkUsage = response.usage;
|
|
221
|
-
const STOP_REASON = {
|
|
222
|
-
end_turn: "stop", max_tokens: "length", tool_use: "tool_calls",
|
|
223
|
-
};
|
|
224
|
-
const normalisedStopReason = STOP_REASON[response.stop_reason ?? ""] ?? "unknown";
|
|
225
|
-
return {
|
|
226
|
-
message,
|
|
227
|
-
stopReason: toolCalls.length > 0 ? "tool_calls" : "completed",
|
|
228
|
-
usage: {
|
|
229
|
-
inputTokens: sdkUsage.input_tokens,
|
|
230
|
-
outputTokens: sdkUsage.output_tokens,
|
|
231
|
-
stopReason: normalisedStopReason,
|
|
232
|
-
maxTokens: this.providerConfig.maxTokens ?? defaultMaxTokens(effectiveModel),
|
|
233
|
-
cacheReadTokens: sdkUsage.cache_read_input_tokens ?? undefined,
|
|
234
|
-
cacheCreationTokens: sdkUsage.cache_creation_input_tokens ?? undefined,
|
|
235
|
-
},
|
|
236
|
-
};
|
|
237
|
-
};
|
|
238
|
-
if (retryConfig) {
|
|
239
|
-
return pRetry(run, {
|
|
240
|
-
retries: retryConfig.retries,
|
|
241
|
-
minTimeout: retryConfig.minTimeout,
|
|
242
|
-
maxTimeout: retryConfig.maxTimeout,
|
|
243
|
-
factor: retryConfig.factor,
|
|
244
|
-
onFailedAttempt: error => {
|
|
245
|
-
if (request.signal?.aborted) {
|
|
246
|
-
throw error; // don't retry on cancellation
|
|
247
|
-
}
|
|
248
|
-
// Don't retry auth failures — credentials won't change between attempts
|
|
249
|
-
if ("status" in error && error.status === 401) {
|
|
250
|
-
throw new ToolError("PROVIDER_AUTH_ERROR", `Anthropic authentication failed. ` +
|
|
251
|
-
`Check ADHD_AGENT_ANTHROPIC_SECRET in your ~/.adhd/.env ` +
|
|
252
|
-
`(run \`claude setup-token\` to obtain a fresh OAuth access token).`);
|
|
253
|
-
}
|
|
254
|
-
},
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
return run();
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
//# sourceMappingURL=anthropic.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../../../../packages/ai/agent-mcp/src/providers/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAE1C,OAAO,MAAM,MAAM,SAAS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;QAEF,IAAI,WAAW,EAAE,CAAC;YACd,OAAO,MAAM,CAAC,GAAG,EAAE;gBACf,OAAO,EAAE,WAAW,CAAC,OAAO;gBAC5B,UAAU,EAAE,WAAW,CAAC,UAAU;gBAClC,UAAU,EAAE,WAAW,CAAC,UAAU;gBAClC,MAAM,EAAE,WAAW,CAAC,MAAM;gBAC1B,eAAe,EAAE,KAAK,CAAC,EAAE;oBACrB,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;wBAC1B,MAAM,KAAK,CAAC,CAAC,8BAA8B;oBAC/C,CAAC;oBACD,wEAAwE;oBACxE,IAAI,QAAQ,IAAI,KAAK,IAAK,KAA6B,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;wBACrE,MAAM,IAAI,SAAS,CACf,qBAAqB,EACrB,mCAAmC;4BACnC,yDAAyD;4BACzD,oEAAoE,CACvE,CAAC;oBACN,CAAC;gBACL,CAAC;aACJ,CAAC,CAAC;QACP,CAAC;QAED,OAAO,GAAG,EAAE,CAAC;IACjB,CAAC;CACJ"}
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ClaudeCliProvider — drives the local `claude` CLI (Claude Code) as an LLM
|
|
3
|
-
* backend using a persistent subprocess with bidirectional stream-json I/O.
|
|
4
|
-
*
|
|
5
|
-
* Auth: uses whatever credentials Claude Code already has configured
|
|
6
|
-
* (subscription, API key, OAuth — whatever `claude auth status` shows).
|
|
7
|
-
*
|
|
8
|
-
* Flags applied on every invocation:
|
|
9
|
-
* --system-prompt Replace Claude Code's default prompt with the agent's prompt
|
|
10
|
-
* --mcp-config <tempfile> Writes the agent's mcpServers to a temp JSON file; path passed here
|
|
11
|
-
* --strict-mcp-config Ignore all other MCP configs; only load what we pass
|
|
12
|
-
* --input-format stream-json Accept NDJSON messages on stdin
|
|
13
|
-
* --output-format stream-json Emit NDJSON events on stdout
|
|
14
|
-
* --verbose Emit full event stream including tool calls and results
|
|
15
|
-
*
|
|
16
|
-
* --disallowedTools <name> (×N) — one pair per built-in not in allowedBuiltinTools.
|
|
17
|
-
* All Claude Code built-ins are blocked by default; only tools explicitly listed
|
|
18
|
-
* in the agent definition's allowedBuiltinTools field are permitted. MCP tools
|
|
19
|
-
* (from --mcp-config) are unaffected.
|
|
20
|
-
*
|
|
21
|
-
* ⚠️ --tools "" is NOT used — it disables MCP tools as well as built-ins.
|
|
22
|
-
* ⚠️ --bare is NOT used — it blocks --mcp-config from loading entirely.
|
|
23
|
-
*
|
|
24
|
-
* Agent-spec mode (config.systemPromptIsAgentSpec === true):
|
|
25
|
-
* The agent's systemPrompt is treated as a complete Claude Code agent markdown
|
|
26
|
-
* file (YAML frontmatter + body). Instead of --system-prompt + --disallowedTools,
|
|
27
|
-
* the provider writes it to an isolated temp project dir and passes
|
|
28
|
-
* --add-dir <tmpdir> --setting-sources project --agent <frontmatterName>
|
|
29
|
-
* so Claude *internally parses the frontmatter header* — including the `tools:`
|
|
30
|
-
* field — which then governs tool access and TAKES PRECEDENCE over the built-in
|
|
31
|
-
* disallow enumeration. `--agent` matches the frontmatter `name:`, not the
|
|
32
|
-
* filename; cwd is preserved so file/Bash tools keep their working root. Omit
|
|
33
|
-
* `tools:` in the header to inherit all tools; list `mcp__<server>__<tool>`
|
|
34
|
-
* entries to expose specific MCP tools. allowedBuiltinTools is ignored in this mode.
|
|
35
|
-
*
|
|
36
|
-
* Tool loop: when Claude Code emits a tool_use block, we execute it via the
|
|
37
|
-
* request.executeTool callback (wired by the orchestrator to the McpClientRegistry)
|
|
38
|
-
* and write a tool_result message back to stdin. This continues until Claude
|
|
39
|
-
* emits a "result" event, at which point chat() resolves with stopReason "completed".
|
|
40
|
-
*
|
|
41
|
-
* ⚠️ KNOWN ISSUE: Claude Code may crash with
|
|
42
|
-
* "Z is not an Object. (evaluating '"tool_use_id"in Z')"
|
|
43
|
-
* when a tool_result is written to stdin. This is a suspected Claude Code bug
|
|
44
|
-
* (github.com/anthropics/claude-code/issues/24594). If you hit this, the
|
|
45
|
-
* provider will throw PROVIDER_ERROR and the task will fail. Fall back to the
|
|
46
|
-
* anthropic provider with useClaudeOauth: true for a fully supported path.
|
|
47
|
-
*/
|
|
48
|
-
import type { LLMProvider, ProviderChatRequest, ProviderChatResponse } from "./types.js";
|
|
49
|
-
import type { McpServerConfig, ProviderConfig } from "../validation/index.js";
|
|
50
|
-
type ClaudeCliConfig = Extract<ProviderConfig, {
|
|
51
|
-
type: "claudecli";
|
|
52
|
-
}>;
|
|
53
|
-
/**
|
|
54
|
-
* Compute the effective allowed built-in set and the `--disallowedTools` argv
|
|
55
|
-
* entries that must be passed to the `claude` CLI subprocess.
|
|
56
|
-
*
|
|
57
|
-
* This is the single source of truth for [inv:no-third-tool-model]: the
|
|
58
|
-
* allowed set is resolved in priority order:
|
|
59
|
-
* 1. `compiledTools` (AGENT_TOOL / compile.tools model) — wins when present.
|
|
60
|
-
* 2. `config.allowedBuiltinTools` — legacy / transition-window fallback.
|
|
61
|
-
*
|
|
62
|
-
* Extracted here so tests can assert the REAL produced argv without spawning
|
|
63
|
-
* a subprocess. `chat()` must call this function; no divergence allowed.
|
|
64
|
-
*
|
|
65
|
-
* @param compiledTools - Platform-alias array from `compileAgent().tools`, or
|
|
66
|
-
* `undefined` when the compiler integration is not available yet.
|
|
67
|
-
* @param allowedBuiltinTools - Per-agent legacy allowlist from config.
|
|
68
|
-
* @returns `{ effectiveAllowed, disallowedArgv }` where `disallowedArgv` is the
|
|
69
|
-
* flat `["--disallowedTools", "<name>", ...]` fragment ready to push into args.
|
|
70
|
-
*/
|
|
71
|
-
export declare function computeClaudeBuiltinArgs(params: {
|
|
72
|
-
compiledTools: string[] | undefined;
|
|
73
|
-
allowedBuiltinTools: string[] | undefined;
|
|
74
|
-
}): {
|
|
75
|
-
effectiveAllowed: string[];
|
|
76
|
-
disallowedArgv: string[];
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Extracts the `name:` field from the leading YAML frontmatter block of a Claude
|
|
80
|
-
* Code agent markdown system prompt. Only the first `---`-delimited block at the
|
|
81
|
-
* very top of the string is considered. Returns undefined when there is no leading
|
|
82
|
-
* frontmatter block or no `name:` key.
|
|
83
|
-
*
|
|
84
|
-
* Claude Code selects an agent (`--agent <name>`) by this frontmatter name — not
|
|
85
|
-
* the on-disk filename — so the provider must read it to build the correct flag.
|
|
86
|
-
*/
|
|
87
|
-
export declare function extractAgentSpecName(md: string): string | undefined;
|
|
88
|
-
/**
|
|
89
|
-
* Ensures an agent-spec markdown carries a frontmatter `name:` so the subprocess
|
|
90
|
-
* can select it with `--agent`. Returns the markdown to write and the agent name
|
|
91
|
-
* to pass to `--agent`.
|
|
92
|
-
*
|
|
93
|
-
* - If the prompt already names the agent, it is used verbatim and the markdown is
|
|
94
|
-
* returned unchanged (the spec author's `tools:` header is the source of truth).
|
|
95
|
-
* - If a frontmatter block exists but lacks `name:`, a generated name is injected
|
|
96
|
-
* into it.
|
|
97
|
-
* - If there is no frontmatter at all, the prompt is wrapped in a minimal block
|
|
98
|
-
* (no `tools:` → Claude inherits all tools, matching pre-spec-mode behavior).
|
|
99
|
-
*/
|
|
100
|
-
export declare function normalizeAgentSpec(md: string): {
|
|
101
|
-
content: string;
|
|
102
|
-
agentName: string;
|
|
103
|
-
};
|
|
104
|
-
export declare class ClaudeCliProvider implements LLMProvider {
|
|
105
|
-
private readonly config;
|
|
106
|
-
private readonly mcpServers;
|
|
107
|
-
/**
|
|
108
|
-
* Platform-alias tool list derived from the AGENT_TOOL / compiled.tools model.
|
|
109
|
-
*
|
|
110
|
-
* When supplied (via `compileAgent({ platform: "claude_code" }).tools`), this
|
|
111
|
-
* array is the single source of truth for which Claude Code built-ins are
|
|
112
|
-
* permitted — config.allowedBuiltinTools is ignored. When absent, the provider
|
|
113
|
-
* falls back to config.allowedBuiltinTools (legacy / transition-window path).
|
|
114
|
-
*
|
|
115
|
-
* [inv:no-third-tool-model] — one tool-permission source, not two.
|
|
116
|
-
*/
|
|
117
|
-
private readonly compiledTools;
|
|
118
|
-
constructor(config: ClaudeCliConfig, mcpServers?: Record<string, McpServerConfig>, compiledTools?: string[]);
|
|
119
|
-
/**
|
|
120
|
-
* Build the subprocess environment for the claude CLI subprocess.
|
|
121
|
-
*
|
|
122
|
-
* Merges the live process.env (PATH, HOME, call-time vars) with the
|
|
123
|
-
* config snapshot (ADHD_AGENT_* vars loaded from the .env hierarchy at
|
|
124
|
-
* startup), with the config snapshot winning on conflicts. This ensures
|
|
125
|
-
* the subprocess inherits both the user's shell context and any overrides
|
|
126
|
-
* loaded via the .env hierarchy — and allows test-injected env vars (e.g.
|
|
127
|
-
* CAPTURE_FILE) to flow through to the subprocess without a module reload.
|
|
128
|
-
*/
|
|
129
|
-
private buildSubprocessEnv;
|
|
130
|
-
/**
|
|
131
|
-
* Write the agent's McpServerConfig map to a temp JSON file in the format
|
|
132
|
-
* Claude Code's --mcp-config flag expects. Inline JSON strings are not parsed
|
|
133
|
-
* by the CLI — a file path is required.
|
|
134
|
-
*
|
|
135
|
-
* Maps our `transport` field to Claude Code's `type`. The caller is responsible
|
|
136
|
-
* for deleting the file (use the finally block in chat()).
|
|
137
|
-
*
|
|
138
|
-
* Returns undefined if there are no servers to configure.
|
|
139
|
-
*/
|
|
140
|
-
private writeMcpConfigFile;
|
|
141
|
-
/**
|
|
142
|
-
* Writes an agent-spec markdown system prompt to an isolated temp project dir
|
|
143
|
-
* as `<dir>/.claude/agents/<name>.md`, so the subprocess can discover and
|
|
144
|
-
* parse it via `--add-dir <dir> --setting-sources project --agent <name>`.
|
|
145
|
-
*
|
|
146
|
-
* Returns the temp dir (for `--add-dir`) and the frontmatter agent name (for
|
|
147
|
-
* `--agent`). The caller MUST delete the dir in its finally block.
|
|
148
|
-
*/
|
|
149
|
-
private writeAgentSpecDir;
|
|
150
|
-
chat(request: ProviderChatRequest): Promise<ProviderChatResponse>;
|
|
151
|
-
}
|
|
152
|
-
export {};
|
|
153
|
-
//# sourceMappingURL=claudecli.d.ts.map
|
|
@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