@tangenta/claude-code 0.0.0-stage → 0.20.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/.claude-plugin/plugin.json +15 -0
- package/.mcp.json +20 -0
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/commands/renew.md +6 -0
- package/commands/setup.md +10 -0
- package/hooks/hooks.json +67 -0
- package/lib/agent-proxy.mjs +214 -0
- package/lib/agent.mjs +207 -0
- package/lib/config.mjs +200 -0
- package/lib/hooks.mjs +551 -0
- package/lib/inputs.mjs +95 -0
- package/lib/proxy.mjs +206 -0
- package/lib/store.mjs +138 -0
- package/lib/tangenta.mjs +89 -0
- package/package.json +31 -3
- package/scripts/agent-mcp.mjs +34 -0
- package/scripts/hook.mjs +39 -0
- package/scripts/mcp-proxy.mjs +45 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tangenta",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Attaches Claude Code to a Tangenta issue: the issue page shows the session's plan and activity, and people on it can message, answer and stop Claude.",
|
|
5
|
+
"author": { "name": "Tangenta" },
|
|
6
|
+
"keywords": ["tangenta", "issues", "agents"],
|
|
7
|
+
"userConfig": {
|
|
8
|
+
"api_url": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"title": "Tangenta API address",
|
|
11
|
+
"description": "Only for a self-hosted Tangenta: its API address. Leave it empty for the hosted Tangenta. TANGENTA_API_URL in the environment wins over it.",
|
|
12
|
+
"required": false
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
package/.mcp.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mcpServers": {
|
|
3
|
+
"session": {
|
|
4
|
+
"type": "stdio",
|
|
5
|
+
"command": "node",
|
|
6
|
+
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/mcp-proxy.mjs"],
|
|
7
|
+
"env": {
|
|
8
|
+
"TANGENTA_PLUGIN_API_URL": "${user_config.api_url}"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"agent": {
|
|
12
|
+
"type": "stdio",
|
|
13
|
+
"command": "node",
|
|
14
|
+
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/agent-mcp.mjs"],
|
|
15
|
+
"env": {
|
|
16
|
+
"TANGENTA_PLUGIN_API_URL": "${user_config.api_url}"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Koode
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tangenta/claude-code
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The [Tangenta](https://tangenta.app) plugin for Claude Code. It attaches your own Claude Code to
|
|
4
|
+
a Tangenta issue: the issue page shows the session's plan and activity, and the people you
|
|
5
|
+
choose can message Claude, answer its questions and stop it. Hooks deliver what happens on the
|
|
6
|
+
issue to Claude; a local MCP server forwards the session tools to Tangenta with the session's
|
|
7
|
+
token. Plain JavaScript for Node 20 or later, no dependencies.
|
|
8
|
+
|
|
9
|
+
It needs a Tangenta instance: the hosted one at tangenta.app, or your own.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
Every Tangenta instance serves a marketplace file that pins this package to the version the
|
|
14
|
+
instance runs:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
claude plugin marketplace add https://tangenta.app/integrations/claude-code/marketplace.json
|
|
18
|
+
claude plugin install tangenta@tangenta
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
For a self-hosted instance, use that instance's address in the first command and set the
|
|
22
|
+
plugin's `api_url` option to its API address (or `TANGENTA_API_URL` in the shell you start
|
|
23
|
+
Claude Code from). The hosted Tangenta needs no setting.
|
|
24
|
+
|
|
25
|
+
Then open an issue in Tangenta, start an agent session and send the prompt it gives you to
|
|
26
|
+
Claude Code. See `docs/AGENTS.md` in the Tangenta repository for the trust rules and the limits.
|
|
27
|
+
|
|
28
|
+
## Connect as an agent
|
|
29
|
+
|
|
30
|
+
The plugin also connects Claude Code to Tangenta as an agent: a member of the workspace with its
|
|
31
|
+
own API key and access. Add the agent in Tangenta (Settings > Agents) and copy its one-time
|
|
32
|
+
setup prompt into Claude Code. The prompt installs this plugin and runs `/tangenta:setup` with
|
|
33
|
+
the setup token, which works once within an hour.
|
|
34
|
+
|
|
35
|
+
A second local MCP server, `agent`, keeps the agent's key on your machine and never shows it. Its
|
|
36
|
+
tools:
|
|
37
|
+
|
|
38
|
+
- `connect_agent` exchanges the setup token (and, for a self-hosted instance, the API address)
|
|
39
|
+
for the agent's key.
|
|
40
|
+
- `agent_status` tells which agent, workspace and access Claude Code is connected as, and whether
|
|
41
|
+
Tangenta still accepts the key.
|
|
42
|
+
- `renew_agent_key` swaps the key for a new one with the same access. The old key stops working
|
|
43
|
+
5 minutes later.
|
|
44
|
+
|
|
45
|
+
Slash commands:
|
|
46
|
+
|
|
47
|
+
- `/tangenta:setup <setup token> [API address]` calls `connect_agent`, then `agent_status`.
|
|
48
|
+
- `/tangenta:renew` calls `renew_agent_key`, then `agent_status`.
|
|
49
|
+
|
|
50
|
+
MIT licensed.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Renew the Tangenta agent's API key (the old key stops working 5 minutes later)
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Call the tool `renew_agent_key` of the Tangenta plugin's `agent` MCP server, then `agent_status`
|
|
6
|
+
of the same server, and tell me what they answered. Never ask for, print or store the key.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Connect Claude Code to Tangenta as an agent, with the one-time setup token of its setup prompt
|
|
3
|
+
argument-hint: <setup token> [API address]
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Connect this Claude Code to Tangenta as the agent the setup token was made for: call the tool
|
|
7
|
+
`connect_agent` of the Tangenta plugin's `agent` MCP server with `setupToken` set to the first
|
|
8
|
+
word of "$ARGUMENTS" and, when there is a second word, `apiUrl` set to it. Never write the token
|
|
9
|
+
or a key to a file, a command or the chat. Then call `agent_status` of the same server and tell
|
|
10
|
+
me what it answered. If `connect_agent` isn't listed yet, ask me to restart Claude Code first.
|
package/hooks/hooks.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"description": "Tangenta session hooks: attach from the prompt, deliver messages from Tangenta, stop on request and end the session.",
|
|
3
|
+
"hooks": {
|
|
4
|
+
"UserPromptSubmit": [
|
|
5
|
+
{
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "node",
|
|
10
|
+
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/hook.mjs"],
|
|
11
|
+
"timeout": 20
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"PreToolUse": [
|
|
17
|
+
{
|
|
18
|
+
"matcher": "*",
|
|
19
|
+
"hooks": [
|
|
20
|
+
{
|
|
21
|
+
"type": "command",
|
|
22
|
+
"command": "node",
|
|
23
|
+
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/hook.mjs"],
|
|
24
|
+
"timeout": 15
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"PostToolUse": [
|
|
30
|
+
{
|
|
31
|
+
"matcher": "*",
|
|
32
|
+
"hooks": [
|
|
33
|
+
{
|
|
34
|
+
"type": "command",
|
|
35
|
+
"command": "node",
|
|
36
|
+
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/hook.mjs"],
|
|
37
|
+
"timeout": 15
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
"Stop": [
|
|
43
|
+
{
|
|
44
|
+
"hooks": [
|
|
45
|
+
{
|
|
46
|
+
"type": "command",
|
|
47
|
+
"command": "node",
|
|
48
|
+
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/hook.mjs"],
|
|
49
|
+
"timeout": 600
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
"SessionEnd": [
|
|
55
|
+
{
|
|
56
|
+
"hooks": [
|
|
57
|
+
{
|
|
58
|
+
"type": "command",
|
|
59
|
+
"command": "node",
|
|
60
|
+
"args": ["${CLAUDE_PLUGIN_ROOT}/scripts/hook.mjs"],
|
|
61
|
+
"timeout": 2
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugin's `agent` MCP server (stdio): Claude Code works in Tangenta as one agent member with
|
|
3
|
+
* the agent's own API key (lib/agent.mjs). Its own tools connect (`connect_agent`, with the setup
|
|
4
|
+
* token of the agent's setup prompt), check (`agent_status`) and renew (`renew_agent_key`) that
|
|
5
|
+
* key; once connected, every Tangenta MCP tool is listed and forwarded to `<api>/mcp` with the key,
|
|
6
|
+
* read from the state file on every call, so a renewal takes effect at once. After connecting it
|
|
7
|
+
* tells Claude Code the tool list changed.
|
|
8
|
+
*/
|
|
9
|
+
import { ConfigError } from "./config.mjs";
|
|
10
|
+
import {
|
|
11
|
+
AgentError,
|
|
12
|
+
NOT_CONNECTED,
|
|
13
|
+
connectAgent,
|
|
14
|
+
readCredentials,
|
|
15
|
+
redact,
|
|
16
|
+
renewAgentKey,
|
|
17
|
+
} from "./agent.mjs";
|
|
18
|
+
import { TangentaError, rpc } from "./tangenta.mjs";
|
|
19
|
+
|
|
20
|
+
const VERSIONS = ["2025-11-25", "2025-06-18", "2025-03-26"];
|
|
21
|
+
const CALL_TIMEOUT_MS = 30_000;
|
|
22
|
+
const LIST_TIMEOUT_MS = 10_000;
|
|
23
|
+
|
|
24
|
+
export const AGENT_TOOLS = [
|
|
25
|
+
{
|
|
26
|
+
name: "connect_agent",
|
|
27
|
+
title: "Connect as the Tangenta agent",
|
|
28
|
+
description:
|
|
29
|
+
"Exchanges the one-time setup token from an agent's Tangenta setup prompt for the " +
|
|
30
|
+
"agent's own API key, which the plugin keeps; the key is never shown. The token works " +
|
|
31
|
+
"once, within an hour.",
|
|
32
|
+
inputSchema: {
|
|
33
|
+
type: "object",
|
|
34
|
+
properties: {
|
|
35
|
+
setupToken: { type: "string", description: "The setup token, tng_stp_..." },
|
|
36
|
+
apiUrl: { type: "string", description: "The API address the setup prompt names." },
|
|
37
|
+
},
|
|
38
|
+
required: ["setupToken"],
|
|
39
|
+
additionalProperties: false,
|
|
40
|
+
},
|
|
41
|
+
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: "agent_status",
|
|
45
|
+
title: "Tangenta agent status",
|
|
46
|
+
description:
|
|
47
|
+
"Which Tangenta agent, workspace and access Claude Code is connected as, and whether " +
|
|
48
|
+
"Tangenta still accepts its key.",
|
|
49
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
50
|
+
annotations: { readOnlyHint: true },
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "renew_agent_key",
|
|
54
|
+
title: "Renew the Tangenta agent key",
|
|
55
|
+
description:
|
|
56
|
+
"Swaps the agent's API key for a new one with the same access; the old key stops working " +
|
|
57
|
+
"5 minutes later.",
|
|
58
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
59
|
+
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
const LOCAL_NAMES = new Set(AGENT_TOOLS.map((tool) => tool.name));
|
|
64
|
+
|
|
65
|
+
function result(id, value) {
|
|
66
|
+
return { jsonrpc: "2.0", id, result: value };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function failure(id, code, message) {
|
|
70
|
+
return { jsonrpc: "2.0", id, error: { code, message: redact(message) } };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function text(message, isError = false) {
|
|
74
|
+
const content = [{ type: "text", text: redact(message) }];
|
|
75
|
+
return isError ? { content, isError: true } : { content };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function initialize(params) {
|
|
79
|
+
const requested = params?.protocolVersion;
|
|
80
|
+
return {
|
|
81
|
+
protocolVersion: VERSIONS.includes(requested) ? requested : VERSIONS[0],
|
|
82
|
+
capabilities: { tools: { listChanged: true } },
|
|
83
|
+
serverInfo: { name: "tangenta-agent", title: "Tangenta agent", version: "0.20.0" },
|
|
84
|
+
instructions:
|
|
85
|
+
"Tangenta's tools, as the agent member this Claude Code is connected as. Connect with " +
|
|
86
|
+
"connect_agent and the setup token of the agent's setup prompt; check with agent_status.",
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function remote(credentials, method, params, timeoutMs, deps) {
|
|
91
|
+
const message = { jsonrpc: "2.0", id: 1, method, params };
|
|
92
|
+
return rpc({
|
|
93
|
+
apiUrl: credentials.apiUrl,
|
|
94
|
+
token: credentials.apiKey,
|
|
95
|
+
message,
|
|
96
|
+
timeoutMs,
|
|
97
|
+
fetchImpl: deps.fetchImpl,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The remote tools, or none when not connected or Tangenta can't be reached. */
|
|
102
|
+
async function remoteTools(env, deps) {
|
|
103
|
+
const credentials = readCredentials(env);
|
|
104
|
+
if (!credentials) return [];
|
|
105
|
+
try {
|
|
106
|
+
const answer = await remote(credentials, "tools/list", {}, LIST_TIMEOUT_MS, deps);
|
|
107
|
+
const tools = answer?.result?.tools;
|
|
108
|
+
return Array.isArray(tools) ? tools.filter((tool) => !LOCAL_NAMES.has(tool?.name)) : [];
|
|
109
|
+
} catch {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function unauthorizedText() {
|
|
115
|
+
return (
|
|
116
|
+
"Tangenta no longer accepts the agent's key: it was revoked, renewed more than 5 minutes " +
|
|
117
|
+
"ago elsewhere, or the agent was removed. Ask an admin for a new setup prompt from the " +
|
|
118
|
+
"agent's page."
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function status(env, deps) {
|
|
123
|
+
const credentials = readCredentials(env);
|
|
124
|
+
if (!credentials) return text(NOT_CONNECTED, true);
|
|
125
|
+
const agent = credentials.agent?.name ?? "the agent";
|
|
126
|
+
const workspace = credentials.workspace?.name ?? "its workspace";
|
|
127
|
+
const intro =
|
|
128
|
+
`Connected as ${agent} in ${workspace} at ${credentials.apiUrl}, ${credentials.access} ` +
|
|
129
|
+
`access, key ending ${credentials.keySuffix}.`;
|
|
130
|
+
try {
|
|
131
|
+
const answer = await remote(credentials, "tools/list", {}, LIST_TIMEOUT_MS, deps);
|
|
132
|
+
const count = answer?.result?.tools?.length ?? 0;
|
|
133
|
+
return text(`${intro} Tangenta accepts the key and offers ${count} tools.`);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error instanceof TangentaError && error.kind === "unauthorized") {
|
|
136
|
+
return text(`${intro} ${unauthorizedText()}`, true);
|
|
137
|
+
}
|
|
138
|
+
return text(`${intro} ${error.message}`, true);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function forward(id, name, args, env, deps) {
|
|
143
|
+
const credentials = readCredentials(env);
|
|
144
|
+
if (!credentials) return result(id, text(NOT_CONNECTED, true));
|
|
145
|
+
let answer;
|
|
146
|
+
try {
|
|
147
|
+
answer = await remote(
|
|
148
|
+
credentials,
|
|
149
|
+
"tools/call",
|
|
150
|
+
{ name, arguments: args },
|
|
151
|
+
CALL_TIMEOUT_MS,
|
|
152
|
+
deps,
|
|
153
|
+
);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (!(error instanceof TangentaError)) throw error;
|
|
156
|
+
const message = error.kind === "unauthorized" ? unauthorizedText() : error.message;
|
|
157
|
+
return result(id, text(message, true));
|
|
158
|
+
}
|
|
159
|
+
if (answer?.error) return failure(id, answer.error.code ?? -32_603, String(answer.error.message));
|
|
160
|
+
return result(id, answer?.result ?? text("Tangenta's answer had no result.", true));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function callLocal(id, name, args, env, deps) {
|
|
164
|
+
try {
|
|
165
|
+
if (name === "connect_agent") {
|
|
166
|
+
const message = await connectAgent(env, args, deps);
|
|
167
|
+
deps.notify?.({ jsonrpc: "2.0", method: "notifications/tools/list_changed" });
|
|
168
|
+
return result(id, text(message));
|
|
169
|
+
}
|
|
170
|
+
if (name === "renew_agent_key") return result(id, text(await renewAgentKey(env, deps)));
|
|
171
|
+
return result(id, await status(env, deps));
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if (error instanceof AgentError || error instanceof ConfigError) {
|
|
174
|
+
return result(id, text(error.message, true));
|
|
175
|
+
}
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Answers one JSON-RPC message, or null for a notification or a response. `deps.env`,
|
|
182
|
+
* `deps.fetchImpl` and `deps.notify` (sends a notification to Claude Code) replace the process
|
|
183
|
+
* environment, fetch and stdout in tests.
|
|
184
|
+
*/
|
|
185
|
+
export async function handleAgentMessage(message, deps = {}) {
|
|
186
|
+
const env = deps.env ?? process.env;
|
|
187
|
+
if (!message || message.jsonrpc !== "2.0" || typeof message.method !== "string") {
|
|
188
|
+
if (message && message.jsonrpc === "2.0" && ("result" in message || "error" in message)) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
return failure(message?.id ?? null, -32_600, "Invalid request.");
|
|
192
|
+
}
|
|
193
|
+
if (!("id" in message)) return null;
|
|
194
|
+
const { id, method } = message;
|
|
195
|
+
const params = message.params ?? {};
|
|
196
|
+
switch (method) {
|
|
197
|
+
case "initialize":
|
|
198
|
+
return result(id, initialize(params));
|
|
199
|
+
case "ping":
|
|
200
|
+
return result(id, {});
|
|
201
|
+
case "tools/list":
|
|
202
|
+
return result(id, { tools: [...AGENT_TOOLS, ...(await remoteTools(env, deps))] });
|
|
203
|
+
case "tools/call": {
|
|
204
|
+
const args = params.arguments ?? {};
|
|
205
|
+
if (typeof params.name !== "string" || typeof args !== "object" || Array.isArray(args)) {
|
|
206
|
+
return failure(id, -32_602, "A tool call needs a name and an arguments object.");
|
|
207
|
+
}
|
|
208
|
+
if (LOCAL_NAMES.has(params.name)) return callLocal(id, params.name, args, env, deps);
|
|
209
|
+
return forward(id, params.name, args, env, deps);
|
|
210
|
+
}
|
|
211
|
+
default:
|
|
212
|
+
return failure(id, -32_601, "Method not found.");
|
|
213
|
+
}
|
|
214
|
+
}
|
package/lib/agent.mjs
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent's own API key: Claude Code connects to Tangenta as one agent member. A one-time setup
|
|
3
|
+
* token (`tng_stp_`, from the agent's setup prompt) is exchanged once at
|
|
4
|
+
* `POST <api>/agents/setup/exchange` for the agent's API key (`tng_api_`), and
|
|
5
|
+
* `POST <api>/agents/keys/renew` swaps that key for a new one (the old key works 5 more minutes).
|
|
6
|
+
*
|
|
7
|
+
* The key lives in `<state root>/agent/credentials.json`: the directory 0700, the file 0600,
|
|
8
|
+
* written atomically; a state root inside the project is refused, so the key never lands in a
|
|
9
|
+
* repository. Nothing here prints, logs or answers the key or the token: answers name the last 4
|
|
10
|
+
* characters only.
|
|
11
|
+
*/
|
|
12
|
+
import {
|
|
13
|
+
chmodSync,
|
|
14
|
+
closeSync,
|
|
15
|
+
mkdirSync,
|
|
16
|
+
openSync,
|
|
17
|
+
readFileSync,
|
|
18
|
+
realpathSync,
|
|
19
|
+
renameSync,
|
|
20
|
+
writeSync,
|
|
21
|
+
} from "node:fs";
|
|
22
|
+
import { isAbsolute, join, relative } from "node:path";
|
|
23
|
+
|
|
24
|
+
import { attachApiUrl, projectDir, stateRoot } from "./config.mjs";
|
|
25
|
+
|
|
26
|
+
/** The agent API key and setup token forms; answers and errors never carry either. */
|
|
27
|
+
export const AGENT_SECRET_PATTERN = /tng_(?:api|stp)_[A-Za-z0-9_-]{20,}/g;
|
|
28
|
+
|
|
29
|
+
const SETUP_TOKEN = /^tng_stp_[A-Za-z0-9_-]{43}$/;
|
|
30
|
+
const REQUEST_TIMEOUT_MS = 15_000;
|
|
31
|
+
|
|
32
|
+
export class AgentError extends Error {}
|
|
33
|
+
|
|
34
|
+
/** `text` with every agent key and setup token replaced. */
|
|
35
|
+
export function redact(text) {
|
|
36
|
+
return String(text).replaceAll(AGENT_SECRET_PATTERN, "[kept by the Tangenta plugin]");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function realPath(path) {
|
|
40
|
+
try {
|
|
41
|
+
return realpathSync(path);
|
|
42
|
+
} catch {
|
|
43
|
+
return path;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function inside(parent, child) {
|
|
48
|
+
const path = relative(parent, child);
|
|
49
|
+
return path === "" || (!path.startsWith("..") && !isAbsolute(path));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Where the agent's credentials are kept, outside the project. */
|
|
53
|
+
export function credentialsPath(env) {
|
|
54
|
+
const root = realPath(stateRoot(env));
|
|
55
|
+
if (inside(realPath(projectDir(env, null)), root)) {
|
|
56
|
+
throw new AgentError(
|
|
57
|
+
"The Tangenta plugin's state directory is inside the project, where the agent's key could " +
|
|
58
|
+
"end up in the repository. Point TANGENTA_PLUGIN_STATE_DIR somewhere else.",
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return join(root, "agent", "credentials.json");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The stored credentials (`apiUrl`, `apiKey`, `keySuffix`, `access`, `agent`, `workspace`). */
|
|
65
|
+
export function readCredentials(env) {
|
|
66
|
+
let text;
|
|
67
|
+
try {
|
|
68
|
+
text = readFileSync(credentialsPath(env), "utf8");
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (error instanceof AgentError) throw error;
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const credentials = JSON.parse(text);
|
|
75
|
+
return typeof credentials?.apiKey === "string" && typeof credentials?.apiUrl === "string"
|
|
76
|
+
? credentials
|
|
77
|
+
: null;
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Replaces the stored credentials, atomically and readable by this user only. */
|
|
84
|
+
export function writeCredentials(env, credentials) {
|
|
85
|
+
const path = credentialsPath(env);
|
|
86
|
+
const directory = join(path, "..");
|
|
87
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
88
|
+
chmodSync(directory, 0o700);
|
|
89
|
+
const temporary = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
90
|
+
const descriptor = openSync(temporary, "wx", 0o600);
|
|
91
|
+
try {
|
|
92
|
+
writeSync(descriptor, JSON.stringify(credentials));
|
|
93
|
+
} finally {
|
|
94
|
+
closeSync(descriptor);
|
|
95
|
+
}
|
|
96
|
+
renameSync(temporary, path);
|
|
97
|
+
chmodSync(path, 0o600);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** POSTs `body` as JSON and answers the parsed answer; failures name no secret. */
|
|
101
|
+
async function post({ url, body, apiKey, fetchImpl = fetch }) {
|
|
102
|
+
const headers = { "content-type": "application/json", accept: "application/json" };
|
|
103
|
+
if (apiKey) headers.authorization = `Bearer ${apiKey}`;
|
|
104
|
+
let response;
|
|
105
|
+
try {
|
|
106
|
+
response = await fetchImpl(url, {
|
|
107
|
+
method: "POST",
|
|
108
|
+
redirect: "manual",
|
|
109
|
+
headers,
|
|
110
|
+
body: JSON.stringify(body),
|
|
111
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
112
|
+
});
|
|
113
|
+
} catch (error) {
|
|
114
|
+
const reason = error?.name === "TimeoutError" ? "didn't answer in time" : "can't be reached";
|
|
115
|
+
throw new AgentError(`Tangenta at ${new URL(url).origin} ${reason}.`);
|
|
116
|
+
}
|
|
117
|
+
let answer = null;
|
|
118
|
+
try {
|
|
119
|
+
answer = await response.json();
|
|
120
|
+
} catch {
|
|
121
|
+
answer = null;
|
|
122
|
+
}
|
|
123
|
+
if (!response.ok) {
|
|
124
|
+
const message = answer?.errors?.[0]?.message ?? `Tangenta answered HTTP ${response.status}.`;
|
|
125
|
+
throw new AgentError(redact(message));
|
|
126
|
+
}
|
|
127
|
+
return answer ?? {};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function describe(credentials) {
|
|
131
|
+
const agent = credentials.agent?.name ?? "the agent";
|
|
132
|
+
const handle = credentials.agent?.handle ? ` (@${credentials.agent.handle})` : "";
|
|
133
|
+
const workspace = credentials.workspace?.name ? ` in ${credentials.workspace.name}` : "";
|
|
134
|
+
return (
|
|
135
|
+
`${agent}${handle}${workspace} at ${credentials.apiUrl}, ${credentials.access} access, ` +
|
|
136
|
+
`key ending ${credentials.keySuffix}`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Exchanges `setupToken` at `apiUrl` (checked like an attach line's address: https, and the
|
|
142
|
+
* configured address wins) and stores the key. Answers a sentence without the key.
|
|
143
|
+
*/
|
|
144
|
+
export async function connectAgent(env, { setupToken, apiUrl }, deps = {}) {
|
|
145
|
+
const token = typeof setupToken === "string" ? setupToken.trim() : "";
|
|
146
|
+
if (!SETUP_TOKEN.test(token)) {
|
|
147
|
+
throw new AgentError(
|
|
148
|
+
"That isn't a Tangenta setup token (tng_stp_...). Copy it from the setup prompt.",
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
const address = attachApiUrl(env, typeof apiUrl === "string" && apiUrl.trim() ? apiUrl : null);
|
|
152
|
+
const answer = await post({
|
|
153
|
+
url: `${address}/agents/setup/exchange`,
|
|
154
|
+
body: { setupToken: token },
|
|
155
|
+
fetchImpl: deps.fetchImpl,
|
|
156
|
+
});
|
|
157
|
+
if (typeof answer.apiKey !== "string") throw new AgentError("Tangenta's answer had no key.");
|
|
158
|
+
const credentials = {
|
|
159
|
+
apiUrl: address,
|
|
160
|
+
apiKey: answer.apiKey,
|
|
161
|
+
keySuffix: answer.keySuffix ?? answer.apiKey.slice(-4),
|
|
162
|
+
access: answer.access,
|
|
163
|
+
agent: answer.agent ?? null,
|
|
164
|
+
workspace: answer.workspace ?? null,
|
|
165
|
+
connectedAt: new Date().toISOString(),
|
|
166
|
+
};
|
|
167
|
+
writeCredentials(env, credentials);
|
|
168
|
+
return `Connected as ${describe(credentials)}. The key is kept by the plugin.`;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Renews the stored key: Tangenta answers a new key for the same agent and access, and the old
|
|
173
|
+
* one stops working a few minutes later. Answers a sentence without either key.
|
|
174
|
+
*/
|
|
175
|
+
export async function renewAgentKey(env, deps = {}) {
|
|
176
|
+
const credentials = readCredentials(env);
|
|
177
|
+
if (!credentials) throw new AgentError(NOT_CONNECTED);
|
|
178
|
+
const answer = await post({
|
|
179
|
+
url: `${credentials.apiUrl}/agents/keys/renew`,
|
|
180
|
+
body: {},
|
|
181
|
+
apiKey: credentials.apiKey,
|
|
182
|
+
fetchImpl: deps.fetchImpl,
|
|
183
|
+
});
|
|
184
|
+
if (typeof answer.apiKey !== "string") throw new AgentError("Tangenta's answer had no key.");
|
|
185
|
+
const renewed = {
|
|
186
|
+
...credentials,
|
|
187
|
+
apiKey: answer.apiKey,
|
|
188
|
+
keySuffix: answer.keySuffix ?? answer.apiKey.slice(-4),
|
|
189
|
+
access: answer.access ?? credentials.access,
|
|
190
|
+
renewedAt: new Date().toISOString(),
|
|
191
|
+
};
|
|
192
|
+
writeCredentials(env, renewed);
|
|
193
|
+
const ends = answer.previousKeyExpiresAt
|
|
194
|
+
? ` The old key stops at ${answer.previousKeyExpiresAt}.`
|
|
195
|
+
: "";
|
|
196
|
+
return `Renewed the key of ${describe(renewed)}.${ends}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export const NOT_CONNECTED =
|
|
200
|
+
"Claude Code isn't connected to Tangenta as an agent. Paste the agent's setup prompt from its " +
|
|
201
|
+
"page in Tangenta, or run /tangenta:setup with its setup token.";
|
|
202
|
+
|
|
203
|
+
/** The stored connection as a sentence, without the key; null when there is none. */
|
|
204
|
+
export function connectionSummary(env) {
|
|
205
|
+
const credentials = readCredentials(env);
|
|
206
|
+
return credentials ? describe(credentials) : null;
|
|
207
|
+
}
|