Skip to content

Commit 606780c

Browse files
committed
refactor(server): move transcript export into thread management
1 parent 075b7aa commit 606780c

4 files changed

Lines changed: 80 additions & 40 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadManagementService.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ import {
2121
type OrchestrationV2Run,
2222
type OrchestrationV2ThreadShellSnapshot,
2323
type OrchestrationV2ThreadProjection,
24+
type OrchestrationV2ThreadTranscript,
2425
type OrchestrationV2ThreadShell,
2526
type OrchestrationV2TurnItem,
2627
ProjectId,
28+
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
2729
RunId,
2830
type ScheduledTaskId,
2931
ThreadId,
3032
type TurnItemId,
3133
} from "@t3tools/contracts";
34+
import { threadTranscriptHeader } from "@t3tools/shared/threadTranscript";
3235
import * as Context from "effect/Context";
3336
import * as DateTime from "effect/DateTime";
3437
import * as Duration from "effect/Duration";
@@ -39,6 +42,8 @@ import * as Schema from "effect/Schema";
3942
import * as Stream from "effect/Stream";
4043

4144
import * as Orchestrator from "./Orchestrator.ts";
45+
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
46+
import { projectedRowEncodedBytes } from "./threadHistoryPaging.ts";
4247
import { projectTurnItemForDetail } from "./WireProjection.ts";
4348
import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts";
4449

@@ -176,6 +181,15 @@ export class ThreadManagementThreadNotFoundError extends Schema.TaggedError<Thre
176181
}
177182
}
178183

