Skip to content

Commit

Permalink
handle user cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jul 30, 2024
1 parent 30b3961 commit 9ede8b6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
8 changes: 7 additions & 1 deletion packages/cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export async function startServer(options: { port: string }) {
// add handler
const chatId = randomHex(6)
chats[chatId] = async (chunk) => {
if (!responseSoFar) {
trace.itemValue("model", chunk.model)
trace.appendContent("\n\n")
}
trace.appendToken(chunk.chunk)
responseSoFar += chunk.chunk ?? ""
tokensSoFar += chunk.tokens ?? 0
partialCb?.({
Expand All @@ -116,6 +121,8 @@ export async function startServer(options: { port: string }) {
})
finishReason = chunk.finishReason as any
if (finishReason) {
trace.appendContent("\n\n")
trace.itemValue(`finish reason`, finishReason)
delete chats[chatId]
resolve({ text: responseSoFar, finishReason })
}
Expand All @@ -129,7 +136,6 @@ export async function startServer(options: { port: string }) {
messages,
})
for (const ws of wss.clients) {
trace.log(`chat: sending request to client`)
ws.send(msg)
break
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/fence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function renderFencedVariables(vars: Fenced[]) {
validation,
args,
language,
}) => `- \`${k}\` ${
}) => `- ${k ? `\`${k}\`` : ""} ${
validation !== undefined
? `schema ${args.schema}: ${validation.valid === undefined ? EMOJI_UNDEFINED : validation.valid ? EMOJI_SUCCESS : EMOJI_FAIL}`
: "no label"
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,7 @@ export const OpenAIChatCompletion: ChatCompletionHandler = async (
if (typeof delta?.content == "string") {
numTokens += estimateTokens(delta.content, encoder)
chatResp += delta.content
if (delta.content)
trace.appendContent(
delta.content.includes("`")
? `\`\`\` ${delta.content.replace(/\n/g, " ")} \`\`\` `
: `\`${delta.content.replace(/\n/g, " ")}\` `
)
trace.appendToken(delta.content)
} else if (Array.isArray(delta.tool_calls)) {
const { tool_calls } = delta
for (const call of tool_calls) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/server/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export interface ChatCancel {
export interface ChatChunk extends RequestMessage {
type: "chat.chunk"
chatId: string
model?: string
finishReason?: string
chunk?: string
tokens?: number
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export class MarkdownTrace extends EventTarget implements ToolCallTrace {
}
}

appendToken(content: string) {
if (!content) return
this.appendContent(
content.includes("`")
? `\`\`\` ${content.replace(/\r?\n/g, " ")} \`\`\` `
: `\`${content.replace(/\r?\n/g, " ")}\` `
)
}

startDetails(title: string, success?: boolean) {
this.detailsDepth++
title = title?.trim() || ""
Expand Down
8 changes: 7 additions & 1 deletion packages/vscode/src/lmaccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ export function createChatModelRunner(
const token = new vscode.CancellationTokenSource().token
const { model, messages, modelOptions } = req
const chatModel = await pickChatModel(state, model)
if (!chatModel) throw new Error("No chat model selected.")
if (!chatModel) {
onChunk({
finishReason: "cancel",
})
return
}
const chatMessages = messagesToChatMessages(messages)
const request = await chatModel.sendRequest(
chatMessages,
Expand All @@ -213,6 +218,7 @@ export function createChatModelRunner(
chunk: fragment,
tokens: await chatModel.countTokens(text),
finishReason: undefined,
model: chatModel.id,
})
}
onChunk({
Expand Down

0 comments on commit 9ede8b6

Please sign in to comment.