@@ -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" ;
3235import * as Context from "effect/Context" ;
3336import * as DateTime from "effect/DateTime" ;
3437import * as Duration from "effect/Duration" ;
@@ -39,6 +42,8 @@ import * as Schema from "effect/Schema";
3942import * as Stream from "effect/Stream" ;
4043
4144import * as Orchestrator from "./Orchestrator.ts" ;
45+ import * as ServerEnvironment from "../environment/ServerEnvironment.ts" ;
46+ import { projectedRowEncodedBytes } from "./threadHistoryPaging.ts" ;
4247import { projectTurnItemForDetail } from "./WireProjection.ts" ;
4348import * 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+
179193export 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,
0 commit comments