The identical helper function was duplicated across ClaudeAdapter.ts, CursorAdapter.ts, and opencodeRuntime.ts. Consolidate into the shared schemaJson module and import from there.
Unified · Split
| @@ -82,9 +82,9 @@ import { · 81 unmodified lines | ||
| 82 | 82 | ProviderAdapterValidationError, |
| 83 | 83 | type ProviderAdapterError, |
| 84 | 84 | } from "../Errors.ts"; |
| 85 | +import { encodeJsonStringForDiagnostics } from "@t3tools/shared/schemaJson"; | |
| 85 | 86 | import { type ClaudeAdapterShape } from "../Services/ClaudeAdapter.ts"; |
| 86 | 87 | import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts"; |
| 87 | −const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString); | |
| 88 | 88 | const decodeUnknownJsonStringExit = Schema.decodeUnknownExit(Schema.UnknownFromJsonString); |
| 89 | 89 | |
| 90 | 90 | const PROVIDER = ProviderDriverKind.make("claudeAgent"); |
| @@ -95,11 +95,6 @@ type ClaudeToolResultStreamKind = Extract< · 4 unmodified lines | ||
| 95 | 95 | >; |
| 96 | 96 | type ClaudeSdkEffort = NonNullable<ClaudeQueryOptions["effort"]>; |
| 97 | 97 | |
| 98 | −function encodeJsonStringForDiagnostics(input: unknown): string | undefined { | |
| 99 | − const result = encodeUnknownJsonStringExit(input); | |
| 100 | − return Exit.isSuccess(result) ? result.value : undefined; | |
| 101 | −} | |
| 102 | − | |
| 103 | 98 | type PromptQueueItem = |
| 104 | 99 | | { |
| 105 | 100 | readonly type: "message"; |
| @@ -72,10 +72,10 @@ import { · 71 unmodified lines | ||
| 72 | 72 | extractPlanMarkdown, |
| 73 | 73 | extractTodosAsPlan, |
| 74 | 74 | } from "../acp/CursorAcpExtension.ts"; |
| 75 | +import { encodeJsonStringForDiagnostics } from "@t3tools/shared/schemaJson"; | |
| 75 | 76 | import { type CursorAdapterShape } from "../Services/CursorAdapter.ts"; |
| 76 | 77 | import { resolveCursorAcpBaseModelId } from "./CursorProvider.ts"; |
| 77 | 78 | import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts"; |
| 78 | −const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString); | |
| 79 | 79 | |
| 80 | 80 | const PROVIDER = ProviderDriverKind.make("cursor"); |
| 81 | 81 | const CURSOR_RESUME_VERSION = 1 as const; |
| @@ -83,11 +83,6 @@ const ACP_PLAN_MODE_ALIASES = ["plan", "architect"]; · 1 unmodified line | ||
| 83 | 83 | const ACP_IMPLEMENT_MODE_ALIASES = ["code", "agent", "default", "chat", "implement"]; |
| 84 | 84 | const ACP_APPROVAL_MODE_ALIASES = ["ask"]; |
| 85 | 85 | |
| 86 | −function encodeJsonStringForDiagnostics(input: unknown): string | undefined { | |
| 87 | − const result = encodeUnknownJsonStringExit(input); | |
| 88 | − return Exit.isSuccess(result) ? result.value : undefined; | |
| 89 | −} | |
| 90 | − | |
| 91 | 86 | export interface CursorAdapterLiveOptions { |
| 92 | 87 | readonly environment?: NodeJS.ProcessEnv; |
| 93 | 88 | readonly nativeEventLogPath?: string; |
| @@ -24,14 +24,13 @@ import * as P from "effect/Predicate"; · 23 unmodified lines | ||
| 24 | 24 | import * as Ref from "effect/Ref"; |
| 25 | 25 | import * as Result from "effect/Result"; |
| 26 | 26 | import * as Scope from "effect/Scope"; |
| 27 | −import * as Schema from "effect/Schema"; | |
| 28 | 27 | import * as Stream from "effect/Stream"; |
| 29 | 28 | import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; |
| 30 | 29 | |
| 30 | +import { encodeJsonStringForDiagnostics } from "@t3tools/shared/schemaJson"; | |
| 31 | 31 | import { isWindowsCommandNotFound } from "../processRunner.ts"; |
| 32 | 32 | import { collectStreamAsString } from "./providerSnapshot.ts"; |
| 33 | 33 | import * as NetService from "@t3tools/shared/Net"; |
| 34 | −const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString); | |
| 35 | 34 | const OPENCODE_EMPTY_CONFIG_CONTENT = "{}"; |
| 36 | 35 | |
| 37 | 36 | const OPENCODE_SERVER_READY_PREFIX = "opencode server listening"; |
| @@ -58,11 +57,6 @@ export class OpenCodeRuntimeError extends Data.TaggedError(OPENCODE_RUNTIME_ERRO · 20 unmodified lines | ||
| 58 | 57 | P.isTagged(u, OPENCODE_RUNTIME_ERROR_TAG); |
| 59 | 58 | } |
| 60 | 59 | |
| 61 | −function encodeJsonStringForDiagnostics(input: unknown): string | undefined { | |
| 62 | − const result = encodeUnknownJsonStringExit(input); | |
| 63 | − return Exit.isSuccess(result) ? result.value : undefined; | |
| 64 | −} | |
| 65 | − | |
| 66 | 60 | export function openCodeRuntimeErrorDetail(cause: unknown): string { |
| 67 | 61 | if (OpenCodeRuntimeError.is(cause)) return cause.detail; |
| 68 | 62 | if (cause instanceof Error && cause.message.trim().length > 0) return cause.message.trim(); |
| @@ -84,6 +84,17 @@ export const fromLenientJsonString = new SchemaTransformation.Transformation( · 83 unmodified lines | ||
| 84 | 84 | SchemaGetter.stringifyJson(), |
| 85 | 85 | ); |
| 86 | 86 | |
| 87 | +const encodeUnknownJsonStringExit = Schema.encodeUnknownExit(Schema.UnknownFromJsonString); | |
| 88 | + | |
| 89 | +/** | |
| 90 | + * Attempt to encode an unknown value to a JSON string for diagnostic output. | |
| 91 | + * Returns `undefined` on encoding failure instead of throwing. | |
| 92 | + */ | |
| 93 | +export function encodeJsonStringForDiagnostics(input: unknown): string | undefined { | |
| 94 | + const result = encodeUnknownJsonStringExit(input); | |
| 95 | + return Exit.isSuccess(result) ? result.value : undefined; | |
| 96 | +} | |
| 97 | + | |
| 87 | 98 | export const prettyJsonString = SchemaGetter.parseJson<string>().compose( |
| 88 | 99 | SchemaGetter.stringifyJson({ space: 2 }), |
| 89 | 100 | ); |