Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09d324bc28 |
No files matched your search
@@ -74,6 +74,18 @@ const ProviderRollbackConversationInput = Schema.Struct({
|
||||
numTurns: NonNegativeInt,
|
||||
});
|
||||
|
||||
function toValidationError(
|
||||
operation: string,
|
||||
issue: string,
|
||||
cause?: unknown,
|
||||
): ProviderValidationError {
|
||||
return new ProviderValidationError({
|
||||
operation,
|
||||
issue,
|
||||
...(cause !== undefined ? { cause } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
const decodeInputOrValidationError = <S extends Schema.Top>(input: {
|
||||
readonly operation: string;
|
||||
readonly schema: S;
|
||||
@@ -236,12 +248,12 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
payload.providerInstanceId !== undefined
|
||||
? Effect.succeed(payload.providerInstanceId)
|
||||
: Effect.fail(
|
||||
new ProviderValidationError({
|
||||
toValidationError(
|
||||
operation,
|
||||
issue: payload.provider
|
||||
payload.provider
|
||||
? `Provider instance id is required for provider '${payload.provider}'.`
|
||||
: "Provider instance id is required.",
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const upsertSessionBinding = (
|
||||
@@ -376,10 +388,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
}
|
||||
|
||||
if (!hasResumeCursor) {
|
||||
return yield* new ProviderValidationError({
|
||||
operation: input.operation,
|
||||
issue: `Cannot recover thread '${input.binding.threadId}' because no provider resume state is persisted.`,
|
||||
});
|
||||
return yield* toValidationError(
|
||||
input.operation,
|
||||
`Cannot recover thread '${input.binding.threadId}' because no provider resume state is persisted.`,
|
||||
);
|
||||
}
|
||||
|
||||
const persistedCwd = readPersistedCwd(input.binding.runtimePayload);
|
||||
@@ -399,10 +411,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
.pipe(Effect.onError(() => clearMcpSession(input.binding.threadId)));
|
||||
if (resumed.provider !== adapter.provider) {
|
||||
yield* clearMcpSession(input.binding.threadId);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: input.operation,
|
||||
issue: `Adapter/provider mismatch while recovering thread '${input.binding.threadId}'. Expected '${adapter.provider}', received '${resumed.provider}'.`,
|
||||
});
|
||||
return yield* toValidationError(
|
||||
input.operation,
|
||||
`Adapter/provider mismatch while recovering thread '${input.binding.threadId}'. Expected '${adapter.provider}', received '${resumed.provider}'.`,
|
||||
);
|
||||
}
|
||||
|
||||
yield* upsertSessionBinding(
|
||||
@@ -433,10 +445,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
const bindingOption = yield* directory.getBinding(input.threadId);
|
||||
const binding = Option.getOrUndefined(bindingOption);
|
||||
if (!binding) {
|
||||
return yield* new ProviderValidationError({
|
||||
operation: input.operation,
|
||||
issue: `Cannot route thread '${input.threadId}' because no persisted provider binding exists.`,
|
||||
});
|
||||
return yield* toValidationError(
|
||||
input.operation,
|
||||
`Cannot route thread '${input.threadId}' because no persisted provider binding exists.`,
|
||||
);
|
||||
}
|
||||
const instanceId = yield* requireBindingInstanceId(input.operation, binding);
|
||||
const adapter = yield* registry.getByInstance(instanceId);
|
||||
@@ -531,10 +543,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
const resolvedProvider = instanceInfo.driverKind;
|
||||
metricProvider = resolvedProvider;
|
||||
if (parsed.provider !== undefined && parsed.provider !== resolvedProvider) {
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.startSession",
|
||||
issue: `Provider instance '${resolvedInstanceId}' belongs to driver '${resolvedProvider}', not '${parsed.provider}'.`,
|
||||
});
|
||||
return yield* toValidationError(
|
||||
"ProviderService.startSession",
|
||||
`Provider instance '${resolvedInstanceId}' belongs to driver '${resolvedProvider}', not '${parsed.provider}'.`,
|
||||
);
|
||||
}
|
||||
const input = {
|
||||
...parsed,
|
||||
@@ -542,10 +554,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
provider: resolvedProvider,
|
||||
};
|
||||
if (!instanceInfo.enabled) {
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.startSession",
|
||||
issue: `Provider instance '${resolvedInstanceId}' is disabled in T3 Code settings.`,
|
||||
});
|
||||
return yield* toValidationError(
|
||||
"ProviderService.startSession",
|
||||
`Provider instance '${resolvedInstanceId}' is disabled in T3 Code settings.`,
|
||||
);
|
||||
}
|
||||
const persistedBinding = Option.getOrUndefined(yield* directory.getBinding(threadId));
|
||||
const effectiveResumeCursor =
|
||||
@@ -590,10 +602,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
|
||||
if (session.provider !== adapter.provider) {
|
||||
yield* clearMcpSession(threadId);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.startSession",
|
||||
issue: `Adapter/provider mismatch: requested '${adapter.provider}', received '${session.provider}'.`,
|
||||
});
|
||||
return yield* toValidationError(
|
||||
"ProviderService.startSession",
|
||||
`Adapter/provider mismatch: requested '${adapter.provider}', received '${session.provider}'.`,
|
||||
);
|
||||
}
|
||||
const sessionWithInstance = {
|
||||
...session,
|
||||
@@ -642,10 +654,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
attachments: parsed.attachments ?? [],
|
||||
};
|
||||
if (!input.input && input.attachments.length === 0) {
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.sendTurn",
|
||||
issue: "Either input text or at least one attachment is required",
|
||||
});
|
||||
return yield* toValidationError(
|
||||
"ProviderService.sendTurn",
|
||||
"Either input text or at least one attachment is required",
|
||||
);
|
||||
}
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"provider.operation": "send-turn",
|
||||
|
||||
@@ -15,6 +15,15 @@ import {
|
||||
} from "../Services/ProviderSessionDirectory.ts";
|
||||
const decodeProviderDriverKindValue = Schema.decodeUnknownEffect(ProviderDriverKind);
|
||||
|
||||
function toPersistenceError(operation: string) {
|
||||
return (cause: unknown) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation,
|
||||
detail: `Failed to execute ${operation}.`,
|
||||
cause,
|
||||
});
|
||||
}
|
||||
|
||||
function decodeProviderDriverKind(
|
||||
providerName: string,
|
||||
operation: string,
|
||||
@@ -79,14 +88,7 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
|
||||
const getBinding = (threadId: ThreadId) =>
|
||||
repository.getByThreadId({ threadId }).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.getBinding:getByThreadId",
|
||||
detail: "Failed to execute ProviderSessionDirectory.getBinding:getByThreadId.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.mapError(toPersistenceError("ProviderSessionDirectory.getBinding:getByThreadId")),
|
||||
Effect.flatMap((runtime) =>
|
||||
Option.match(runtime, {
|
||||
onNone: () => Effect.succeed(Option.none<ProviderRuntimeBinding>()),
|
||||
@@ -99,16 +101,9 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
);
|
||||
|
||||
const upsert: ProviderSessionDirectoryShape["upsert"] = Effect.fn(function* (binding) {
|
||||
const existing = yield* repository.getByThreadId({ threadId: binding.threadId }).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.upsert:getByThreadId",
|
||||
detail: "Failed to execute ProviderSessionDirectory.upsert:getByThreadId.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const existing = yield* repository
|
||||
.getByThreadId({ threadId: binding.threadId })
|
||||
.pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.upsert:getByThreadId")));
|
||||
|
||||
const existingRuntime = Option.getOrUndefined(existing);
|
||||
const resolvedThreadId = binding.threadId ?? existingRuntime?.threadId;
|
||||
@@ -150,16 +145,7 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
binding.runtimePayload,
|
||||
),
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.upsert:upsert",
|
||||
detail: "Failed to execute ProviderSessionDirectory.upsert:upsert.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
.pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.upsert:upsert")));
|
||||
});
|
||||
|
||||
const getProvider: ProviderSessionDirectoryShape["getProvider"] = (threadId) =>
|
||||
@@ -180,27 +166,13 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
|
||||
const listThreadIds: ProviderSessionDirectoryShape["listThreadIds"] = () =>
|
||||
repository.list().pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.listThreadIds:list",
|
||||
detail: "Failed to execute ProviderSessionDirectory.listThreadIds:list.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.mapError(toPersistenceError("ProviderSessionDirectory.listThreadIds:list")),
|
||||
Effect.map((rows) => rows.map((row) => row.threadId)),
|
||||
);
|
||||
|
||||
const listBindings: ProviderSessionDirectoryShape["listBindings"] = () =>
|
||||
repository.list().pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.listBindings:list",
|
||||
detail: "Failed to execute ProviderSessionDirectory.listBindings:list.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.mapError(toPersistenceError("ProviderSessionDirectory.listBindings:list")),
|
||||
Effect.flatMap((rows) =>
|
||||
Effect.forEach(
|
||||
rows,
|
||||
@@ -223,3 +195,7 @@ export const ProviderSessionDirectoryLive = Layer.effect(
|
||||
ProviderSessionDirectory,
|
||||
makeProviderSessionDirectory,
|
||||
);
|
||||
|
||||
export function makeProviderSessionDirectoryLive() {
|
||||
return Layer.effect(ProviderSessionDirectory, makeProviderSessionDirectory);
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { EDITORS, EditorId, type EnvironmentId } from "@t3tools/contracts";
|
||||
import { EDITORS, EditorId, EnvironmentId } from "@t3tools/contracts";
|
||||
import {
|
||||
mapAtomCommandResult,
|
||||
type AtomCommandFailure,
|
||||
type AtomCommandResult,
|
||||
} from "@t3tools/client-runtime/state/runtime";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Data from "effect/Data";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { AsyncResult } from "effect/unstable/reactivity";
|
||||
import { getLocalStorageItem, setLocalStorageItem, useLocalStorage } from "./hooks/useLocalStorage";
|
||||
import { useCallback, useMemo } from "react";
|
||||
@@ -14,11 +14,29 @@ import { useAtomCommand } from "./state/use-atom-command";
|
||||
|
||||
const LAST_EDITOR_KEY = "t3code:last-editor";
|
||||
|
||||
export class PreferredEditorUnavailableError extends Data.TaggedError(
|
||||
export class PreferredEditorEnvironmentRequiredError extends Schema.TaggedErrorClass<PreferredEditorEnvironmentRequiredError>()(
|
||||
"PreferredEditorEnvironmentRequiredError",
|
||||
{
|
||||
targetPath: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Cannot open ${this.targetPath} because no environment is selected.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class PreferredEditorUnavailableError extends Schema.TaggedErrorClass<PreferredEditorUnavailableError>()(
|
||||
"PreferredEditorUnavailableError",
|
||||
)<{
|
||||
readonly message: string;
|
||||
}> {}
|
||||
{
|
||||
environmentId: EnvironmentId,
|
||||
targetPath: Schema.String,
|
||||
availableEditorIds: Schema.Array(EditorId),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `No available editor can open ${this.targetPath} in environment ${this.environmentId}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) {
|
||||
const [lastEditor, setLastEditor] = useLocalStorage(LAST_EDITOR_KEY, null, EditorId);
|
||||
@@ -55,13 +73,18 @@ export function useOpenInPreferredEditor(
|
||||
async (
|
||||
targetPath: string,
|
||||
): Promise<
|
||||
AtomCommandResult<EditorId, OpenInEditorError | PreferredEditorUnavailableError>
|
||||
AtomCommandResult<
|
||||
EditorId,
|
||||
| OpenInEditorError
|
||||
| PreferredEditorEnvironmentRequiredError
|
||||
| PreferredEditorUnavailableError
|
||||
>
|
||||
> => {
|
||||
if (environmentId === null) {
|
||||
return AsyncResult.failure(
|
||||
Cause.fail(
|
||||
new PreferredEditorUnavailableError({
|
||||
message: "No environment is selected.",
|
||||
new PreferredEditorEnvironmentRequiredError({
|
||||
targetPath,
|
||||
}),
|
||||
),
|
||||
);
|
||||
@@ -71,7 +94,9 @@ export function useOpenInPreferredEditor(
|
||||
return AsyncResult.failure(
|
||||
Cause.fail(
|
||||
new PreferredEditorUnavailableError({
|
||||
message: "No available editors found.",
|
||||
environmentId,
|
||||
targetPath,
|
||||
availableEditorIds: availableEditors,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user