neagen 0.1.4 → 0.1.6

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.
@@ -0,0 +1,15 @@
1
+ // Type surface for the plain-ESM formatter so root TS tests can import it.
2
+ export type ApprovalInputRequest = {
3
+ requestId: string;
4
+ prompt: string;
5
+ action?: {
6
+ kind?: string;
7
+ callId?: string;
8
+ toolName?: string;
9
+ input?: Record<string, unknown>;
10
+ };
11
+ [key: string]: unknown;
12
+ };
13
+
14
+ export function decorateApprovalRequest<T extends ApprovalInputRequest>(request: T): T;
15
+ export function decorateApprovalRequests<T>(requests: T): T;
@@ -0,0 +1,102 @@
1
+ // Human-readable approval prompts for outbound agent-mail tools. Eve's default
2
+ // approval prompt is "Approve tool call: <name>" plus raw input JSON; for the
3
+ // tools that ship Owner content to another party we replace it with a card the
4
+ // Owner can actually review (recipient, file identity, content preview).
5
+ // Presentation only: the enforcement stays server-side (signed review, egress
6
+ // gate, approval: always()).
7
+
8
+ const PREVIEW_MAX_LINES = 12;
9
+ const PREVIEW_MAX_LINE_CHARS = 100;
10
+ const BODY_MAX_CHARS = 700;
11
+ const RULE = "─".repeat(44);
12
+
13
+ function formatBytes(size) {
14
+ if (!Number.isFinite(size)) return "unknown size";
15
+ if (size < 1024) return `${size} B`;
16
+ if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KB`;
17
+ return `${(size / (1024 * 1024)).toFixed(1)} MB`;
18
+ }
19
+
20
+ function clampLine(line) {
21
+ return line.length > PREVIEW_MAX_LINE_CHARS
22
+ ? `${line.slice(0, PREVIEW_MAX_LINE_CHARS - 1)}…`
23
+ : line;
24
+ }
25
+
26
+ function previewBlock(text, { truncated = false } = {}) {
27
+ const lines = String(text).split("\n");
28
+ const shown = lines.slice(0, PREVIEW_MAX_LINES).map((line) => ` │ ${clampLine(line)}`);
29
+ const clipped = truncated || lines.length > PREVIEW_MAX_LINES;
30
+ return [
31
+ ` ${RULE}`,
32
+ ...shown,
33
+ clipped ? ` ${RULE} (preview truncated)` : ` ${RULE}`,
34
+ ];
35
+ }
36
+
37
+ function clampBody(body) {
38
+ const text = String(body);
39
+ return text.length > BODY_MAX_CHARS
40
+ ? { text: `${text.slice(0, BODY_MAX_CHARS)}…`, truncated: true }
41
+ : { text, truncated: false };
42
+ }
43
+
44
+ function formatSendFilePrompt(input) {
45
+ const review = input?.review;
46
+ if (!review || typeof review !== "object") return undefined;
47
+ const to = review.to ?? input?.to ?? "connection";
48
+ const header = [
49
+ `Send file to ${to}?`,
50
+ ` ${review.filename} · ${review.contentType} · ${formatBytes(review.size)}`,
51
+ ` sha256 ${String(review.sha256).slice(0, 16)}…`,
52
+ ];
53
+ return [
54
+ ...header,
55
+ ...previewBlock(review.preview ?? "", { truncated: review.previewTruncated === true }),
56
+ "Approve to deliver this exact file. Deny to cancel.",
57
+ ].join("\n");
58
+ }
59
+
60
+ function formatSendMessagePrompt(input) {
61
+ if (typeof input?.body !== "string") return undefined;
62
+ const { text, truncated } = clampBody(input.body);
63
+ return [
64
+ `Send message${input.to ? ` to ${input.to}` : ""}?`,
65
+ ...previewBlock(text, { truncated }),
66
+ "Approve to send. Deny to cancel.",
67
+ ].join("\n");
68
+ }
69
+
70
+ function formatApproveDraftPrompt(input) {
71
+ const lines = [`Approve and send draft${input?.draftId ? ` ${String(input.draftId).slice(0, 8)}…` : ""}?`];
72
+ if (typeof input?.body === "string") {
73
+ const { text, truncated } = clampBody(input.body);
74
+ lines.push(...previewBlock(text, { truncated }), "(revised body shown above)");
75
+ }
76
+ lines.push("Approve to send. Deny to cancel.");
77
+ return lines.join("\n");
78
+ }
79
+
80
+ const FORMATTERS = {
81
+ send_file: formatSendFilePrompt,
82
+ send_message: formatSendMessagePrompt,
83
+ approve_draft: formatApproveDraftPrompt,
84
+ };
85
+
86
+ /**
87
+ * Returns a copy of the input request with a human-readable prompt when the
88
+ * tool is one of the outbound agent-mail tools; otherwise returns it unchanged.
89
+ */
90
+ export function decorateApprovalRequest(request) {
91
+ const toolName = request?.action?.toolName;
92
+ const format = toolName ? FORMATTERS[toolName] : undefined;
93
+ if (!format) return request;
94
+ const prompt = format(request.action?.input ?? {});
95
+ return prompt ? { ...request, prompt } : request;
96
+ }
97
+
98
+ /** Rewrites prompts on any request list carried by a stream event (in place-safe copy). */
99
+ export function decorateApprovalRequests(requests) {
100
+ if (!Array.isArray(requests)) return requests;
101
+ return requests.map((request) => decorateApprovalRequest(request));
102
+ }
@@ -85,7 +85,7 @@ export async function runDeviceGrantLogin({ host, profile, renderer }) {
85
85
 
86
86
  ui.renderLine(`Your code: ${code.user_code}`, "info");
87
87
  ui.renderLine(code.verification_uri_complete, "info");
88
- ui.renderLine("Opening your browser create or sign in, then approve…", "info");
88
+ ui.renderLine("Opening your browser. Create or sign in, then approve…", "info");
89
89
  // Mirror to stderr so login is never silent if the panel hasn't painted yet.
90
90
  console.error(
91
91
  `\n Your code: ${code.user_code}\n Approve at: ${code.verification_uri_complete}\n\n` +
@@ -97,8 +97,18 @@ export async function runDeviceGrantLogin({ host, profile, renderer }) {
97
97
  const intervalMs = (code.interval ?? 5) * 1000;
98
98
  const deadline = Date.now() + (code.expires_in ?? 600) * 1000;
99
99
 
100
+ let lastReminderAt = Date.now();
100
101
  while (Date.now() < deadline) {
101
- ui.setStatus("Waiting for passkey approval…");
102
+ // Keep the code and remaining time visible so a failed browser-open never
103
+ // strands the user scrolling back for the URL.
104
+ const minutesLeft = Math.max(1, Math.ceil((deadline - Date.now()) / 60_000));
105
+ ui.setStatus(
106
+ `Waiting for passkey approval… code ${code.user_code}, ~${minutesLeft} min left`,
107
+ );
108
+ if (Date.now() - lastReminderAt >= 60_000) {
109
+ lastReminderAt = Date.now();
110
+ ui.renderLine(`Still waiting. Approve at: ${code.verification_uri_complete}`, "info");
111
+ }
102
112
  await new Promise((r) => setTimeout(r, intervalMs));
103
113
 
104
114
  const tokRes = await fetch(`${host}/api/auth/device/token`, {
@@ -129,7 +139,7 @@ export async function runDeviceGrantLogin({ host, profile, renderer }) {
129
139
  throw new Error(err.error_description ?? err.error ?? "device/token failed");
130
140
  }
131
141
 
132
- ui.renderLine("The code expired before approval. Run neagen again.", "error");
142
+ ui.renderLine("The code expired before approval. Run `neagen login` for a fresh one.", "error");
133
143
  throw new Error("device code expired");
134
144
  } finally {
135
145
  ui.setStatus(undefined);
@@ -14,6 +14,7 @@ import {
14
14
  selectModel,
15
15
  } from "./api.mjs";
16
16
  import { runDeviceGrantLogin } from "./device-login.mjs";
17
+ import { decorateApprovalRequests } from "./approval-format.mjs";
17
18
  import { parseSseChunk } from "./turn-stream.mjs";
18
19
  import { subscribeInboxWakeAuto } from "./wake.mjs";
19
20
 
@@ -98,6 +99,18 @@ function isBoundaryEvent(event) {
98
99
  event?.type === "session.failed";
99
100
  }
100
101
 
102
+ // Swap Eve's raw "Approve tool call: <name>" prompt for a reviewable card on
103
+ // the events that carry approval requests. Presentation only; see approval-format.mjs.
104
+ function decorateEventData(event, data) {
105
+ if (event === "input.requested" && Array.isArray(data?.requests)) {
106
+ return { ...data, requests: decorateApprovalRequests(data.requests) };
107
+ }
108
+ if (event === "session.waiting" && Array.isArray(data?.inputRequests)) {
109
+ return { ...data, inputRequests: decorateApprovalRequests(data.inputRequests) };
110
+ }
111
+ return data;
112
+ }
113
+
101
114
  async function* readProductTurnEvents(response, onProductEvent) {
102
115
  if (!response.ok) {
103
116
  let message = `HTTP ${response.status}`;
@@ -143,7 +156,7 @@ async function* readProductTurnEvents(response, onProductEvent) {
143
156
  return;
144
157
  }
145
158
 
146
- const eveEvent = { type: event, data };
159
+ const eveEvent = { type: event, data: decorateEventData(event, data) };
147
160
  if (isBoundaryEvent(eveEvent)) {
148
161
  heldBoundary = eveEvent;
149
162
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neagen",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Your personal networking agent, in the terminal. Chat, agent-mail, and files against your Neagen — passkey login, no passwords.",
5
5
  "type": "module",
6
6
  "bin": {