xonecas.io
64875
Mc

mini-coder / commit fc07f4b

refactor: deduplicate isRecord, stripAnsi, normalizeStringId and simplify patterns

- Consolidate 5 private isRecord copies into single import from history/shared.ts
- Replace status-bar's private stripAnsi with import from internal/ansi.ts (with early-exit optimization added)
- Merge identical normalizeToolCallId/normalizeTextPartId into normalizeStringId
- Simplify subagent tool wrapper (remove unnecessary async/await/variable)
- Simplify conditional spreads in buildAgentOptions

xonecas2026-03-18 fc07f4be6de7b0054b3a2d742543ada42e6cb5f9 parent 1f8413f

9 files changed

src/agent/tools.ts+4−4
@@ -42,10 +42,10 @@ export function buildToolSet(opts: {
4242 shell as ToolDef,
4343 withCwdDefault(listSkillsTool as ToolDef, cwd),
4444 withCwdDefault(readSkillTool as ToolDef, cwd),
45- createSubagentTool(async (prompt, agentName) => {
46- const output = await opts.runSubagent(prompt, agentName, undefined);
47- return output;
48- }, opts.availableAgents) as ToolDef,
45+ createSubagentTool(
46+ (prompt, agentName) => opts.runSubagent(prompt, agentName, undefined),
47+ opts.availableAgents,
48+ ) as ToolDef,
4949 ];
5050
5151 if (process.env.EXA_API_KEY) {
src/cli/status-bar.ts+1−9
@@ -1,18 +1,10 @@
11 import * as c from "yoctocolors";
22 import type { StatusBarData } from "../agent/reporter.ts";
3+import { stripAnsi } from "../internal/ansi.ts";
34 import { terminal } from "./terminal-io.ts";
45
5-const ANSI_ESCAPE = "\u001b";
66 const STATUS_SEP = c.dim(" · ");
77
8-function stripAnsi(s: string): string {
9- if (!s.includes(ANSI_ESCAPE)) return s;
10- return s
11- .split(ANSI_ESCAPE)
12- .map((chunk, idx) => (idx === 0 ? chunk : chunk.replace(/^\[[0-9;]*m/, "")))
13- .join("");
14-}
15-
168 function truncatePlainText(value: string, maxLen: number): string {
179 if (value.length <= maxLen) return value;
1810 if (maxLen <= 1) return "…";
src/index.ts+5−5
@@ -81,11 +81,11 @@ function buildAgentOptions(opts: {
8181 initialOpenAIPromptCacheRetention: getPreferredOpenAIPromptCacheRetention(),
8282 initialGoogleCachedContent: getPreferredGoogleCachedContent(),
8383 reporter: opts.reporter,
84- ...(opts.sessionId ? { sessionId: opts.sessionId } : {}),
85- ...(opts.headless ? { headless: true } : {}),
86- ...(opts.agentSystemPrompt
87- ? { agentSystemPrompt: opts.agentSystemPrompt }
88- : {}),
84+ ...(opts.sessionId !== undefined && { sessionId: opts.sessionId }),
85+ ...(opts.headless !== undefined && { headless: opts.headless }),
86+ ...(opts.agentSystemPrompt !== undefined && {
87+ agentSystemPrompt: opts.agentSystemPrompt,
88+ }),
8989 };
9090 }
9191
src/internal/ansi.ts+3−0
@@ -1,7 +1,10 @@
11 /** Pre-compiled regex for ANSI color/style escape sequences. */
22 const ANSI_REGEX = new RegExp(`${String.fromCharCode(0x1b)}\\[[0-9;]*m`, "g");
33
4+const ANSI_ESCAPE = "\u001b";
5+
46 /** Strip ANSI color/style escape sequences from a string. */
57 export function stripAnsi(text: string): string {
8+ if (!text.includes(ANSI_ESCAPE)) return text;
69 return text.replace(ANSI_REGEX, "");
710 }
src/llm-api/model-info-fetch.ts+1−4
@@ -1,4 +1,5 @@
11 import { getAccessToken, isLoggedIn } from "../oauth/auth-storage.ts";
2+import { isRecord } from "./history/shared.ts";
23
34 const ZEN_BASE = "https://opencode.ai/zen/v1";
45 const OPENAI_BASE = "https://api.openai.com";
@@ -13,10 +14,6 @@ export interface ProviderModelCandidate {
1314 free: boolean;
1415 }
1516
16-function isRecord(value: unknown): value is Record<string, unknown> {
17- return typeof value === "object" && value !== null;
18-}
19-
2017 function normalizeModelId(modelId: string): string {
2118 let out = modelId.trim().toLowerCase();
2219 while (out.startsWith("models/")) {
src/llm-api/model-info-normalize.ts+1−4
@@ -1,4 +1,5 @@
11 import type { ModelCapabilityRow } from "../session/db/model-info-repo.ts";
2+import { isRecord } from "./history/shared.ts";
23
34 interface ModelsDevEntry {
45 canonicalModelId: string;
@@ -13,10 +14,6 @@ export interface ModelMatchIndex {
1314 alias: Map<string, string | null>;
1415 }
1516
16-function isRecord(value: unknown): value is Record<string, unknown> {
17- return typeof value === "object" && value !== null;
18-}
19-
2017 function basename(value: string): string {
2118 const idx = value.lastIndexOf("/");
2219 return idx === -1 ? value : value.slice(idx + 1);
src/llm-api/turn-context.ts+1−4
@@ -1,4 +1,5 @@
11 import { pruneMessages } from "ai";
2+import { isRecord } from "./history/shared.ts";
23 import type { CoreMessage } from "./turn.ts";
34
45 interface ToolContributorStats {
@@ -37,10 +38,6 @@ function safeStringify(value: unknown): string {
3738 }
3839 }
3940
40-function isRecord(value: unknown): value is Record<string, unknown> {
41- return value !== null && typeof value === "object";
42-}
43-
4441 /**
4542 * Lightweight alternative to getMessageDiagnostics that computes only the
4643 * fields needed for the context-pruned yield event (no roleBreakdown / tool
src/llm-api/turn-execution.ts+6−15
@@ -1,6 +1,7 @@
11 import type { FlexibleSchema, StepResult } from "ai";
22 import { dynamicTool, jsonSchema, type streamText } from "ai";
33 import { normalizeUnknownError } from "./error-utils.ts";
4+import { isRecord } from "./history/shared.ts";
45 import {
56 extractToolArgs,
67 hasRenderableToolArgs,
@@ -119,7 +120,7 @@ interface StreamTextChunk {
119120
120121 const TOOL_RESULT_CHUNK_TYPES = new Set(["tool-result", "tool-error"]);
121122
122-function normalizeToolCallId(raw: unknown): string | null {
123+function normalizeStringId(raw: unknown): string | null {
123124 if (typeof raw !== "string") return null;
124125 const trimmed = raw.trim();
125126 return trimmed ? trimmed : null;
@@ -131,16 +132,6 @@ function normalizeToolName(raw: unknown): string {
131132 return trimmed || "tool";
132133 }
133134
134-function isRecord(value: unknown): value is Record<string, unknown> {
135- return value !== null && typeof value === "object";
136-}
137-
138-function normalizeTextPartId(raw: unknown): string | null {
139- if (typeof raw !== "string") return null;
140- const trimmed = raw.trim();
141- return trimmed ? trimmed : null;
142-}
143-
144135 function getOpenAITextPhase(
145136 chunk: StreamTextChunk,
146137 ): "commentary" | "final_answer" | null {
@@ -171,7 +162,7 @@ class StreamTextPhaseTracker {
171162 private sawExplicitReasoningThisStep = false;
172163
173164 route(chunk: StreamTextChunk): StreamTextRoute {
174- const textPartId = normalizeTextPartId(chunk.id);
165+ const textPartId = normalizeStringId(chunk.id);
175166 switch (chunk.type) {
176167 case "start-step": {
177168 this.sawExplicitReasoningThisStep = false;
@@ -232,7 +223,7 @@ class StreamToolCallTracker {
232223
233224 if (type === "tool-input-start") {
234225 const toolName = normalizeToolName(chunk.toolName);
235- const toolCallId = normalizeToolCallId(chunk.toolCallId);
226+ const toolCallId = normalizeStringId(chunk.toolCallId);
236227 const args = extractToolArgs(chunk);
237228 if (!hasRenderableToolArgs(args)) {
238229 if (!toolCallId) {
@@ -254,7 +245,7 @@ class StreamToolCallTracker {
254245 chunk: this.trackRenderableStart(
255246 chunk,
256247 toolName,
257- normalizeToolCallId(chunk.toolCallId),
248+ normalizeStringId(chunk.toolCallId),
258249 ),
259250 suppressTurnEvent: false,
260251 };
@@ -262,7 +253,7 @@ class StreamToolCallTracker {
262253
263254 if (TOOL_RESULT_CHUNK_TYPES.has(type)) {
264255 const toolName = normalizeToolName(chunk.toolName);
265- const existingToolCallId = normalizeToolCallId(chunk.toolCallId);
256+ const existingToolCallId = normalizeStringId(chunk.toolCallId);
266257 if (existingToolCallId) {
267258 this.consumeTracked(toolName, existingToolCallId);
268259 return { chunk, suppressTurnEvent: false };
src/llm-api/turn-provider-options.ts+1−4
@@ -1,3 +1,4 @@
1+import { isRecord } from "./history/shared.ts";
12 import { isOpenAIGPT } from "./history-transforms.ts";
23 import {
34 getCacheFamily,
@@ -6,10 +7,6 @@ import {
67 type ThinkingEffort,
78 } from "./provider-options.ts";
89
9-function isRecord(value: unknown): value is Record<string, unknown> {
10- return value !== null && typeof value === "object";
11-}
12-
1310 function mergeDeep(
1411 target: Record<string, unknown>,
1512 source: Record<string, unknown>,