Files
t3code/apps/server/src/persistence/Layers/ProjectionThreadSessions.ts
T
Julius Marminge 26f631b638 Migrate provider runtime/session flow to thread-scoped IDs
- Replace provider session-id centric plumbing with thread-id based runtime handling across server orchestration and provider layers
- Update persistence repositories/migrations and contracts for threadless runtime semantics
- Refresh integration/unit tests and web session/store types to match the new runtime model
2026-03-05 23:00:41 -08:00

108 lines
3.3 KiB
TypeScript

import * as SqlClient from "effect/unstable/sql/SqlClient";
import * as SqlSchema from "effect/unstable/sql/SqlSchema";
import { Effect, Layer } from "effect";
import { toPersistenceSqlError } from "../Errors.ts";
import {
ProjectionThreadSession,
ProjectionThreadSessionRepository,
type ProjectionThreadSessionRepositoryShape,
DeleteProjectionThreadSessionInput,
GetProjectionThreadSessionInput,
} from "../Services/ProjectionThreadSessions.ts";
const makeProjectionThreadSessionRepository = Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
const upsertProjectionThreadSessionRow = SqlSchema.void({
Request: ProjectionThreadSession,
execute: (row) =>
sql`
INSERT INTO projection_thread_sessions (
thread_id,
status,
provider_name,
runtime_mode,
active_turn_id,
last_error,
updated_at
)
VALUES (
${row.threadId},
${row.status},
${row.providerName},
${row.runtimeMode},
${row.activeTurnId},
${row.lastError},
${row.updatedAt}
)
ON CONFLICT (thread_id)
DO UPDATE SET
status = excluded.status,
provider_name = excluded.provider_name,
runtime_mode = excluded.runtime_mode,
active_turn_id = excluded.active_turn_id,
last_error = excluded.last_error,
updated_at = excluded.updated_at
`,
});
const getProjectionThreadSessionRow = SqlSchema.findOneOption({
Request: GetProjectionThreadSessionInput,
Result: ProjectionThreadSession,
execute: ({ threadId }) =>
sql`
SELECT
thread_id AS "threadId",
status,
provider_name AS "providerName",
runtime_mode AS "runtimeMode",
active_turn_id AS "activeTurnId",
last_error AS "lastError",
updated_at AS "updatedAt"
FROM projection_thread_sessions
WHERE thread_id = ${threadId}
`,
});
const deleteProjectionThreadSessionRow = SqlSchema.void({
Request: DeleteProjectionThreadSessionInput,
execute: ({ threadId }) =>
sql`
DELETE FROM projection_thread_sessions
WHERE thread_id = ${threadId}
`,
});
const upsert: ProjectionThreadSessionRepositoryShape["upsert"] = (row) =>
upsertProjectionThreadSessionRow(row).pipe(
Effect.mapError(toPersistenceSqlError("ProjectionThreadSessionRepository.upsert:query")),
);
const getByThreadId: ProjectionThreadSessionRepositoryShape["getByThreadId"] = (input) =>
getProjectionThreadSessionRow(input).pipe(
Effect.mapError(
toPersistenceSqlError("ProjectionThreadSessionRepository.getByThreadId:query"),
),
);
const deleteByThreadId: ProjectionThreadSessionRepositoryShape["deleteByThreadId"] = (input) =>
deleteProjectionThreadSessionRow(input).pipe(
Effect.mapError(
toPersistenceSqlError("ProjectionThreadSessionRepository.deleteByThreadId:query"),
),
);
return {
upsert,
getByThreadId,
deleteByThreadId,
} satisfies ProjectionThreadSessionRepositoryShape;
});
export const ProjectionThreadSessionRepositoryLive = Layer.effect(
ProjectionThreadSessionRepository,
makeProjectionThreadSessionRepository,
);