refactor: extract encodeJsonStringForDiagnostics into @t3tools/shared/schemaJson
The identical helper function was duplicated across ClaudeAdapter.ts, CursorAdapter.ts, and opencodeRuntime.ts. Consolidate into the shared schemaJson module and import from there.
This commit is contained in:
4 files changed
+14
-19
No files matched your search
@@ -82,9 +82,9 @@ import {
|
||||
ProviderAdapterValidationError,
|
||||
type ProviderAdapterError,
|
||||
} from "../Errors.ts";
|
||||
import { encodeJsonStringForDiagnostics } from "@t3tools/shared/schemaJson";
|
||||
import { type ClaudeAdapterShape } from "../Services/ClaudeAdapter.ts";
|
||||
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
|
||||
const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString);
|
||||
const decodeUnknownJsonStringExit = Schema.decodeUnknownExit(Schema.UnknownFromJsonString);
|
||||
|
||||
const PROVIDER = ProviderDriverKind.make("claudeAgent");
|
||||
@@ -95,11 +95,6 @@ type ClaudeToolResultStreamKind = Extract<
|
||||
>;
|
||||
type ClaudeSdkEffort = NonNullable<ClaudeQueryOptions["effort"]>;
|
||||
|
||||
function encodeJsonStringForDiagnostics(input: unknown): string | undefined {
|
||||
const result = encodeUnknownJsonStringExit(input);
|
||||
return Exit.isSuccess(result) ? result.value : undefined;
|
||||
}
|
||||
|
||||
type PromptQueueItem =
|
||||
| {
|
||||
readonly type: "message";
|
||||
|
||||
@@ -72,10 +72,10 @@ import {
|
||||
extractPlanMarkdown,
|
||||
extractTodosAsPlan,
|
||||
} from "../acp/CursorAcpExtension.ts";
|
||||
import { encodeJsonStringForDiagnostics } from "@t3tools/shared/schemaJson";
|
||||
import { type CursorAdapterShape } from "../Services/CursorAdapter.ts";
|
||||
import { resolveCursorAcpBaseModelId } from "./CursorProvider.ts";
|
||||
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
|
||||
const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString);
|
||||
|
||||
const PROVIDER = ProviderDriverKind.make("cursor");
|
||||
const CURSOR_RESUME_VERSION = 1 as const;
|
||||
@@ -83,11 +83,6 @@ const ACP_PLAN_MODE_ALIASES = ["plan", "architect"];
|
||||
const ACP_IMPLEMENT_MODE_ALIASES = ["code", "agent", "default", "chat", "implement"];
|
||||
const ACP_APPROVAL_MODE_ALIASES = ["ask"];
|
||||
|
||||
function encodeJsonStringForDiagnostics(input: unknown): string | undefined {
|
||||
const result = encodeUnknownJsonStringExit(input);
|
||||
return Exit.isSuccess(result) ? result.value : undefined;
|
||||
}
|
||||
|
||||
export interface CursorAdapterLiveOptions {
|
||||
readonly environment?: NodeJS.ProcessEnv;
|
||||
readonly nativeEventLogPath?: string;
|
||||
|
||||
@@ -24,14 +24,13 @@ import * as P from "effect/Predicate";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Scope from "effect/Scope";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as Stream from "effect/Stream";
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
import { encodeJsonStringForDiagnostics } from "@t3tools/shared/schemaJson";
|
||||
import { isWindowsCommandNotFound } from "../processRunner.ts";
|
||||
import { collectStreamAsString } from "./providerSnapshot.ts";
|
||||
import * as NetService from "@t3tools/shared/Net";
|
||||
const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString);
|
||||
const OPENCODE_EMPTY_CONFIG_CONTENT = "{}";
|
||||
|
||||
const OPENCODE_SERVER_READY_PREFIX = "opencode server listening";
|
||||
@@ -58,11 +57,6 @@ export class OpenCodeRuntimeError extends Data.TaggedError(OPENCODE_RUNTIME_ERRO
|
||||
P.isTagged(u, OPENCODE_RUNTIME_ERROR_TAG);
|
||||
}
|
||||
|
||||
function encodeJsonStringForDiagnostics(input: unknown): string | undefined {
|
||||
const result = encodeUnknownJsonStringExit(input);
|
||||
return Exit.isSuccess(result) ? result.value : undefined;
|
||||
}
|
||||
|
||||
export function openCodeRuntimeErrorDetail(cause: unknown): string {
|
||||
if (OpenCodeRuntimeError.is(cause)) return cause.detail;
|
||||
if (cause instanceof Error && cause.message.trim().length > 0) return cause.message.trim();
|
||||
|
||||
@@ -84,6 +84,17 @@ export const fromLenientJsonString = new SchemaTransformation.Transformation(
|
||||
SchemaGetter.stringifyJson(),
|
||||
);
|
||||
|
||||
const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString);
|
||||
|
||||
/**
|
||||
* Attempt to encode an unknown value to a JSON string for diagnostic output.
|
||||
* Returns `undefined` on encoding failure instead of throwing.
|
||||
*/
|
||||
export function encodeJsonStringForDiagnostics(input: unknown): string | undefined {
|
||||
const result = encodeUnknownJsonStringExit(input);
|
||||
return Exit.isSuccess(result) ? result.value : undefined;
|
||||
}
|
||||
|
||||
export const prettyJsonString = SchemaGetter.parseJson<string>().compose(
|
||||
SchemaGetter.stringifyJson({ space: 2 }),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user