MCPcopy Index your code
hub / github.com/microsoft/TypeChat / complete

Function complete

typescript/src/model.ts:168–202  ·  view source on GitHub ↗
(prompt: string | PromptSection[])

Source from the content-addressed store, hash-verified

166 return model;
167
168 async function complete(prompt: string | PromptSection[]) {
169 let retryCount = 0;
170 const retryMaxAttempts = model.retryMaxAttempts ?? 3;
171 const retryPauseMs = model.retryPauseMs ?? 1000;
172 const messages = typeof prompt === "string" ? [{ role: "user", content: prompt }] : prompt;
173 while (true) {
174 const options = {
175 method: "POST",
176 body: JSON.stringify({
177 ...defaultParams,
178 messages,
179 temperature: 0,
180 n: 1
181 }),
182 headers: {
183 "content-type": "application/json",
184 ...headers
185 }
186 }
187 const response = await fetch(url, options);
188 if (response.ok) {
189 const json = await response.json() as { choices: { message: PromptSection }[] };
190 if (typeof json.choices[0].message.content === "string") {
191 return success(json.choices[0].message.content ?? "");
192 } else {
193 return error(`REST API unexpected response format: ${JSON.stringify(json.choices[0].message.content)}`);
194 }
195 }
196 if (!isTransientHttpError(response.status) || retryCount >= retryMaxAttempts) {
197 return error(`REST API error ${response.status}: ${response.statusText}`);
198 }
199 await sleep(getRetryDelayMs(response, retryPauseMs, retryPauseMs * retryMaxAttempts));
200 retryCount++;
201 }
202 }
203}
204
205/**

Callers

nothing calls this directly

Calls 5

successFunction · 0.90
errorFunction · 0.90
isTransientHttpErrorFunction · 0.85
sleepFunction · 0.85
getRetryDelayMsFunction · 0.85

Tested by

no test coverage detected