@@ -9,12 +9,11 @@ import * as FileSystem from "effect/FileSystem";
99import * as Layer from "effect/Layer" ;
1010import * as Path from "effect/Path" ;
1111import * as Schema from "effect/Schema" ;
12- import { FetchHttpClient , HttpClient , HttpClientRequest } from "effect/http" ;
1312import * as CodexTextGeneration from "../../apps/server/src/textGeneration/CodexTextGeneration.ts" ;
1413import * as ServerConfig from "../../apps/server/src/config.ts" ;
15- import { morphConfig } from "../shared/proxy.ts" ;
1614import { condensedCases , leafCases , type LeafMessage } from "./cases.ts" ;
1715import { gradeCondensed , gradeLeaf } from "./grade.ts" ;
16+ import { morphRunner } from "./morph.ts" ;
1817
1918/**
2019 * How a peer reply reaches the model. `main` sends it as a nameless assistant message, as main
@@ -63,59 +62,6 @@ const Results = Schema.Array(
6362const decodeSettings = Schema . decodeUnknownSync ( CodexSettings ) ;
6463const decodeResults = Schema . decodeEffect ( Schema . fromJsonString ( Results ) ) ;
6564const encodeResults = Schema . encodeEffect ( Schema . fromJsonString ( Results ) ) ;
66- const decodeMorphReply = Schema . decodeUnknownEffect (
67- Schema . fromJsonString (
68- Schema . Struct ( {
69- output : Schema . Array (
70- Schema . Struct ( {
71- type : Schema . String ,
72- content : Schema . optional (
73- Schema . Array ( Schema . Struct ( { text : Schema . optional ( Schema . String ) } ) ) ,
74- ) ,
75- } ) ,
76- ) ,
77- } ) ,
78- ) ,
79- ) ;
80-
81- const MORPH_ATTEMPTS = 8 ;
82-
83- /** Morph's free Responses API, given the production prompt as OpenCode sends it: text only. */
84- const morphRunner = ( ) : TextGenerationOperations . Runner => {
85- const endpoint = morphConfig ( ) ;
86- return ( request ) =>
87- Effect . gen ( function * ( ) {
88- const client = yield * HttpClient . HttpClient ;
89- const post = HttpClientRequest . post ( `${ endpoint . url } /responses` ) . pipe (
90- HttpClientRequest . setHeaders ( endpoint . headers ) ,
91- HttpClientRequest . bodyJson ( {
92- model : request . modelSelection . model ,
93- input : [ { role : "user" , content : [ { type : "input_text" , text : request . prompt } ] } ] ,
94- store : false ,
95- max_output_tokens : 8_000 ,
96- } ) ,
97- Effect . flatMap ( client . execute ) ,
98- ) ;
99- let response = yield * post ;
100- // Morph limits input tokens per minute and says when to come back.
101- for ( let attempt = 1 ; response . status === 429 || response . status >= 500 ; attempt ++ ) {
102- if ( attempt >= MORPH_ATTEMPTS ) break ;
103- const retryAfter = Number ( response . headers [ "retry-after" ] ) ;
104- yield * Effect . sleep ( `${ retryAfter > 0 ? Math . min ( retryAfter , 60 ) : 2 ** attempt } seconds` ) ;
105- response = yield * post ;
106- }
107- // The body is omitted so a credential echoed in an error never reaches the log.
108- if ( response . status !== 200 ) return yield * Effect . die ( `Morph HTTP ${ response . status } ` ) ;
109- const reply = yield * decodeMorphReply ( yield * response . text ) ;
110- const text = reply . output
111- . filter ( ( item ) => item . type === "message" )
112- . flatMap ( ( item ) => item . content ?? [ ] )
113- . map ( ( part ) => part . text ?? "" )
114- . join ( "" ) ;
115- return yield * TextGenerationOperations . decodeJsonReply ( request , "Morph" , text ) ;
116- } ) . pipe ( Effect . orDie , Effect . provide ( FetchHttpClient . layer ) ) ;
117- } ;
118-
11965const asArm = ( message : LeafMessage , index : number ) => {
12066 const ref = `M${ index + 1 } ` ;
12167 if ( message . role !== "peer" || arm === "full" ) return { ref, ...message } ;
0 commit comments