Skip to content

Commit 625d43c

Browse files
authored
Merge pull request #7147 from uinstinct/telemetry-premature-close
chore: add premature close to telemetry
2 parents 39e7edd + b53e632 commit 625d43c

File tree

1 file changed

+119
-97
lines changed

1 file changed

+119
-97
lines changed

core/llm/streamChat.ts

Lines changed: 119 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -48,114 +48,136 @@ export async function* llmStreamChat(
4848
},
4949
};
5050

51-
if (legacySlashCommandData) {
52-
const { command, contextItems, historyIndex, input, selectedCode } =
53-
legacySlashCommandData;
54-
const slashCommand = config.slashCommands?.find(
55-
(sc) => sc.name === command.name,
56-
);
57-
if (!slashCommand) {
58-
throw new Error(`Unknown slash command ${command.name}`);
59-
}
60-
void Telemetry.capture(
61-
"useSlashCommand",
62-
{
63-
name: command.name,
64-
},
65-
true,
66-
);
67-
if (!slashCommand.run) {
68-
console.error(
69-
`Slash command ${command.name} (${command.source}) has no run function`,
51+
try {
52+
if (legacySlashCommandData) {
53+
const { command, contextItems, historyIndex, input, selectedCode } =
54+
legacySlashCommandData;
55+
const slashCommand = config.slashCommands?.find(
56+
(sc) => sc.name === command.name,
7057
);
71-
throw new Error(`Slash command not found`);
72-
}
73-
74-
const gen = slashCommand.run({
75-
input,
76-
history: messages,
77-
llm: model,
78-
contextItems,
79-
params: command.params,
80-
ide,
81-
addContextItem: (item) => {
82-
void messenger.request("addContextItem", {
83-
item,
84-
historyIndex,
85-
});
86-
},
87-
selectedCode,
88-
config,
89-
fetch: (url, init) =>
90-
fetchwithRequestOptions(
91-
url,
92-
{
93-
...init,
94-
signal: abortController.signal,
95-
},
96-
config.requestOptions,
97-
),
98-
completionOptions,
99-
abortController,
100-
});
101-
let next = await gen.next();
102-
while (!next.done) {
103-
if (abortController.signal.aborted) {
104-
next = await gen.return(errorPromptLog);
105-
break;
58+
if (!slashCommand) {
59+
throw new Error(`Unknown slash command ${command.name}`);
10660
}
107-
if (next.value) {
108-
yield {
109-
role: "assistant",
110-
content: next.value,
111-
};
61+
void Telemetry.capture(
62+
"useSlashCommand",
63+
{
64+
name: command.name,
65+
},
66+
true,
67+
);
68+
if (!slashCommand.run) {
69+
console.error(
70+
`Slash command ${command.name} (${command.source}) has no run function`,
71+
);
72+
throw new Error(`Slash command not found`);
11273
}
113-
next = await gen.next();
114-
}
115-
if (!next.done) {
116-
throw new Error("Will never happen");
117-
}
11874

119-
return next.value;
120-
} else {
121-
const gen = model.streamChat(
122-
messages,
123-
abortController.signal,
124-
completionOptions,
125-
messageOptions,
126-
);
127-
let next = await gen.next();
128-
while (!next.done) {
129-
if (abortController.signal.aborted) {
130-
next = await gen.return(errorPromptLog);
131-
break;
75+
const gen = slashCommand.run({
76+
input,
77+
history: messages,
78+
llm: model,
79+
contextItems,
80+
params: command.params,
81+
ide,
82+
addContextItem: (item) => {
83+
void messenger.request("addContextItem", {
84+
item,
85+
historyIndex,
86+
});
87+
},
88+
selectedCode,
89+
config,
90+
fetch: (url, init) =>
91+
fetchwithRequestOptions(
92+
url,
93+
{
94+
...init,
95+
signal: abortController.signal,
96+
},
97+
config.requestOptions,
98+
),
99+
completionOptions,
100+
abortController,
101+
});
102+
let next = await gen.next();
103+
while (!next.done) {
104+
if (abortController.signal.aborted) {
105+
next = await gen.return(errorPromptLog);
106+
break;
107+
}
108+
if (next.value) {
109+
yield {
110+
role: "assistant",
111+
content: next.value,
112+
};
113+
}
114+
next = await gen.next();
115+
}
116+
if (!next.done) {
117+
throw new Error("Will never happen");
132118
}
133119

134-
const chunk = next.value;
120+
return next.value;
121+
} else {
122+
const gen = model.streamChat(
123+
messages,
124+
abortController.signal,
125+
completionOptions,
126+
messageOptions,
127+
);
128+
let next = await gen.next();
129+
while (!next.done) {
130+
if (abortController.signal.aborted) {
131+
next = await gen.return(errorPromptLog);
132+
break;
133+
}
135134

136-
yield chunk;
137-
next = await gen.next();
138-
}
139-
if (config.experimental?.readResponseTTS && "completion" in next.value) {
140-
void TTS.read(next.value?.completion);
141-
}
135+
const chunk = next.value;
142136

143-
void Telemetry.capture(
144-
"chat",
145-
{
146-
model: model.model,
147-
provider: model.providerName,
148-
},
149-
true,
150-
);
137+
yield chunk;
138+
next = await gen.next();
139+
}
140+
if (config.experimental?.readResponseTTS && "completion" in next.value) {
141+
void TTS.read(next.value?.completion);
142+
}
151143

152-
void checkForFreeTrialExceeded(configHandler, messenger);
144+
void Telemetry.capture(
145+
"chat",
146+
{
147+
model: model.model,
148+
provider: model.providerName,
149+
},
150+
true,
151+
);
153152

154-
if (!next.done) {
155-
throw new Error("Will never happen");
156-
}
153+
void checkForFreeTrialExceeded(configHandler, messenger);
157154

158-
return next.value;
155+
if (!next.done) {
156+
throw new Error("Will never happen");
157+
}
158+
159+
return next.value;
160+
}
161+
} catch (error) {
162+
if (
163+
error instanceof Error &&
164+
error.message.toLowerCase().includes("premature close")
165+
) {
166+
void Telemetry.capture(
167+
"stream_premature_close_error",
168+
{
169+
model: model.model,
170+
provider: model.providerName,
171+
errorMessage: error.message,
172+
context: legacySlashCommandData ? "slash_command" : "regular_chat",
173+
...(legacySlashCommandData && {
174+
command: legacySlashCommandData.command.name,
175+
}),
176+
},
177+
false,
178+
);
179+
}
180+
throw error;
159181
}
160182
}
161183

0 commit comments

Comments
 (0)