@a-tether/server 0.1.1 → 0.1.2

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.
@@ -1,12 +1,28 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { createInterface } from "node:readline";
3
3
  import { randomUUID } from "node:crypto";
4
+ import { readFileSync } from "node:fs";
5
+ import { homedir } from "node:os";
6
+ import { join } from "node:path";
4
7
  import { getBuiltinModels } from "@earendil-works/pi-ai/providers/all";
5
8
  import { api, providerId, defaultUrl } from "../../shared/protocol.js";
6
9
  import { loadSettings } from "../../shared/settings.js";
7
10
  export const isolated = ["-p", "--tools", "", "--setting-sources", "", "--strict-mcp-config",
8
11
  "--settings", JSON.stringify({ disableAllHooks: true }), "--disable-slash-commands", "--no-session-persistence", "--permission-mode", "dontAsk"];
9
12
  export const streaming = ["--verbose", "--input-format", "stream-json", "--output-format", "stream-json"];
13
+ // Turns run with --setting-sources "", so Claude Code ignores the env block of its own
14
+ // settings.json, while `claude auth status` reads it. A login token kept there must
15
+ // reach every invocation, or status reports logged in and each turn fails.
16
+ function settingsToken(env) {
17
+ try {
18
+ const file = join(env.CLAUDE_CONFIG_DIR || join(homedir(), ".claude"), "settings.json");
19
+ const token = JSON.parse(readFileSync(file, "utf8"))?.env?.CLAUDE_CODE_OAUTH_TOKEN;
20
+ return typeof token === "string" && token ? token : undefined;
21
+ }
22
+ catch {
23
+ return undefined;
24
+ }
25
+ }
10
26
  export async function invoke(args, options) {
11
27
  const { signal } = options;
12
28
  signal?.throwIfAborted();
@@ -18,6 +34,9 @@ export async function invoke(args, options) {
18
34
  ["CLAUDE_CODE_USE_BEDROCK", "CLAUDE_CODE_USE_VERTEX", "CLAUDE_CODE_USE_FOUNDRY"].some(k => env[k] && !["false", "0"].includes(env[k]))) {
19
35
  throw new Error("Conflicting backend configuration");
20
36
  }
37
+ const oauth = env.CLAUDE_CODE_OAUTH_TOKEN || settingsToken(env);
38
+ if (oauth)
39
+ env.CLAUDE_CODE_OAUTH_TOKEN = oauth;
21
40
  Object.assign(env, { ENABLE_TOOL_SEARCH: "false", CLAUDE_CODE_DISABLE_FILE_MENTIONS: "1",
22
41
  CLAUDE_CODE_DISABLE_AUTO_MEMORY: "1", CLAUDE_CODE_DISABLE_CLAUDE_MDS: "1", DISABLE_AUTOUPDATER: "1",
23
42
  CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1", DISABLE_TELEMETRY: "1" });
@@ -7,7 +7,9 @@ import { invoke, isolated, streaming } from "./claude-process.js";
7
7
  import { loadSettings } from "../../shared/settings.js";
8
8
  // Present the conversation in order, retaining call IDs and images. Do not forward
9
9
  // provider diagnostics, local tool details, or session bookkeeping to Claude.
10
- function userInput(context) {
10
+ // History names tools the way Claude must call them, so the model does not copy
11
+ // Pi's bare names into new calls.
12
+ function userInput(context, toolName) {
11
13
  const blocks = [];
12
14
  for (const msg of context.messages) {
13
15
  if (msg.role === "system")
@@ -18,10 +20,12 @@ function userInput(context) {
18
20
  return [{ type: "image", attachment: true }];
19
21
  if (p.type === "thinking")
20
22
  return [];
23
+ if (p.type === "toolCall")
24
+ return [{ ...p, name: toolName(p.name) }];
21
25
  return [p];
22
26
  });
23
27
  const record = msg.role === "toolResult"
24
- ? { role: msg.role, toolCallId: msg.toolCallId, name: msg.toolName, isError: msg.isError, content }
28
+ ? { role: msg.role, toolCallId: msg.toolCallId, name: toolName(msg.toolName), isError: msg.isError, content }
25
29
  : { role: msg.role, content };
26
30
  blocks.push({ type: "text", text: JSON.stringify(record) });
27
31
  for (const p of parts)
@@ -53,7 +57,12 @@ export function streamClaudeCode(model, context, options, route = model.id) {
53
57
  name: transportName(tool.name, index),
54
58
  description: tool.description, inputSchema: tool.parameters,
55
59
  }));
56
- const names = new Map(catalog.map((tool, i) => ["mcp__tether__" + tool.name, tools[i].name]));
60
+ const claudeNames = new Map(tools.map((tool, i) => [tool.name, "mcp__tether__" + catalog[i].name]));
61
+ // A model sometimes drops the MCP prefix. Map bare names back as well; any other
62
+ // name goes to Pi unchanged, which answers it with a "not found" tool result.
63
+ const names = new Map(tools.flatMap((tool, i) => [
64
+ [tool.name, tool.name], [catalog[i].name, tool.name], ["mcp__tether__" + catalog[i].name, tool.name]
65
+ ]));
57
66
  const dir = await mkdtemp(join(tmpdir(), "a-tether-turn-"));
58
67
  try {
59
68
  const file = join(dir, "catalog.json");
@@ -106,7 +115,8 @@ export function streamClaudeCode(model, context, options, route = model.id) {
106
115
  throw new Error("Claude final content differs from stream");
107
116
  }
108
117
  stream.push({ type: "start", partial: message });
109
- const { code, stderr } = await invoke(args, { signal: options.signal, cwd: dir, input: userInput(context),
118
+ const input = userInput(context, name => claudeNames.get(name) ?? name);
119
+ const { code, stderr } = await invoke(args, { signal: options.signal, cwd: dir, input,
110
120
  env: options.maxTokens ? { CLAUDE_CODE_MAX_OUTPUT_TOKENS: String(options.maxTokens) } : {}, onLine(line) {
111
121
  let envelope;
112
122
  try {
@@ -176,15 +186,16 @@ export function streamClaudeCode(model, context, options, route = model.id) {
176
186
  }
177
187
  if (b.type !== "tool_use")
178
188
  continue;
179
- const name = names.get(b.name);
180
- if (!name)
181
- throw new Error("Unexpected Claude tool");
182
- if (typeof b.id !== "string" || !b.input || typeof b.input !== "object" || Array.isArray(b.input))
189
+ if (typeof b.id !== "string" || typeof b.name !== "string")
183
190
  throw new Error("Invalid tool call");
184
191
  if (callsSeen.has(b.id))
185
192
  continue;
186
193
  callsSeen.add(b.id);
187
- const toolCall = { type: "toolCall", id: b.id, name, arguments: b.input };
194
+ // Pi owns the tools: it rejects unknown names and validates arguments,
195
+ // and the model corrects itself on the next turn.
196
+ const name = names.get(b.name) ?? b.name;
197
+ const input = b.input && typeof b.input === "object" && !Array.isArray(b.input) ? b.input : {};
198
+ const toolCall = { type: "toolCall", id: b.id, name, arguments: input };
188
199
  const i = message.content.push(toolCall) - 1;
189
200
  stream.push({ type: "toolcall_start", contentIndex: i, partial: message });
190
201
  stream.push({ type: "toolcall_end", contentIndex: i, toolCall, partial: message });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@a-tether/server",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A-tether C-side service: lends a logged-in Claude Code to Pi on another machine over an SSH tunnel.",
5
5
  "type": "module",
6
6
  "license": "MIT",