Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aaafc61b6 |
No files matched your search
@@ -62,6 +62,22 @@ function catalogError(operation: string, cause: unknown) {
|
||||
});
|
||||
}
|
||||
|
||||
function shellPersistenceError(
|
||||
operation:
|
||||
| "load-shell"
|
||||
| "save-shell"
|
||||
| "load-thread"
|
||||
| "save-thread"
|
||||
| "remove-thread"
|
||||
| "clear-environment",
|
||||
cause: unknown,
|
||||
) {
|
||||
return new ConnectionPersistenceError({
|
||||
operation,
|
||||
message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`,
|
||||
});
|
||||
}
|
||||
|
||||
function threadSnapshotFileName(threadId: ThreadId): string {
|
||||
return `${encodeURIComponent(threadId)}.json`;
|
||||
}
|
||||
@@ -84,7 +100,7 @@ const threadSnapshotDirectory = Effect.fn("mobile.connectionStorage.threadSnapsh
|
||||
}
|
||||
return directory;
|
||||
},
|
||||
catch: (cause) => new ConnectionPersistenceError({ operation, environmentId, cause }),
|
||||
catch: (cause) => shellPersistenceError(operation, cause),
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -101,6 +117,16 @@ const threadSnapshotFile = Effect.fn("mobile.connectionStorage.threadSnapshotFil
|
||||
);
|
||||
});
|
||||
|
||||
function targetPersistenceError(
|
||||
operation: "list-targets" | "register-connection" | "remove-connection",
|
||||
error: ConnectionTransientError,
|
||||
) {
|
||||
return new ConnectionPersistenceError({
|
||||
operation,
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
|
||||
const secureCatalogStorage: SecureCatalogStorage = {
|
||||
getItem: (key) =>
|
||||
Effect.tryPromise({
|
||||
@@ -137,7 +163,7 @@ const shellSnapshotFileInDirectory = Effect.fn(
|
||||
directory.create({ idempotent: true, intermediates: true });
|
||||
return new File(directory, shellSnapshotFileName(environmentId));
|
||||
},
|
||||
catch: (cause) => new ConnectionPersistenceError({ operation, environmentId, cause }),
|
||||
catch: (cause) => shellPersistenceError(operation, cause),
|
||||
});
|
||||
});
|
||||
|
||||
@@ -158,38 +184,18 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
const targetStore = ConnectionTargetStore.of({
|
||||
list: catalog.read.pipe(
|
||||
Effect.map((document) => document.targets),
|
||||
Effect.mapError(
|
||||
(cause) => new ConnectionPersistenceError({ operation: "list-targets", cause }),
|
||||
),
|
||||
Effect.mapError((error) => targetPersistenceError("list-targets", error)),
|
||||
),
|
||||
});
|
||||
const registrationStore = ConnectionRegistrationStore.of({
|
||||
register: (registration) =>
|
||||
catalog
|
||||
.update((document) => registerConnectionInCatalog(document, registration))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "register-connection",
|
||||
environmentId: registration.target.environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
.pipe(Effect.mapError((error) => targetPersistenceError("register-connection", error))),
|
||||
remove: (target) =>
|
||||
catalog
|
||||
.update((document) => removeConnectionFromCatalog(document, target))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "remove-connection",
|
||||
environmentId: target.environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
.pipe(Effect.mapError((error) => targetPersistenceError("remove-connection", error))),
|
||||
});
|
||||
const profileStore = ProfileStore.make({
|
||||
get: (connectionId) =>
|
||||
@@ -277,34 +283,15 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
if (file.exists) {
|
||||
const raw = yield* Effect.tryPromise({
|
||||
try: () => file.text(),
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||
});
|
||||
const parsed = yield* Effect.try({
|
||||
try: () => JSON.parse(raw) as unknown,
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||
});
|
||||
const stored = yield* Effect.fromResult(
|
||||
Schema.decodeUnknownResult(StoredShellSnapshot)(parsed),
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
).pipe(Effect.mapError((cause) => shellPersistenceError("load-shell", cause)));
|
||||
return stored.environmentId === environmentId
|
||||
? Option.some(stored.snapshot)
|
||||
: Option.none();
|
||||
@@ -316,34 +303,15 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
}
|
||||
const legacyRaw = yield* Effect.tryPromise({
|
||||
try: () => legacyFile.text(),
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||
});
|
||||
const legacyParsed = yield* Effect.try({
|
||||
try: () => JSON.parse(legacyRaw) as unknown,
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("load-shell", cause),
|
||||
});
|
||||
const legacyStored = yield* Effect.fromResult(
|
||||
Schema.decodeUnknownResult(LegacyStoredShellSnapshot)(legacyParsed),
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
).pipe(Effect.mapError((cause) => shellPersistenceError("load-shell", cause)));
|
||||
return legacyStored.environmentId === environmentId
|
||||
? Option.some(legacyStored.snapshot)
|
||||
: Option.none();
|
||||
@@ -358,16 +326,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
} as const;
|
||||
const encoded = yield* Effect.fromResult(
|
||||
Schema.encodeUnknownResult(StoredShellSnapshot)(stored),
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "save-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
).pipe(Effect.mapError((cause) => shellPersistenceError("save-shell", cause)));
|
||||
yield* Effect.try({
|
||||
try: () => {
|
||||
if (!file.exists) {
|
||||
@@ -375,12 +334,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
}
|
||||
file.write(JSON.stringify(encoded));
|
||||
},
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "save-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("save-shell", cause),
|
||||
});
|
||||
}),
|
||||
loadThread: (environmentId, threadId) =>
|
||||
@@ -391,37 +345,15 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
}
|
||||
const raw = yield* Effect.tryPromise({
|
||||
try: () => file.text(),
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-thread",
|
||||
environmentId,
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("load-thread", cause),
|
||||
});
|
||||
const parsed = yield* Effect.try({
|
||||
try: () => JSON.parse(raw) as unknown,
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-thread",
|
||||
environmentId,
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("load-thread", cause),
|
||||
});
|
||||
const stored = yield* Effect.fromResult(
|
||||
Schema.decodeUnknownResult(StoredThreadSnapshot)(parsed),
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-thread",
|
||||
environmentId,
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
).pipe(Effect.mapError((cause) => shellPersistenceError("load-thread", cause)));
|
||||
return stored.environmentId === environmentId && stored.threadId === threadId
|
||||
? Option.some(stored.thread)
|
||||
: Option.none();
|
||||
@@ -436,17 +368,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
threadId: thread.id,
|
||||
thread,
|
||||
}),
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "save-thread",
|
||||
environmentId,
|
||||
threadId: thread.id,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
).pipe(Effect.mapError((cause) => shellPersistenceError("save-thread", cause)));
|
||||
yield* Effect.try({
|
||||
try: () => {
|
||||
if (!file.exists) {
|
||||
@@ -454,55 +376,36 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
}
|
||||
file.write(JSON.stringify(encoded));
|
||||
},
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "save-thread",
|
||||
environmentId,
|
||||
threadId: thread.id,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("save-thread", cause),
|
||||
});
|
||||
}),
|
||||
removeThread: (environmentId, threadId) =>
|
||||
Effect.gen(function* () {
|
||||
const file = yield* threadSnapshotFile(environmentId, threadId, "remove-thread");
|
||||
if (file.exists) {
|
||||
yield* Effect.try({
|
||||
try: () => file.delete(),
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "remove-thread",
|
||||
environmentId,
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
file.delete();
|
||||
}
|
||||
}),
|
||||
}).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
cause._tag === "ConnectionPersistenceError"
|
||||
? cause
|
||||
: shellPersistenceError("remove-thread", cause),
|
||||
),
|
||||
),
|
||||
clear: (environmentId) =>
|
||||
Effect.gen(function* () {
|
||||
const file = yield* shellSnapshotFile(environmentId, "clear-environment");
|
||||
if (file.exists) {
|
||||
yield* Effect.try({
|
||||
try: () => file.delete(),
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "clear-environment",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("clear-environment", cause),
|
||||
});
|
||||
}
|
||||
const legacyFile = yield* legacyShellSnapshotFile(environmentId, "clear-environment");
|
||||
if (legacyFile.exists) {
|
||||
yield* Effect.try({
|
||||
try: () => legacyFile.delete(),
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "clear-environment",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("clear-environment", cause),
|
||||
});
|
||||
}
|
||||
const threadDirectory = yield* threadSnapshotDirectory(
|
||||
@@ -512,12 +415,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
if (threadDirectory.exists) {
|
||||
yield* Effect.try({
|
||||
try: () => threadDirectory.delete(),
|
||||
catch: (cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "clear-environment",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
catch: (cause) => shellPersistenceError("clear-environment", cause),
|
||||
});
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -74,18 +74,6 @@ 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;
|
||||
@@ -248,12 +236,12 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
payload.providerInstanceId !== undefined
|
||||
? Effect.succeed(payload.providerInstanceId)
|
||||
: Effect.fail(
|
||||
toValidationError(
|
||||
new ProviderValidationError({
|
||||
operation,
|
||||
payload.provider
|
||||
issue: payload.provider
|
||||
? `Provider instance id is required for provider '${payload.provider}'.`
|
||||
: "Provider instance id is required.",
|
||||
),
|
||||
}),
|
||||
);
|
||||
|
||||
const upsertSessionBinding = (
|
||||
@@ -388,10 +376,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
}
|
||||
|
||||
if (!hasResumeCursor) {
|
||||
return yield* toValidationError(
|
||||
input.operation,
|
||||
`Cannot recover thread '${input.binding.threadId}' because no provider resume state is persisted.`,
|
||||
);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: input.operation,
|
||||
issue: `Cannot recover thread '${input.binding.threadId}' because no provider resume state is persisted.`,
|
||||
});
|
||||
}
|
||||
|
||||
const persistedCwd = readPersistedCwd(input.binding.runtimePayload);
|
||||
@@ -411,10 +399,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* toValidationError(
|
||||
input.operation,
|
||||
`Adapter/provider mismatch while recovering thread '${input.binding.threadId}'. Expected '${adapter.provider}', received '${resumed.provider}'.`,
|
||||
);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: input.operation,
|
||||
issue: `Adapter/provider mismatch while recovering thread '${input.binding.threadId}'. Expected '${adapter.provider}', received '${resumed.provider}'.`,
|
||||
});
|
||||
}
|
||||
|
||||
yield* upsertSessionBinding(
|
||||
@@ -445,10 +433,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
const bindingOption = yield* directory.getBinding(input.threadId);
|
||||
const binding = Option.getOrUndefined(bindingOption);
|
||||
if (!binding) {
|
||||
return yield* toValidationError(
|
||||
input.operation,
|
||||
`Cannot route thread '${input.threadId}' because no persisted provider binding exists.`,
|
||||
);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: input.operation,
|
||||
issue: `Cannot route thread '${input.threadId}' because no persisted provider binding exists.`,
|
||||
});
|
||||
}
|
||||
const instanceId = yield* requireBindingInstanceId(input.operation, binding);
|
||||
const adapter = yield* registry.getByInstance(instanceId);
|
||||
@@ -543,10 +531,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
const resolvedProvider = instanceInfo.driverKind;
|
||||
metricProvider = resolvedProvider;
|
||||
if (parsed.provider !== undefined && parsed.provider !== resolvedProvider) {
|
||||
return yield* toValidationError(
|
||||
"ProviderService.startSession",
|
||||
`Provider instance '${resolvedInstanceId}' belongs to driver '${resolvedProvider}', not '${parsed.provider}'.`,
|
||||
);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.startSession",
|
||||
issue: `Provider instance '${resolvedInstanceId}' belongs to driver '${resolvedProvider}', not '${parsed.provider}'.`,
|
||||
});
|
||||
}
|
||||
const input = {
|
||||
...parsed,
|
||||
@@ -554,10 +542,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
provider: resolvedProvider,
|
||||
};
|
||||
if (!instanceInfo.enabled) {
|
||||
return yield* toValidationError(
|
||||
"ProviderService.startSession",
|
||||
`Provider instance '${resolvedInstanceId}' is disabled in T3 Code settings.`,
|
||||
);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.startSession",
|
||||
issue: `Provider instance '${resolvedInstanceId}' is disabled in T3 Code settings.`,
|
||||
});
|
||||
}
|
||||
const persistedBinding = Option.getOrUndefined(yield* directory.getBinding(threadId));
|
||||
const effectiveResumeCursor =
|
||||
@@ -602,10 +590,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
|
||||
if (session.provider !== adapter.provider) {
|
||||
yield* clearMcpSession(threadId);
|
||||
return yield* toValidationError(
|
||||
"ProviderService.startSession",
|
||||
`Adapter/provider mismatch: requested '${adapter.provider}', received '${session.provider}'.`,
|
||||
);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.startSession",
|
||||
issue: `Adapter/provider mismatch: requested '${adapter.provider}', received '${session.provider}'.`,
|
||||
});
|
||||
}
|
||||
const sessionWithInstance = {
|
||||
...session,
|
||||
@@ -654,10 +642,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
attachments: parsed.attachments ?? [],
|
||||
};
|
||||
if (!input.input && input.attachments.length === 0) {
|
||||
return yield* toValidationError(
|
||||
"ProviderService.sendTurn",
|
||||
"Either input text or at least one attachment is required",
|
||||
);
|
||||
return yield* new ProviderValidationError({
|
||||
operation: "ProviderService.sendTurn",
|
||||
issue: "Either input text or at least one attachment is required",
|
||||
});
|
||||
}
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"provider.operation": "send-turn",
|
||||
|
||||
@@ -15,15 +15,6 @@ 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,
|
||||
@@ -88,7 +79,14 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
|
||||
const getBinding = (threadId: ThreadId) =>
|
||||
repository.getByThreadId({ threadId }).pipe(
|
||||
Effect.mapError(toPersistenceError("ProviderSessionDirectory.getBinding:getByThreadId")),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.getBinding:getByThreadId",
|
||||
detail: "Failed to execute ProviderSessionDirectory.getBinding:getByThreadId.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.flatMap((runtime) =>
|
||||
Option.match(runtime, {
|
||||
onNone: () => Effect.succeed(Option.none<ProviderRuntimeBinding>()),
|
||||
@@ -101,9 +99,16 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
);
|
||||
|
||||
const upsert: ProviderSessionDirectoryShape["upsert"] = Effect.fn(function* (binding) {
|
||||
const existing = yield* repository
|
||||
.getByThreadId({ threadId: binding.threadId })
|
||||
.pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.upsert:getByThreadId")));
|
||||
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 existingRuntime = Option.getOrUndefined(existing);
|
||||
const resolvedThreadId = binding.threadId ?? existingRuntime?.threadId;
|
||||
@@ -145,7 +150,16 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
binding.runtimePayload,
|
||||
),
|
||||
})
|
||||
.pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.upsert:upsert")));
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.upsert:upsert",
|
||||
detail: "Failed to execute ProviderSessionDirectory.upsert:upsert.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const getProvider: ProviderSessionDirectoryShape["getProvider"] = (threadId) =>
|
||||
@@ -166,13 +180,27 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
||||
|
||||
const listThreadIds: ProviderSessionDirectoryShape["listThreadIds"] = () =>
|
||||
repository.list().pipe(
|
||||
Effect.mapError(toPersistenceError("ProviderSessionDirectory.listThreadIds:list")),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.listThreadIds:list",
|
||||
detail: "Failed to execute ProviderSessionDirectory.listThreadIds:list.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.map((rows) => rows.map((row) => row.threadId)),
|
||||
);
|
||||
|
||||
const listBindings: ProviderSessionDirectoryShape["listBindings"] = () =>
|
||||
repository.list().pipe(
|
||||
Effect.mapError(toPersistenceError("ProviderSessionDirectory.listBindings:list")),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ProviderSessionDirectoryPersistenceError({
|
||||
operation: "ProviderSessionDirectory.listBindings:list",
|
||||
detail: "Failed to execute ProviderSessionDirectory.listBindings:list.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.flatMap((rows) =>
|
||||
Effect.forEach(
|
||||
rows,
|
||||
@@ -195,7 +223,3 @@ export const ProviderSessionDirectoryLive = Layer.effect(
|
||||
ProviderSessionDirectory,
|
||||
makeProviderSessionDirectory,
|
||||
);
|
||||
|
||||
export function makeProviderSessionDirectoryLive() {
|
||||
return Layer.effect(ProviderSessionDirectory, makeProviderSessionDirectory);
|
||||
}
|
||||
@@ -67,6 +67,25 @@ function catalogError(operation: string, cause: unknown) {
|
||||
});
|
||||
}
|
||||
|
||||
function persistenceError(
|
||||
operation:
|
||||
| "list-targets"
|
||||
| "register-connection"
|
||||
| "remove-connection"
|
||||
| "load-shell"
|
||||
| "save-shell"
|
||||
| "load-thread"
|
||||
| "save-thread"
|
||||
| "remove-thread"
|
||||
| "clear-environment",
|
||||
cause: unknown,
|
||||
) {
|
||||
return new ConnectionPersistenceError({
|
||||
operation,
|
||||
message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`,
|
||||
});
|
||||
}
|
||||
|
||||
const openDatabase = Effect.fn("web.connectionStorage.openDatabase")(function* () {
|
||||
return yield* Effect.callback<IDBDatabase, ConnectionTransientError>((resume) => {
|
||||
if (typeof indexedDB === "undefined") {
|
||||
@@ -311,38 +330,18 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
const targetStore = ConnectionTargetStore.of({
|
||||
list: catalog.read.pipe(
|
||||
Effect.map((document) => document.targets),
|
||||
Effect.mapError(
|
||||
(cause) => new ConnectionPersistenceError({ operation: "list-targets", cause }),
|
||||
),
|
||||
Effect.mapError((cause) => persistenceError("list-targets", cause)),
|
||||
),
|
||||
});
|
||||
const registrationStore = ConnectionRegistrationStore.of({
|
||||
register: (registration) =>
|
||||
catalog
|
||||
.update((document) => registerConnectionInCatalog(document, registration))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "register-connection",
|
||||
environmentId: registration.target.environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
.pipe(Effect.mapError((cause) => persistenceError("register-connection", cause))),
|
||||
remove: (target) =>
|
||||
catalog
|
||||
.update((document) => removeConnectionFromCatalog(document, target))
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "remove-connection",
|
||||
environmentId: target.environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
.pipe(Effect.mapError((cause) => persistenceError("remove-connection", cause))),
|
||||
});
|
||||
const profileStore = ProfileStore.make({
|
||||
get: (connectionId) =>
|
||||
@@ -431,14 +430,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
return Effect.succeed(Option.none());
|
||||
}
|
||||
return decodeStoredShellSnapshot(raw).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.mapError((cause) => persistenceError("load-shell", cause)),
|
||||
Effect.map((stored) =>
|
||||
stored.environmentId === environmentId
|
||||
? Option.some(stored.snapshot)
|
||||
@@ -449,11 +441,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
Effect.mapError((cause) =>
|
||||
cause._tag === "ConnectionPersistenceError"
|
||||
? cause
|
||||
: new ConnectionPersistenceError({
|
||||
operation: "load-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
: persistenceError("load-shell", cause),
|
||||
),
|
||||
),
|
||||
saveShell: (environmentId, snapshot) =>
|
||||
@@ -462,26 +450,13 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION,
|
||||
environmentId,
|
||||
snapshot,
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "save-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
|
||||
yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded);
|
||||
}).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
cause._tag === "ConnectionPersistenceError"
|
||||
? cause
|
||||
: new ConnectionPersistenceError({
|
||||
operation: "save-shell",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
: persistenceError("save-shell", cause),
|
||||
),
|
||||
),
|
||||
loadThread: (environmentId, threadId) =>
|
||||
@@ -495,15 +470,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
return Effect.succeed(Option.none());
|
||||
}
|
||||
return decodeStoredThreadSnapshot(raw).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "load-thread",
|
||||
environmentId,
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.mapError((cause) => persistenceError("load-thread", cause)),
|
||||
Effect.map((stored) =>
|
||||
stored.environmentId === environmentId && stored.threadId === threadId
|
||||
? Option.some(stored.thread)
|
||||
@@ -514,12 +481,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
Effect.mapError((cause) =>
|
||||
cause._tag === "ConnectionPersistenceError"
|
||||
? cause
|
||||
: new ConnectionPersistenceError({
|
||||
operation: "load-thread",
|
||||
environmentId,
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
: persistenceError("load-thread", cause),
|
||||
),
|
||||
),
|
||||
saveThread: (environmentId, thread) =>
|
||||
@@ -529,17 +491,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
environmentId,
|
||||
threadId: thread.id,
|
||||
thread,
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "save-thread",
|
||||
environmentId,
|
||||
threadId: thread.id,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause)));
|
||||
yield* writeDatabaseValue(
|
||||
database,
|
||||
THREAD_STORE_NAME,
|
||||
@@ -550,12 +502,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
Effect.mapError((cause) =>
|
||||
cause._tag === "ConnectionPersistenceError"
|
||||
? cause
|
||||
: new ConnectionPersistenceError({
|
||||
operation: "save-thread",
|
||||
environmentId,
|
||||
threadId: thread.id,
|
||||
cause,
|
||||
}),
|
||||
: persistenceError("save-thread", cause),
|
||||
),
|
||||
),
|
||||
removeThread: (environmentId, threadId) =>
|
||||
@@ -563,17 +510,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
database,
|
||||
THREAD_STORE_NAME,
|
||||
threadCacheKey(environmentId, threadId),
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "remove-thread",
|
||||
environmentId,
|
||||
threadId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
).pipe(Effect.mapError((cause) => persistenceError("remove-thread", cause))),
|
||||
clear: (environmentId) =>
|
||||
Effect.all(
|
||||
[
|
||||
@@ -585,16 +522,7 @@ export const connectionStorageLayer = Layer.effectContext(
|
||||
),
|
||||
],
|
||||
{ concurrency: "unbounded", discard: true },
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ConnectionPersistenceError({
|
||||
operation: "clear-environment",
|
||||
environmentId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
).pipe(Effect.mapError((cause) => persistenceError("clear-environment", cause))),
|
||||
});
|
||||
|
||||
return Context.make(ConnectionTargetStore, targetStore).pipe(
|
||||
|
||||
@@ -694,7 +694,7 @@ describe("EnvironmentRegistry", () => {
|
||||
Effect.fail(
|
||||
new Persistence.ConnectionPersistenceError({
|
||||
operation: "remove-connection",
|
||||
cause: new Error("Storage is unavailable."),
|
||||
message: "Storage is unavailable.",
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { ConnectionPersistenceError } from "./persistence.ts";
|
||||
|
||||
describe("ConnectionPersistenceError", () => {
|
||||
it("retains resource context and the exact underlying failure", () => {
|
||||
const cause = new Error("disk unavailable");
|
||||
const error = new ConnectionPersistenceError({
|
||||
operation: "load-thread",
|
||||
environmentId: EnvironmentId.make("environment-1"),
|
||||
threadId: ThreadId.make("thread-1"),
|
||||
cause,
|
||||
});
|
||||
|
||||
expect(error).toMatchObject({
|
||||
operation: "load-thread",
|
||||
environmentId: "environment-1",
|
||||
threadId: "thread-1",
|
||||
cause,
|
||||
});
|
||||
expect(error.message).toBe("Could not load thread.");
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,8 @@
|
||||
import {
|
||||
EnvironmentId,
|
||||
type EnvironmentId,
|
||||
type OrchestrationThread,
|
||||
type OrchestrationShellSnapshot,
|
||||
ThreadId,
|
||||
type ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
@@ -26,15 +26,9 @@ export class ConnectionPersistenceError extends Schema.TaggedErrorClass<Connecti
|
||||
"remove-thread",
|
||||
"clear-environment",
|
||||
]),
|
||||
environmentId: Schema.optionalKey(EnvironmentId),
|
||||
threadId: Schema.optionalKey(ThreadId),
|
||||
cause: Schema.Defect(),
|
||||
message: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Could not ${this.operation.replaceAll("-", " ")}.`;
|
||||
}
|
||||
}
|
||||
) {}
|
||||
|
||||
export class ConnectionTargetStore extends Context.Service<
|
||||
ConnectionTargetStore,
|
||||
|
||||
Reference in new issue
Block a user