Skip to content

Commit 8c330cb

Browse files
committed
correct type errors
1 parent 44d91ec commit 8c330cb

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

core/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export interface CompletionOptions extends BaseCompletionOptions {
308308
model: string;
309309
}
310310

311-
export type ChatMessageRole = "user" | "assistant" | "system" | "tool";
311+
export type ChatMessageRole = "user" | "assistant" | "thinking" | "system" | "tool";
312312

313313
export type TextMessagePart = {
314314
type: "text";

core/llm/countTokens.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class LlamaEncoding implements Encoding {
2727
}
2828

2929
class NonWorkerAsyncEncoder implements AsyncEncoder {
30-
constructor(private readonly encoding: Encoding) {}
30+
constructor(private readonly encoding: Encoding) { }
3131

32-
async close(): Promise<void> {}
32+
async close(): Promise<void> { }
3333

3434
async encode(text: string): Promise<number[]> {
3535
return this.encoding.encode(text);
@@ -366,6 +366,7 @@ function chatMessageIsEmpty(message: ChatMessage): boolean {
366366
message.content.trim() === "" &&
367367
!message.toolCalls
368368
);
369+
case "thinking":
369370
case "tool":
370371
return false;
371372
}
@@ -383,8 +384,8 @@ function compileChatMessages(
383384
): ChatMessage[] {
384385
let msgsCopy = msgs
385386
? msgs
386-
.map((msg) => ({ ...msg }))
387-
.filter((msg) => !chatMessageIsEmpty(msg) && msg.role !== "system")
387+
.map((msg) => ({ ...msg }))
388+
.filter((msg) => !chatMessageIsEmpty(msg) && msg.role !== "system")
388389
: [];
389390

390391
msgsCopy = addSpaceToAnyEmptyMessages(msgsCopy);
@@ -469,5 +470,6 @@ export {
469470
pruneLinesFromTop,
470471
pruneRawPromptFromTop,
471472
pruneStringFromBottom,
472-
pruneStringFromTop,
473+
pruneStringFromTop
473474
};
475+

core/llm/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ export abstract class BaseLLM implements ILLM {
181181
options.completionOptions?.maxTokens ??
182182
(llmInfo?.maxCompletionTokens
183183
? Math.min(
184-
llmInfo.maxCompletionTokens,
185-
// Even if the model has a large maxTokens, we don't want to use that every time,
186-
// because it takes away from the context length
187-
this.contextLength / 4,
188-
)
184+
llmInfo.maxCompletionTokens,
185+
// Even if the model has a large maxTokens, we don't want to use that every time,
186+
// because it takes away from the context length
187+
this.contextLength / 4,
188+
)
189189
: DEFAULT_MAX_TOKENS),
190190
};
191191
this.requestOptions = options.requestOptions;
@@ -811,8 +811,8 @@ export abstract class BaseLLM implements ILLM {
811811
signal,
812812
completionOptions,
813813
)) {
814-
completion += chunk.content;
815-
yield chunk;
814+
815+
if (chunk.role === "assistant") {
816816
completion += chunk.content;
817817
yield chunk;
818818
}
@@ -912,15 +912,15 @@ export abstract class BaseLLM implements ILLM {
912912
);
913913
}
914914

915-
protected async *_streamComplete(
915+
protected async * _streamComplete(
916916
prompt: string,
917917
signal: AbortSignal,
918918
options: CompletionOptions,
919919
): AsyncGenerator<string> {
920920
throw new Error("Not implemented");
921921
}
922922

923-
protected async *_streamChat(
923+
protected async * _streamChat(
924924
messages: ChatMessage[],
925925
signal: AbortSignal,
926926
options: CompletionOptions,

core/llm/llms/Ollama.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { JSONSchema7, JSONSchema7Object } from "json-schema";
22

3-
import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js";
3+
import { ChatMessage, ChatMessageRole, CompletionOptions, LLMOptions } from "../../index.js";
44
import { renderChatMessage } from "../../util/messageContent.js";
55
import { BaseLLM } from "../index.js";
66
import { streamResponse } from "../stream.js";
77

88
type OllamaChatMessage = {
9-
role: "tool" | "user" | "assistant" | "system";
9+
role: ChatMessageRole;
1010
content: string;
1111
images?: string[] | null;
1212
tool_calls?: {
@@ -82,10 +82,10 @@ type OllamaBaseResponse = {
8282
model: string;
8383
created_at: string;
8484
} & (
85-
| {
85+
| {
8686
done: false;
8787
}
88-
| {
88+
| {
8989
done: true;
9090
done_reason: string;
9191
total_duration: number; // Time spent generating the response in nanoseconds
@@ -96,7 +96,7 @@ type OllamaBaseResponse = {
9696
eval_duration: number; // Time spent generating the response in nanoseconds
9797
context: number[]; // An encoding of the conversation used in this response; can be sent in the next request to keep conversational memory
9898
}
99-
);
99+
);
100100

101101
type OllamaErrorResponse = {
102102
error: string;
@@ -105,14 +105,14 @@ type OllamaErrorResponse = {
105105
type OllamaRawResponse =
106106
| OllamaErrorResponse
107107
| (OllamaBaseResponse & {
108-
response: string; // the generated response
109-
});
108+
response: string; // the generated response
109+
});
110110

111111
type OllamaChatResponse =
112112
| OllamaErrorResponse
113113
| (OllamaBaseResponse & {
114-
message: OllamaChatMessage;
115-
});
114+
message: OllamaChatMessage;
115+
});
116116

117117
interface OllamaTool {
118118
type: "function";
@@ -142,7 +142,7 @@ class Ollama extends BaseLLM {
142142
const headers: Record<string, string> = {
143143
"Content-Type": "application/json",
144144
};
145-
145+
146146
if (this.apiKey) {
147147
headers.Authorization = `Bearer ${this.apiKey}`;
148148
}
@@ -331,7 +331,7 @@ class Ollama extends BaseLLM {
331331
const headers: Record<string, string> = {
332332
"Content-Type": "application/json",
333333
};
334-
334+
335335
if (this.apiKey) {
336336
headers.Authorization = `Bearer ${this.apiKey}`;
337337
}
@@ -396,7 +396,7 @@ class Ollama extends BaseLLM {
396396
const headers: Record<string, string> = {
397397
"Content-Type": "application/json",
398398
};
399-
399+
400400
if (this.apiKey) {
401401
headers.Authorization = `Bearer ${this.apiKey}`;
402402
}
@@ -482,7 +482,7 @@ class Ollama extends BaseLLM {
482482
const headers: Record<string, string> = {
483483
"Content-Type": "application/json",
484484
};
485-
485+
486486
if (this.apiKey) {
487487
headers.Authorization = `Bearer ${this.apiKey}`;
488488
}
@@ -523,7 +523,7 @@ class Ollama extends BaseLLM {
523523
const headers: Record<string, string> = {
524524
"Content-Type": "application/json",
525525
};
526-
526+
527527
if (this.apiKey) {
528528
headers.Authorization = `Bearer ${this.apiKey}`;
529529
}
@@ -549,7 +549,7 @@ class Ollama extends BaseLLM {
549549
const headers: Record<string, string> = {
550550
"Content-Type": "application/json",
551551
};
552-
552+
553553
if (this.apiKey) {
554554
headers.Authorization = `Bearer ${this.apiKey}`;
555555
}

core/util/messageContent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function renderChatMessage(message: ChatMessage): string {
2121
switch (message.role) {
2222
case "user":
2323
case "assistant":
24+
case "thinking":
2425
case "system":
2526
return stripImages(message.content);
2627
case "tool":
@@ -36,6 +37,7 @@ export function normalizeToMessageParts(message: ChatMessage): MessagePart[] {
3637
switch (message.role) {
3738
case "user":
3839
case "assistant":
40+
case "thinking":
3941
case "system":
4042
return Array.isArray(message.content)
4143
? message.content

0 commit comments

Comments
 (0)