Files
oh-my-pi/packages/coding-agent/src/tools/tool-result.ts
T
can1357 e4225d6829 refactor(coding-agent): consolidated output utilities into streaming-output module
- Consolidated truncation and output utilities from tools/truncate.ts and tools/output-utils.ts into session/streaming-output.ts with improved UTF-8 boundary handling.
- Renamed formatSize() to formatBytes() across codebase for consistency and clarity in byte-level formatting.
- Refactored OutputSink to use windowed byte truncation instead of full-buffer encoding, improving memory efficiency on large outputs.
- Migrated from Buffer to Uint8Array in web scrapers for better cross-platform compatibility and native browser support.
- Added getArtifactManager() lazy-initialization method to ToolSession for deferred artifact manager instantiation.
- Simplified API surface with wildcard exports from tools and session modules, reducing import complexity.
2026-02-22 01:02:26 +01:00

87 lines
2.3 KiB
TypeScript

import type { AgentToolResult } from "@oh-my-pi/pi-agent-core";
import type { ImageContent, TextContent } from "@oh-my-pi/pi-ai";
import type { OutputSummary, TruncationResult } from "../session/streaming-output";
import type { OutputMeta, TruncationOptions, TruncationSummaryOptions, TruncationTextOptions } from "./output-meta";
import { outputMeta } from "./output-meta";
type ToolContent = Array<TextContent | ImageContent>;
type DetailsWithMeta = { meta?: OutputMeta };
export class ToolResultBuilder<TDetails extends DetailsWithMeta> {
#details: TDetails;
#meta = outputMeta();
#content: ToolContent = [];
constructor(details?: TDetails) {
this.#details = details ?? ({} as TDetails);
}
text(text: string): this {
this.#content = [{ type: "text", text }];
return this;
}
content(content: ToolContent): this {
this.#content = content;
return this;
}
truncation(result: TruncationResult, options: TruncationOptions): this {
this.#meta.truncation(result, options);
return this;
}
truncationFromSummary(summary: OutputSummary, options: TruncationSummaryOptions): this {
this.#meta.truncationFromSummary(summary, options);
return this;
}
truncationFromText(text: string, options: TruncationTextOptions): this {
this.#meta.truncationFromText(text, options);
return this;
}
limits(limits: { matchLimit?: number; resultLimit?: number; headLimit?: number; columnMax?: number }): this {
this.#meta.limits(limits);
return this;
}
sourceUrl(value: string): this {
this.#meta.sourceUrl(value);
return this;
}
sourcePath(value: string): this {
this.#meta.sourcePath(value);
return this;
}
sourceInternal(value: string): this {
this.#meta.sourceInternal(value);
return this;
}
diagnostics(summary: string, messages: string[]): this {
this.#meta.diagnostics(summary, messages);
return this;
}
done(): AgentToolResult<TDetails> {
const meta = this.#meta.get();
if (meta) {
this.#details.meta = meta;
}
const hasDetails = Object.entries(this.#details).some(([, value]) => value !== undefined);
return {
content: this.#content,
details: hasDetails ? this.#details : undefined,
};
}
}
export function toolResult<TDetails extends DetailsWithMeta>(details?: TDetails): ToolResultBuilder<TDetails> {
return new ToolResultBuilder(details);
}