184+
export class ThreadTranscriptTooLargeError extends Schema.TaggedError<ThreadTranscriptTooLargeError>()(
185+
"ThreadTranscriptTooLargeError",
186+
{ threadId: ThreadId },
187+
) {
188+
override get message(): string {
189+
return "The thread transcript exceeds the file attachment limit.";
190+
}
191+
}
192+
179193
export class ThreadManagementRunNotFoundError extends Schema.TaggedError<ThreadManagementRunNotFoundError>()(
180194
"ThreadManagementRunNotFoundError",
181195
{
@@ -304,6 +318,13 @@ export interface ThreadManagementServiceShape {
304318
) => Effect.Effect<OrchestrationV2ThreadProjection, Orchestrator.OrchestratorV2Error>;
305319
readonly getCheckpointContext: Orchestrator.OrchestratorV2["Service"]["getCheckpointContext"];
306320
readonly getThreadSnapshot: Orchestrator.OrchestratorV2["Service"]["getThreadSnapshot"];
321+
readonly getThreadTranscript: (
322+
threadId: ThreadId,
323+
) => Effect.Effect<
324+
OrchestrationV2ThreadTranscript,
325+
Orchestrator.OrchestratorV2Error | ThreadTranscriptTooLargeError,
326+
ServerEnvironment.ServerEnvironmentIdentity
327+
>;
307328
readonly getThreadSnapshotWindow: Orchestrator.OrchestratorV2["Service"]["getThreadSnapshotWindow"];
308329
readonly getProjectThreadRecords: <K extends ProjectionRecordField>(
309330
input: { readonly projectId: ProjectId; readonly threadId: ThreadId },
@@ -501,6 +522,32 @@ const make = Effect.gen(function* () {
501522
ensureProjectionTranscript(threadId).pipe(
502523
Effect.andThen(orchestrator.getThreadSnapshot(threadId)),
503524
);
525+
const getThreadTranscript = Effect.fn("orchestrationV2.threadManagement.getThreadTranscript")(
526+
function* (threadId: ThreadId) {
527+
const { projection } = yield* getThreadSnapshot(threadId);
528+
const environment = yield* ServerEnvironment.ServerEnvironmentIdentity;
529+
const transcript = {
530+
threadId: projection.thread.id,
531+
title: projection.thread.title,
532+
updatedAt: projection.updatedAt,
533+
items: projection.visibleTurnItems,
534+
};
535+
let sizeBytes = Buffer.byteLength(
536+
threadTranscriptHeader(yield* environment.getEnvironmentId, transcript),
537+
"utf8",
538+
);
539+
if (sizeBytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
540+
return yield* new ThreadTranscriptTooLargeError({ threadId });
541+
}
542+
for (const row of transcript.items) {
543+
sizeBytes += projectedRowEncodedBytes(row) + 1;
544+
if (sizeBytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
545+
return yield* new ThreadTranscriptTooLargeError({ threadId });
546+
}
547+
}
548+
return transcript;
549+
},
550+
);
504551
const getThreadSnapshotWindow: ThreadManagementServiceShape["getThreadSnapshotWindow"] = (
505552
threadId,
506553
options,
@@ -907,6 +954,7 @@ const make = Effect.gen(function* () {
907954
getThreadProjection,
908955
getCheckpointContext,
909956
getThreadSnapshot,
957+
getThreadTranscript,
910958
getThreadSnapshotWindow,
911959
getProjectThreadRecords,
912960
getProjectThread,

‎apps/server/src/orchestration-v2/http.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
215215
const response = yield* read(thread.id);
216216
expect(response.status).toBe(expectedStatus);
217217
if (expectedStatus === 400) {
218+
const error = yield* Context.get(services, ThreadManagementService.ThreadManagementService)
219+
.getThreadTranscript(thread.id)
220+
.pipe(
221+
Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, {
222+
getEnvironmentId: Effect.succeed(environmentId),
223+
}),
224+
Effect.flip,
225+
);
226+
expect(error).toBeInstanceOf(ThreadManagementService.ThreadTranscriptTooLargeError);
218227
expect(yield* Effect.promise(() => response.json())).toMatchObject({
219228
_tag: "EnvironmentRequestInvalidError",
220229
reason: "thread_transcript_too_large",

‎apps/server/src/orchestration-v2/http.ts‎

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {
22
AuthOrchestrationReadScope,
33
EnvironmentHttpApi,
4-
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
54
type OrchestrationProjectShell,
65
} from "@t3tools/contracts";
7-
import { threadTranscriptHeader } from "@t3tools/shared/threadTranscript";
86
import * as Effect from "effect/Effect";
97
import * as Predicate from "effect/Predicate";
8+
import * as Schema from "effect/Schema";
109
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
1110
import * as SqlClient from "effect/sql/SqlClient";
1211

@@ -25,7 +24,6 @@ import {
2524
buildBoundedThreadProjection,
2625
decodeThreadHistoryCursor,
2726
InvalidThreadHistoryCursorError,
28-
projectedRowEncodedBytes,
2927
THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
3028
THREAD_HISTORY_PAGE_POLICY,
3129
OLDER_THREAD_USER_TURN_LIMIT,
@@ -36,6 +34,10 @@ import { buildActiveShellSnapshot, loadShellSnapshotParts } from "./ShellStream.
3634
import { boundedSnapshotResponseFields } from "./ThreadStream.ts";
3735
import { projectThreadProjectionForWire } from "./WireProjection.ts";
3836

37+
const isThreadTranscriptTooLargeError = Schema.is(
38+
ThreadManagementService.ThreadTranscriptTooLargeError,
39+
);
40+
3941
function isThreadNotFound(error: unknown): boolean {
4042
return (
4143
Predicate.hasProperty(error, "cause") &&
@@ -187,44 +189,24 @@ export const layer = HttpApiBuilder.group(
187189
Effect.fn("environment.orchestration.threadTranscript")(function* (args) {
188190
yield* annotateEnvironmentRequest(args.endpoint.name);
189191
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
190-
const { projection } = yield* threadManagement
191-
.getThreadSnapshot(args.params.threadId)
192-
.pipe(
193-
Effect.catch(
194-
Effect.fnUntraced(function* (error) {
195-
if (isThreadNotFound(error)) {
196-
return yield* failEnvironmentNotFound("thread_not_found");
197-
}
198-
return yield* failEnvironmentInternal(
199-
"orchestration_thread_snapshot_failed",
200-
error,
201-
);
202-
}),
203-
),
204-
);
205-
let sizeBytes = Buffer.byteLength(
206-
threadTranscriptHeader(yield* environment.getEnvironmentId, {
207-
threadId: projection.thread.id,
208-
title: projection.thread.title,
209-
updatedAt: projection.updatedAt,
210-
}),
211-
"utf8",
192+
return yield* threadManagement.getThreadTranscript(args.params.threadId).pipe(
193+
Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, environment),
194+
traceLocalHandlerWork,
195+
Effect.catch(
196+
Effect.fnUntraced(function* (error) {
197+
if (isThreadTranscriptTooLargeError(error)) {
198+
return yield* failEnvironmentInvalidRequest("thread_transcript_too_large");
199+
}
200+
if (isThreadNotFound(error)) {
201+
return yield* failEnvironmentNotFound("thread_not_found");
202+
}
203+
return yield* failEnvironmentInternal(
204+
"orchestration_thread_snapshot_failed",
205+
error,
206+
);
207+
}),
208+
),
212209
);
213-
if (sizeBytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
214-
return yield* failEnvironmentInvalidRequest("thread_transcript_too_large");
215-
}
216-
for (const row of projection.visibleTurnItems) {
217-
sizeBytes += projectedRowEncodedBytes(row) + 1;
218-
if (sizeBytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
219-
return yield* failEnvironmentInvalidRequest("thread_transcript_too_large");
220-
}
221-
}
222-
return {
223-
threadId: projection.thread.id,
224-
title: projection.thread.title,
225-
updatedAt: projection.updatedAt,
226-
items: projection.visibleTurnItems,
227-
};
228210
}),
229211
)
230212
.handle(

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
203203
getThreadProjection: unused,
204204
getCheckpointContext: unused,
205205
getThreadSnapshot: unused,
206+
getThreadTranscript: unused,
206207
getThreadSnapshotWindow: unused,
207208
getProjectThreadRecords: () => Effect.die("unused project record read"),
208209
getProjectThread: unused,

0 commit comments

Comments
 (0)