@anthonyhaussman/opencode-agy-auth 1.5.20-test.0 → 1.5.20-test.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.
package/dist/index.js CHANGED
@@ -517,6 +517,7 @@ function canonical(value) {
517
517
  }
518
518
  return JSON.stringify(value) ?? "null";
519
519
  }
520
+ var SIDE_CALL_TOOL_TEXT = "OpenCode tools are not available in this request. Answer with text only, without calling tools.";
520
521
  var PendingTools = class {
521
522
  constructor(opts) {
522
523
  this.opts = opts;
@@ -648,6 +649,7 @@ var McpBridge = class _McpBridge {
648
649
  url = "";
649
650
  static async start(opts) {
650
651
  const { tools, pending, log } = opts;
652
+ const callable = opts.callable ?? true;
651
653
  const ajv = new Ajv({ strict: false, allErrors: true });
652
654
  const validators = /* @__PURE__ */ new Map();
653
655
  for (const tool of tools) {
@@ -679,6 +681,7 @@ var McpBridge = class _McpBridge {
679
681
  mcp.setRequestHandler(CallToolRequestSchema, async (request) => {
680
682
  const name = request.params.name;
681
683
  if (!tools.some((t) => t.name === name)) return errorResult(`Unknown OpenCode tool: ${name}`);
684
+ if (!callable) return errorResult(SIDE_CALL_TOOL_TEXT);
682
685
  const input = stripToolSummary(request.params.arguments ?? {});
683
686
  const validate = validators.get(name);
684
687
  if (validate && !validate(input)) {
@@ -778,6 +781,11 @@ var McpBridge = class _McpBridge {
778
781
  }
779
782
  };
780
783
 
784
+ // src/acp/permission.ts
785
+ import { realpathSync, statSync } from "fs";
786
+ import os2 from "os";
787
+ import path4 from "path";
788
+
781
789
  // src/acp/stream-map.ts
782
790
  var TOOL_SUMMARY_KEY2 = "toolSummary";
783
791
  function isRecord(v) {
@@ -855,6 +863,25 @@ var StreamMapper = class {
855
863
  flush() {
856
864
  return this.close();
857
865
  }
866
+ /**
867
+ * Ends the turn: closes the open text block and gives every provider-executed
868
+ * call agy never finished an error result, so OpenCode does not report a
869
+ * missing tool result (agy can drop the update after a denied or aborted call).
870
+ */
871
+ finish() {
872
+ const parts = this.close();
873
+ for (const [toolCallId, toolName] of this.providerCalls) {
874
+ parts.push({
875
+ type: "tool-result",
876
+ toolCallId,
877
+ toolName,
878
+ result: `agy did not report a result for ${toolName}`,
879
+ isError: true
880
+ });
881
+ }
882
+ this.providerCalls.clear();
883
+ return parts;
884
+ }
858
885
  close() {
859
886
  const open = this.open;
860
887
  if (!open) return [];
@@ -956,8 +983,37 @@ function mapUsage(u) {
956
983
  // src/acp/permission.ts
957
984
  var ALLOW_KINDS = ["allow_once", "allow_always"];
958
985
  var REJECT_KINDS = ["reject_once", "reject_always"];
959
- function decidePermission(req) {
960
- const allowed = classifyToolCall(req.toolCall ?? { toolCallId: "" }).kind === "bridge";
986
+ var PATH_KEYS = ["Path", "AbsolutePath", "path", "absolutePath", "file_path"];
987
+ function openCodeToolOutputDir(env = process.env) {
988
+ const base = env.XDG_DATA_HOME || path4.join(env.HOME || os2.homedir(), ".local/share");
989
+ return path4.join(base, "opencode", "tool-output");
990
+ }
991
+ function openCodeShellOutputDir(env = process.env) {
992
+ return path4.join(path4.dirname(openCodeToolOutputDir(env)), "shell");
993
+ }
994
+ function isInside(dir, file) {
995
+ try {
996
+ return file.startsWith(realpathSync(dir) + path4.sep);
997
+ } catch {
998
+ return false;
999
+ }
1000
+ }
1001
+ function isToolOutputRead(toolCall, env) {
1002
+ if (toolCall.name !== "read_file") return false;
1003
+ const input = toolCall.rawInput;
1004
+ const target = PATH_KEYS.map((k) => input?.[k]).find((v) => typeof v === "string");
1005
+ if (!target || !path4.isAbsolute(target)) return false;
1006
+ try {
1007
+ const file = realpathSync(target);
1008
+ const dirs = [openCodeToolOutputDir(env), openCodeShellOutputDir(env)];
1009
+ return dirs.some((dir) => isInside(dir, file)) && statSync(file).isFile();
1010
+ } catch {
1011
+ return false;
1012
+ }
1013
+ }
1014
+ function decidePermission(req, env = process.env) {
1015
+ const toolCall = req.toolCall ?? { toolCallId: "" };
1016
+ const allowed = classifyToolCall(toolCall).kind === "bridge" || isToolOutputRead(toolCall, env);
961
1017
  const options = req.options ?? [];
962
1018
  for (const kind of allowed ? ALLOW_KINDS : REJECT_KINDS) {
963
1019
  const option = options.find((o) => o.kind === kind);
@@ -972,18 +1028,18 @@ import { PassThrough } from "stream";
972
1028
 
973
1029
  // src/acp/debug.ts
974
1030
  import { appendFileSync, mkdirSync } from "fs";
975
- import path4 from "path";
1031
+ import path5 from "path";
976
1032
  var AGY_ACP_DEBUG_ENV = "AGY_ACP_DEBUG";
977
1033
  var AGY_ACP_DEBUG_FILE_ENV = "AGY_ACP_DEBUG_FILE";
978
1034
  function createAgyDebugLog(env = process.env, secrets = []) {
979
1035
  if (env[AGY_ACP_DEBUG_ENV] !== "1") return void 0;
980
- const file = env[AGY_ACP_DEBUG_FILE_ENV] || path4.join(agyAcpDataDir(env), "debug.log");
1036
+ const file = env[AGY_ACP_DEBUG_FILE_ENV] || path5.join(agyAcpDataDir(env), "debug.log");
981
1037
  const hidden = secrets.filter((s) => typeof s === "string" && s.length > 0);
982
1038
  return (event, detail) => {
983
1039
  try {
984
1040
  let line = detail === void 0 ? event : `${event} ${detail}`;
985
1041
  for (const secret of hidden) line = line.split(secret).join("[redacted]");
986
- mkdirSync(path4.dirname(file), { recursive: true, mode: 448 });
1042
+ mkdirSync(path5.dirname(file), { recursive: true, mode: 448 });
987
1043
  appendFileSync(file, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
988
1044
  `, { mode: 384 });
989
1045
  } catch {
@@ -1356,7 +1412,12 @@ var LiveSession = class _LiveSession {
1356
1412
  softTimeoutMs: deps.softTimeoutMs ?? MCP_SOFT_TIMEOUT_MS,
1357
1413
  onClaim: (id) => live?.markClaimed(id)
1358
1414
  });
1359
- const bridge = await McpBridge.start({ tools: config.tools, pending, log: (m) => deps.log("warn", m) });
1415
+ const bridge = await McpBridge.start({
1416
+ tools: config.tools,
1417
+ pending,
1418
+ log: (m) => deps.log("warn", m),
1419
+ callable: !config.sideCall
1420
+ });
1360
1421
  let proc;
1361
1422
  try {
1362
1423
  proc = await AgyAcpProcess.start({
@@ -1375,7 +1436,7 @@ var LiveSession = class _LiveSession {
1375
1436
  const models = config.resumeId ? await live.resumeOrOpen(config.resumeId) : await live.openSession();
1376
1437
  deps.onAuthStatus?.(void 0);
1377
1438
  deps.onModels?.(models);
1378
- if (config.tools.length > 0) {
1439
+ if (config.tools.length > 0 && !config.sideCall) {
1379
1440
  const connected = await bridge.waitForClient(deps.bridgeConnectTimeoutMs ?? BRIDGE_CONNECT_TIMEOUT_MS);
1380
1441
  if (!connected) throw new AgyAcpError("bridge_unreachable", BRIDGE_NOT_CONNECTED_TEXT);
1381
1442
  }
@@ -1423,10 +1484,10 @@ var LiveSession = class _LiveSession {
1423
1484
  const id = req.toolCall?.toolCallId;
1424
1485
  const toolCall = { ...this.toolCalls.get(id), ...definedOnly(req.toolCall) };
1425
1486
  const { response, allowed } = decidePermission({ ...req, toolCall });
1426
- if (allowed) {
1487
+ if (allowed && classifyToolCall(toolCall).kind === "bridge" && !this.config.sideCall) {
1427
1488
  this.turn?.parked.add(id);
1428
1489
  this.turn?.signal.fire();
1429
- } else {
1490
+ } else if (!allowed) {
1430
1491
  this.log("warn", `agy-acp: rejected agy tool ${toolCall.name ?? "(unknown)"} (call ${id})`);
1431
1492
  }
1432
1493
  return response;
@@ -1447,6 +1508,10 @@ var LiveSession = class _LiveSession {
1447
1508
  }
1448
1509
  if (u.sessionUpdate === "usage_update" && typeof u.used === "number") turn.contextUsed = u.used;
1449
1510
  const ev = turn.mapper.map(u);
1511
+ if (ev.bridgeCall && this.config.sideCall) {
1512
+ ev.parts = ev.parts.filter((p) => !(p.type === "tool-call" && p.toolCallId === ev.bridgeCall.callId));
1513
+ ev.bridgeCall = void 0;
1514
+ }
1450
1515
  if (ev.bridgeCall) {
1451
1516
  this.pending.expect(ev.bridgeCall);
1452
1517
  this.callOrder.push(ev.bridgeCall.callId);
@@ -1537,12 +1602,12 @@ async function lockSession(owner, key, signal) {
1537
1602
  // src/acp/session-sync.ts
1538
1603
  import { createHash, randomBytes as randomBytes2 } from "crypto";
1539
1604
  import fs from "fs/promises";
1540
- import path5 from "path";
1605
+ import path6 from "path";
1541
1606
  var MAX_AGE_MS = 30 * 24 * 60 * 60 * 1e3;
1542
1607
  var SessionStore = class {
1543
1608
  filePath;
1544
1609
  chain = Promise.resolve();
1545
- constructor(filePath = path5.join(agyAcpDataDir(), "sessions.json")) {
1610
+ constructor(filePath = path6.join(agyAcpDataDir(), "sessions.json")) {
1546
1611
  this.filePath = filePath;
1547
1612
  }
1548
1613
  async load() {
@@ -1561,9 +1626,9 @@ var SessionStore = class {
1561
1626
  for (const [id, r] of Object.entries(data)) {
1562
1627
  if (typeof r?.updatedAt === "number" && r.updatedAt >= cutoff) kept[id] = r;
1563
1628
  }
1564
- const dir = path5.dirname(this.filePath);
1629
+ const dir = path6.dirname(this.filePath);
1565
1630
  await fs.mkdir(dir, { recursive: true, mode: 448 });
1566
- const tmp = path5.join(dir, `.sessions-${process.pid}-${randomBytes2(6).toString("hex")}.tmp`);
1631
+ const tmp = path6.join(dir, `.sessions-${process.pid}-${randomBytes2(6).toString("hex")}.tmp`);
1567
1632
  try {
1568
1633
  await fs.writeFile(tmp, JSON.stringify(kept), { mode: 384 });
1569
1634
  await fs.rename(tmp, this.filePath);
@@ -1813,6 +1878,7 @@ var Out = class {
1813
1878
  this.controller.close();
1814
1879
  }
1815
1880
  };
1881
+ var SIDE_CALL_SYSTEM_TEXT = "This request has no tools: do not call tools, look up tool schemas or read files. Reply with text only, in exactly the format the instructions above ask for.";
1816
1882
  function headerValue(headers, name) {
1817
1883
  for (const [k, v] of Object.entries(headers ?? {})) if (k.toLowerCase() === name && v) return v;
1818
1884
  return void 0;
@@ -1959,6 +2025,7 @@ var AgyAcpLanguageModel = class {
1959
2025
  const { deps } = this;
1960
2026
  const auth = await deps.auth();
1961
2027
  const fingerprint = fingerprintOf(ctx.system, ctx.tools, auth, this.modelId, ctx.tier);
2028
+ if (ctx.tools.length > 0) this.deps.lastTools = ctx.tools;
1962
2029
  for (; ; ) {
1963
2030
  if (deps.pool.get(key)?.dead) await deps.pool.release(key);
1964
2031
  let created = false;
@@ -2012,11 +2079,14 @@ var AgyAcpLanguageModel = class {
2012
2079
  }
2013
2080
  async runThrowaway(o, ctx, out) {
2014
2081
  const auth = await this.deps.auth();
2082
+ const tools = ctx.tools.length > 0 ? ctx.tools : this.deps.lastTools ?? [];
2083
+ const sideCallSystem = [ctx.system, SIDE_CALL_SYSTEM_TEXT].filter(Boolean).join("\n\n");
2015
2084
  const live = await LiveSession.create(this.deps, {
2016
2085
  key: void 0,
2017
2086
  fingerprint: "",
2018
- tools: [],
2019
- session: this.sessionParams(ctx, []),
2087
+ tools,
2088
+ sideCall: true,
2089
+ session: this.sessionParams({ ...ctx, system: sideCallSystem }, []),
2020
2090
  auth
2021
2091
  });
2022
2092
  try {
@@ -2134,11 +2204,11 @@ var AgyAcpLanguageModel = class {
2134
2204
  }
2135
2205
  if (turn.aborted !== void 0) return { kind: "aborted" };
2136
2206
  if (emitted.length > 0 && (turn.settled || emitted.every((id) => turn.parked.has(id)))) {
2137
- out.emit(...turn.mapper.flush());
2207
+ out.emit(...turn.settled ? turn.mapper.finish() : turn.mapper.flush());
2138
2208
  return { kind: "tool-calls" };
2139
2209
  }
2140
2210
  if (turn.settled) {
2141
- out.emit(...turn.mapper.flush());
2211
+ out.emit(...turn.mapper.finish());
2142
2212
  return { kind: "settled" };
2143
2213
  }
2144
2214
  await turn.signal.wait();