xonecas.io
64875
Mc

mini-coder / commit 6f18835

fix: use real auth file/home dir

xonecas2026-04-23 6f188352b22bee0c8b059d31876d12cec0fcfbb1 parent a5bec0c

6 files changed

bin/mc.ts+1−0
@@ -1,3 +1,4 @@
1+#!/usr/bin/env bun
12 import { main } from "../src/index.ts";
23
34 if (import.meta.main) {
src/agent.ts+1−1
@@ -60,7 +60,7 @@ export async function streamAgent(
6060 for (const call of toolCalls) {
6161 const toolDef = tools.find((i) => i.tool.name === call.name);
6262 const result = (await toolDef?.runner(call.arguments)) || "";
63- const msg: Message = {
63+ const msg: ToolResultMessage = {
6464 role: "toolResult",
6565 toolCallId: call.id,
6666 toolName: call.name,
src/oauth.ts+6−3
@@ -5,9 +5,10 @@ import {
55 loginOpenAICodex,
66 type OAuthProviderId,
77 } from "@mariozechner/pi-ai/oauth";
8+import { AUTH_PATH as AUTH_FILE } from "./shared";
89 import type { CliOptions, SavedOAuthCreds } from "./types";
910
10-const AUTH_FILE = "./.auth.json";
11+// const AUTH_FILE = "./.auth.json";
1112
1213 export async function loginOAuth(provider: OAuthProviderId) {
1314 const rl = readline.createInterface({
@@ -56,9 +57,11 @@ export async function getApiKey(options: CliOptions) {
5657
5758 export async function readCreds() {
5859 const file = Bun.file(AUTH_FILE);
59- const creds: SavedOAuthCreds = JSON.parse(await file.text());
60+ if (await file.exists()) {
61+ return JSON.parse(await file.text());
62+ }
6063
61- return creds;
64+ return {};
6265 }
6366
6467 async function writeCreds(creds: SavedOAuthCreds) {
src/shared.ts+11−0
@@ -1,3 +1,10 @@
1+import { homedir } from "node:os";
2+import { join } from "node:path";
3+
4+export const DATA_DIR = join(homedir(), ".config", "mini-coder");
5+export const AUTH_PATH = join(DATA_DIR, "auth.json");
6+export const SETTINGS_PATH = join(DATA_DIR, "settings.json");
7+
18 export function secureRandomString(
29 length: number,
310 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
@@ -62,3 +69,7 @@ export function onceEvery<T extends unknown[]>(
6269 }
6370 };
6471 }
72+
73+export function takeTail<T>(arr: T[], x: number): T[] {
74+ return x <= 0 ? [] : arr.slice(-x);
75+}
src/tui-conversation.ts+44−28
@@ -2,9 +2,47 @@ import { cel, HStack, Text, VStack } from "@cel-tui/core";
22 import { elapsedTime, relativeTime } from "./shared";
33 import { TextPill, theme } from "./tui-components";
44 import { memoizedSyntaxHighlight } from "./tui-syntax-highlight";
5-import type { TUIState } from "./types";
5+import type { TUIMessage, TUIState } from "./types";
6+
7+function agentMessageNode(msg: TUIMessage) {
8+ return memoizedSyntaxHighlight(
9+ (msg.id ? msg.id : "") + String(msg.timestamp),
10+ msg.content,
11+ "markdown",
12+ );
13+}
14+
15+function userMessageNode(msg: TUIMessage) {
16+ return VStack(
17+ {
18+ padding: { x: 1, y: 1 },
19+ bgColor: theme.bblack,
20+ fgColor: theme.white,
21+ },
22+ [
23+ memoizedSyntaxHighlight(
24+ msg.id ?? String(msg.timestamp),
25+ msg.content,
26+ "markdown",
27+ ),
28+ ],
29+ );
30+}
31+
32+function toolMessageNode(msg: TUIMessage) {
33+ return VStack({ gap: 1 }, [
34+ memoizedSyntaxHighlight(
35+ msg.id ?? String(msg.timestamp),
36+ msg.header ?? "",
37+ "bash",
38+ ),
39+ Text(msg.content, { wrap: "word", fgColor: theme.bblack }),
40+ ]);
41+}
642
743 export function Conversation(state: TUIState) {
44+ // TODO: use scroll callbacks to make the conversation a virtual list,
45+ // only showing a 12 messages window based the scroll position.
846 return VStack(
947 {
1048 flex: 1,
@@ -22,38 +60,16 @@ export function Conversation(state: TUIState) {
2260 const dur = msg.durationMs
2361 ? `Took ${elapsedTime(msg.durationMs / 1000)}`
2462 : "-";
63+
2564 return VStack({ gap: 1 }, [
2665 msg.role === "agent"
27- ? memoizedSyntaxHighlight(
28- (msg.id ? msg.id : "") + String(msg.timestamp),
29- msg.content,
30- "markdown",
31- )
66+ ? agentMessageNode(msg)
3267 : msg.role === "user"
33- ? VStack(
34- {
35- padding: { x: 1, y: 1 },
36- bgColor: theme.bblack,
37- fgColor: theme.white,
38- },
39- [
40- memoizedSyntaxHighlight(
41- msg.id ?? String(msg.timestamp),
42- msg.content,
43- "markdown",
44- ),
45- ],
46- )
68+ ? userMessageNode(msg)
4769 : msg.role === "tool"
48- ? VStack({ gap: 1 }, [
49- memoizedSyntaxHighlight(
50- msg.id ?? String(msg.timestamp),
51- msg.header ?? "",
52- "bash",
53- ),
54- Text(msg.content, { wrap: "word", fgColor: theme.bwhite }),
55- ])
70+ ? toolMessageNode(msg)
5671 : Text(msg.content, { wrap: "word", fgColor: theme.bwhite }),
72+
5773 HStack({ gap: 1, justifyContent: "end" }, [
5874 Text(`${relativeTime(msg.timestamp)} ago.`, {
5975 fgColor: theme.bblack,
src/tui.ts+63−44
@@ -1,9 +1,9 @@
11 import { basename } from "node:path";
22 import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
3-import type { Message } from "@mariozechner/pi-ai";
3+import type { Message, ToolCall, ToolResultMessage } from "@mariozechner/pi-ai";
44 import { streamAgent, TASK_PROMPT } from "./agent";
55 import { getApiKey } from "./oauth";
6-import { onceEvery, secureRandomString } from "./shared";
6+import { onceEvery, secureRandomString, takeTail } from "./shared";
77 import { bash, runBashTool } from "./tool-bash";
88 import { TextPill, theme } from "./tui-components";
99 import { Conversation } from "./tui-conversation";
@@ -83,6 +83,60 @@ export function initTUI(state: TUIState, leave: (s: string) => void) {
8383 );
8484 }
8585
86+// Create the TUIState message for context tool call and result.
87+function createTUIToolMessage(
88+ source: ToolCall | ToolResultMessage,
89+ existing?: Partial<TUIMessage>,
90+): TUIMessage {
91+ // This truncation is TUI only. The file is also
92+ // truncated at tool level to avoid big files being
93+ // sent to the llm. We truncate that even further for the user.
94+ // We only show the tail of the file, which includes if the file
95+ // was truncated at tool level to the user.
96+
97+ // Join text if there is more than one block.
98+ const showLines = 6;
99+ const content = "content" in source ? source.content : [];
100+ const text = content.length
101+ ? content
102+ .map((c) => (c.type === "text" ? c.text : ""))
103+ .filter((c) => Boolean(c))
104+ .join("\n")
105+ : (existing?.content ?? "");
106+ // Grab the tail
107+ let tail = takeTail(text.split("\n"), showLines).join("\n");
108+ // Some commands output without newlines tons of chars.
109+ // if needed to take roughly 6 lines at 100 chars worth of tail.
110+ if (tail.length > showLines * 100) {
111+ tail = tail.slice(showLines * 100 * -1);
112+ }
113+
114+ // Now the similar cut is needed in arguments, but here we care about
115+ // seeing the start of the command, like `cd bla/ && cat ...`
116+ const argsMaxLength = 600; // estimaded by 100 char line width x 10 lines.
117+ let args = existing?.header ?? "Writting...";
118+ if ("arguments" in source && "command" in source.arguments) {
119+ args =
120+ source.arguments?.command?.length > argsMaxLength
121+ ? source.arguments.command.substring(0, argsMaxLength)
122+ : source.arguments.command;
123+ }
124+
125+ const msg: TUIMessage = {
126+ id: "id" in source ? source.id : (existing?.id ?? secureRandomString(8)),
127+ timestamp:
128+ "timestamp" in source
129+ ? source.timestamp
130+ : (existing?.timestamp ?? Date.now()),
131+ role: "tool",
132+ label: "name" in source ? source.name : (existing?.label ?? ""),
133+ header: args,
134+ content: tail,
135+ durationMs: existing?.durationMs ? existing.durationMs : 0,
136+ };
137+ return msg;
138+}
139+
86140 export async function streamTUI(state: TUIState) {
87141 if (state.streaming) return;
88142 state.streaming = true;
@@ -130,17 +184,9 @@ export async function streamTUI(state: TUIState) {
130184 );
131185
132186 for (const call of newToolCalls) {
133- const toolMessage: TUIMessage = {
134- role: "tool",
135- label: call.name.toLocaleUpperCase(),
136- header: call.arguments.command
137- ? call.arguments.command
138- : `Calling ${call.name}...`,
139- content: "",
140- id: call.id,
141- timestamp: Date.now(),
187+ const toolMessage = createTUIToolMessage(call, {
142188 durationMs: dur,
143- };
189+ });
144190 let found = false;
145191 state.messages = state.messages.map((msg) => {
146192 if (call.id === msg.id) {
@@ -166,27 +212,18 @@ export async function streamTUI(state: TUIState) {
166212
167213 // Update tui messages with arguments
168214 for (const call of finishedToolCalls) {
169- const toolMessage: TUIMessage = {
170- role: "tool",
171- label: call.name.toLocaleUpperCase(),
172- header: call.arguments.command
173- ? call.arguments.command
174- : `"Calling ${call.name}..."`,
175- content: "",
176- id: call.id,
177- timestamp: Date.now(),
178- durationMs: dur,
179- };
180215 let updated = false;
181216 state.messages = state.messages.map((msg) => {
182217 if (call.id === msg.id) {
183218 updated = true;
184- return toolMessage;
219+ return createTUIToolMessage(call, { ...msg, durationMs: dur });
185220 }
186221 return msg;
187222 });
188223 if (!updated) {
189- state.messages.push(toolMessage);
224+ state.messages.push(
225+ createTUIToolMessage(call, { durationMs: dur }),
226+ );
190227 }
191228 }
192229
@@ -209,25 +246,7 @@ export async function streamTUI(state: TUIState) {
209246 // Append the output to the existing TUI messages
210247 state.messages = state.messages.map((msg) => {
211248 if (tool.toolCallId === msg.id) {
212- // This truncation is TUI only. The file is also
213- // truncated at tool level to avoid big files being
214- // sent to the llm. We truncate that even further.
215- const truncateLimit = 1000;
216- const result = tool.content
217- .map((c) => (c.type === "text" ? c.text : ""))
218- .filter((c) => Boolean(c))
219- .join("\n");
220- const truncated =
221- result.length > truncateLimit
222- ? `${result.substring(0, truncateLimit)}...\n\nTruncated at ${truncateLimit} of ${result.length} chars`
223- : result;
224-
225- return {
226- ...msg,
227- content: truncated,
228- timestamp: Date.now(),
229- durationMs: dur,
230- };
249+ return createTUIToolMessage(tool, { ...msg, durationMs: dur });
231250 }
232251 return msg;
233252 });