Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aaafc61b6 |
No files matched your search
@@ -74,18 +74,6 @@ const ProviderRollbackConversationInput = Schema.Struct({
|
|||||||
numTurns: NonNegativeInt,
|
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: {
|
const decodeInputOrValidationError = <S extends Schema.Top>(input: {
|
||||||
readonly operation: string;
|
readonly operation: string;
|
||||||
readonly schema: S;
|
readonly schema: S;
|
||||||
@@ -248,12 +236,12 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
payload.providerInstanceId !== undefined
|
payload.providerInstanceId !== undefined
|
||||||
? Effect.succeed(payload.providerInstanceId)
|
? Effect.succeed(payload.providerInstanceId)
|
||||||
: Effect.fail(
|
: Effect.fail(
|
||||||
toValidationError(
|
new ProviderValidationError({
|
||||||
operation,
|
operation,
|
||||||
payload.provider
|
issue: payload.provider
|
||||||
? `Provider instance id is required for provider '${payload.provider}'.`
|
? `Provider instance id is required for provider '${payload.provider}'.`
|
||||||
: "Provider instance id is required.",
|
: "Provider instance id is required.",
|
||||||
),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const upsertSessionBinding = (
|
const upsertSessionBinding = (
|
||||||
@@ -388,10 +376,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!hasResumeCursor) {
|
if (!hasResumeCursor) {
|
||||||
return yield* toValidationError(
|
return yield* new ProviderValidationError({
|
||||||
input.operation,
|
operation: input.operation,
|
||||||
`Cannot recover thread '${input.binding.threadId}' because no provider resume state is persisted.`,
|
issue: `Cannot recover thread '${input.binding.threadId}' because no provider resume state is persisted.`,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const persistedCwd = readPersistedCwd(input.binding.runtimePayload);
|
const persistedCwd = readPersistedCwd(input.binding.runtimePayload);
|
||||||
@@ -411,10 +399,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
.pipe(Effect.onError(() => clearMcpSession(input.binding.threadId)));
|
.pipe(Effect.onError(() => clearMcpSession(input.binding.threadId)));
|
||||||
if (resumed.provider !== adapter.provider) {
|
if (resumed.provider !== adapter.provider) {
|
||||||
yield* clearMcpSession(input.binding.threadId);
|
yield* clearMcpSession(input.binding.threadId);
|
||||||
return yield* toValidationError(
|
return yield* new ProviderValidationError({
|
||||||
input.operation,
|
operation: input.operation,
|
||||||
`Adapter/provider mismatch while recovering thread '${input.binding.threadId}'. Expected '${adapter.provider}', received '${resumed.provider}'.`,
|
issue: `Adapter/provider mismatch while recovering thread '${input.binding.threadId}'. Expected '${adapter.provider}', received '${resumed.provider}'.`,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
yield* upsertSessionBinding(
|
yield* upsertSessionBinding(
|
||||||
@@ -445,10 +433,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
const bindingOption = yield* directory.getBinding(input.threadId);
|
const bindingOption = yield* directory.getBinding(input.threadId);
|
||||||
const binding = Option.getOrUndefined(bindingOption);
|
const binding = Option.getOrUndefined(bindingOption);
|
||||||
if (!binding) {
|
if (!binding) {
|
||||||
return yield* toValidationError(
|
return yield* new ProviderValidationError({
|
||||||
input.operation,
|
operation: input.operation,
|
||||||
`Cannot route thread '${input.threadId}' because no persisted provider binding exists.`,
|
issue: `Cannot route thread '${input.threadId}' because no persisted provider binding exists.`,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
const instanceId = yield* requireBindingInstanceId(input.operation, binding);
|
const instanceId = yield* requireBindingInstanceId(input.operation, binding);
|
||||||
const adapter = yield* registry.getByInstance(instanceId);
|
const adapter = yield* registry.getByInstance(instanceId);
|
||||||
@@ -543,10 +531,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
const resolvedProvider = instanceInfo.driverKind;
|
const resolvedProvider = instanceInfo.driverKind;
|
||||||
metricProvider = resolvedProvider;
|
metricProvider = resolvedProvider;
|
||||||
if (parsed.provider !== undefined && parsed.provider !== resolvedProvider) {
|
if (parsed.provider !== undefined && parsed.provider !== resolvedProvider) {
|
||||||
return yield* toValidationError(
|
return yield* new ProviderValidationError({
|
||||||
"ProviderService.startSession",
|
operation: "ProviderService.startSession",
|
||||||
`Provider instance '${resolvedInstanceId}' belongs to driver '${resolvedProvider}', not '${parsed.provider}'.`,
|
issue: `Provider instance '${resolvedInstanceId}' belongs to driver '${resolvedProvider}', not '${parsed.provider}'.`,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
const input = {
|
const input = {
|
||||||
...parsed,
|
...parsed,
|
||||||
@@ -554,10 +542,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
provider: resolvedProvider,
|
provider: resolvedProvider,
|
||||||
};
|
};
|
||||||
if (!instanceInfo.enabled) {
|
if (!instanceInfo.enabled) {
|
||||||
return yield* toValidationError(
|
return yield* new ProviderValidationError({
|
||||||
"ProviderService.startSession",
|
operation: "ProviderService.startSession",
|
||||||
`Provider instance '${resolvedInstanceId}' is disabled in T3 Code settings.`,
|
issue: `Provider instance '${resolvedInstanceId}' is disabled in T3 Code settings.`,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
const persistedBinding = Option.getOrUndefined(yield* directory.getBinding(threadId));
|
const persistedBinding = Option.getOrUndefined(yield* directory.getBinding(threadId));
|
||||||
const effectiveResumeCursor =
|
const effectiveResumeCursor =
|
||||||
@@ -602,10 +590,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
|
|
||||||
if (session.provider !== adapter.provider) {
|
if (session.provider !== adapter.provider) {
|
||||||
yield* clearMcpSession(threadId);
|
yield* clearMcpSession(threadId);
|
||||||
return yield* toValidationError(
|
return yield* new ProviderValidationError({
|
||||||
"ProviderService.startSession",
|
operation: "ProviderService.startSession",
|
||||||
`Adapter/provider mismatch: requested '${adapter.provider}', received '${session.provider}'.`,
|
issue: `Adapter/provider mismatch: requested '${adapter.provider}', received '${session.provider}'.`,
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
const sessionWithInstance = {
|
const sessionWithInstance = {
|
||||||
...session,
|
...session,
|
||||||
@@ -654,10 +642,10 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
|||||||
attachments: parsed.attachments ?? [],
|
attachments: parsed.attachments ?? [],
|
||||||
};
|
};
|
||||||
if (!input.input && input.attachments.length === 0) {
|
if (!input.input && input.attachments.length === 0) {
|
||||||
return yield* toValidationError(
|
return yield* new ProviderValidationError({
|
||||||
"ProviderService.sendTurn",
|
operation: "ProviderService.sendTurn",
|
||||||
"Either input text or at least one attachment is required",
|
issue: "Either input text or at least one attachment is required",
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
yield* Effect.annotateCurrentSpan({
|
yield* Effect.annotateCurrentSpan({
|
||||||
"provider.operation": "send-turn",
|
"provider.operation": "send-turn",
|
||||||
|
|||||||
@@ -15,15 +15,6 @@ import {
|
|||||||
} from "../Services/ProviderSessionDirectory.ts";
|
} from "../Services/ProviderSessionDirectory.ts";
|
||||||
const decodeProviderDriverKindValue = Schema.decodeUnknownEffect(ProviderDriverKind);
|
const decodeProviderDriverKindValue = Schema.decodeUnknownEffect(ProviderDriverKind);
|
||||||
|
|
||||||
function toPersistenceError(operation: string) {
|
|
||||||
return (cause: unknown) =>
|
|
||||||
new ProviderSessionDirectoryPersistenceError({
|
|
||||||
operation,
|
|
||||||
detail: `Failed to execute ${operation}.`,
|
|
||||||
cause,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function decodeProviderDriverKind(
|
function decodeProviderDriverKind(
|
||||||
providerName: string,
|
providerName: string,
|
||||||
operation: string,
|
operation: string,
|
||||||
@@ -88,7 +79,14 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
|||||||
|
|
||||||
const getBinding = (threadId: ThreadId) =>
|
const getBinding = (threadId: ThreadId) =>
|
||||||
repository.getByThreadId({ threadId }).pipe(
|
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) =>
|
Effect.flatMap((runtime) =>
|
||||||
Option.match(runtime, {
|
Option.match(runtime, {
|
||||||
onNone: () => Effect.succeed(Option.none<ProviderRuntimeBinding>()),
|
onNone: () => Effect.succeed(Option.none<ProviderRuntimeBinding>()),
|
||||||
@@ -101,9 +99,16 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const upsert: ProviderSessionDirectoryShape["upsert"] = Effect.fn(function* (binding) {
|
const upsert: ProviderSessionDirectoryShape["upsert"] = Effect.fn(function* (binding) {
|
||||||
const existing = yield* repository
|
const existing = yield* repository.getByThreadId({ threadId: binding.threadId }).pipe(
|
||||||
.getByThreadId({ threadId: binding.threadId })
|
Effect.mapError(
|
||||||
.pipe(Effect.mapError(toPersistenceError("ProviderSessionDirectory.upsert:getByThreadId")));
|
(cause) =>
|
||||||
|
new ProviderSessionDirectoryPersistenceError({
|
||||||
|
operation: "ProviderSessionDirectory.upsert:getByThreadId",
|
||||||
|
detail: "Failed to execute ProviderSessionDirectory.upsert:getByThreadId.",
|
||||||
|
cause,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
const existingRuntime = Option.getOrUndefined(existing);
|
const existingRuntime = Option.getOrUndefined(existing);
|
||||||
const resolvedThreadId = binding.threadId ?? existingRuntime?.threadId;
|
const resolvedThreadId = binding.threadId ?? existingRuntime?.threadId;
|
||||||
@@ -145,7 +150,16 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
|||||||
binding.runtimePayload,
|
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) =>
|
const getProvider: ProviderSessionDirectoryShape["getProvider"] = (threadId) =>
|
||||||
@@ -166,13 +180,27 @@ const makeProviderSessionDirectory = Effect.gen(function* () {
|
|||||||
|
|
||||||
const listThreadIds: ProviderSessionDirectoryShape["listThreadIds"] = () =>
|
const listThreadIds: ProviderSessionDirectoryShape["listThreadIds"] = () =>
|
||||||
repository.list().pipe(
|
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)),
|
Effect.map((rows) => rows.map((row) => row.threadId)),
|
||||||
);
|
);
|
||||||
|
|
||||||
const listBindings: ProviderSessionDirectoryShape["listBindings"] = () =>
|
const listBindings: ProviderSessionDirectoryShape["listBindings"] = () =>
|
||||||
repository.list().pipe(
|
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.flatMap((rows) =>
|
||||||
Effect.forEach(
|
Effect.forEach(
|
||||||
rows,
|
rows,
|
||||||
@@ -195,7 +223,3 @@ export const ProviderSessionDirectoryLive = Layer.effect(
|
|||||||
ProviderSessionDirectory,
|
ProviderSessionDirectory,
|
||||||
makeProviderSessionDirectory,
|
makeProviderSessionDirectory,
|
||||||
);
|
);
|
||||||
|
|
||||||
export function makeProviderSessionDirectoryLive() {
|
|
||||||
return Layer.effect(ProviderSessionDirectory, makeProviderSessionDirectory);
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user