Skip to content

Commit e844408

Browse files
committed
add logo, fixup cache logic
1 parent d1fc087 commit e844408

File tree

2 files changed

+60
-28
lines changed

2 files changed

+60
-28
lines changed

node/chat/chat.ts

+43-25
Original file line numberDiff line numberDiff line change
@@ -537,33 +537,41 @@ ${msg.error.stack}`,
537537
model,
538538
dispatch,
539539
}) => {
540-
return d`${model.messages.map(
541-
(m, idx) =>
542-
d`${messageModel.view({
543-
model: m,
544-
toolManager: model.toolManager,
545-
dispatch: (msg) => {
546-
dispatch({ type: "message-msg", msg, idx });
547-
},
548-
})}\n`,
549-
)}${
550-
model.conversation.state == "message-in-flight"
551-
? d`Awaiting response ${
552-
MESSAGE_ANIMATION[
553-
Math.floor(
554-
(new Date().getTime() - model.conversation.sendDate.getTime()) /
555-
333,
556-
) % MESSAGE_ANIMATION.length
557-
]
558-
}`
559-
: d`Stopped (${model.conversation.stopReason}) [input: ${model.conversation.usage.inputTokens.toString()}, output: ${model.conversation.usage.outputTokens.toString()}${
560-
model.conversation.usage.cacheHits !== undefined &&
561-
model.conversation.usage.cacheMisses !== undefined
562-
? d`, cache hits: ${model.conversation.usage.cacheHits.toString()}, cache misses: ${model.conversation.usage.cacheMisses.toString()}`
563-
: ""
564-
}]`
540+
return d`${
541+
model.messages.length
542+
? model.messages.map(
543+
(m, idx) =>
544+
d`${messageModel.view({
545+
model: m,
546+
toolManager: model.toolManager,
547+
dispatch: (msg) => {
548+
dispatch({ type: "message-msg", msg, idx });
549+
},
550+
})}\n`,
551+
)
552+
: LOGO
553+
}${
554+
model.messages.length
555+
? model.conversation.state == "message-in-flight"
556+
? d`Awaiting response ${
557+
MESSAGE_ANIMATION[
558+
Math.floor(
559+
(new Date().getTime() -
560+
model.conversation.sendDate.getTime()) /
561+
333,
562+
) % MESSAGE_ANIMATION.length
563+
]
564+
}`
565+
: d`Stopped (${model.conversation.stopReason}) [input: ${model.conversation.usage.inputTokens.toString()}, output: ${model.conversation.usage.outputTokens.toString()}${
566+
model.conversation.usage.cacheHits !== undefined &&
567+
model.conversation.usage.cacheMisses !== undefined
568+
? d`, cache hits: ${model.conversation.usage.cacheHits.toString()}, cache misses: ${model.conversation.usage.cacheMisses.toString()}`
569+
: ""
570+
}]`
571+
: ""
565572
}${
566573
model.conversation.state == "stopped" &&
574+
model.messages.length &&
567575
!contextManagerModel.isContextEmpty(model.contextManager)
568576
? d`\n${contextManagerModel.view({
569577
model: model.contextManager,
@@ -641,3 +649,13 @@ ${msg.error.stack}`,
641649
getMessages,
642650
};
643651
}
652+
653+
const LOGO = d`\
654+
655+
________
656+
╱ ╲
657+
╱ ╱
658+
╱ ╱
659+
╲__╱__╱__╱
660+
661+
`;

node/providers/anthropic.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class AnthropicProvider implements Provider {
116116
};
117117
});
118118

119-
placeCacheBreakpoints(anthropicMessages);
119+
const cacheControlItemsPlaced = placeCacheBreakpoints(anthropicMessages);
120120

121121
const tools: Anthropic.Tool[] = ToolManager.TOOL_SPECS.map(
122122
(t): Anthropic.Tool => {
@@ -133,7 +133,19 @@ export class AnthropicProvider implements Provider {
133133
messages: anthropicMessages,
134134
model: this.options.model,
135135
max_tokens: 4096,
136-
system: DEFAULT_SYSTEM_PROMPT,
136+
system: [
137+
{
138+
type: "text",
139+
text: DEFAULT_SYSTEM_PROMPT,
140+
// the prompt appears in the following order:
141+
// tools
142+
// system
143+
// messages
144+
// This ensures the tools + system prompt (which is approx 1400 tokens) is cached.
145+
cache_control:
146+
cacheControlItemsPlaced < 4 ? { type: "ephemeral" } : null,
147+
},
148+
],
137149
tool_choice: {
138150
type: "auto",
139151
disable_parallel_tool_use: false,
@@ -247,7 +259,7 @@ export class AnthropicProvider implements Provider {
247259
}
248260
}
249261

250-
export function placeCacheBreakpoints(messages: MessageParam[]) {
262+
export function placeCacheBreakpoints(messages: MessageParam[]): number {
251263
// when we scan the messages, keep track of where each part ends.
252264
const blocks: { block: Anthropic.Messages.ContentBlockParam; acc: number }[] =
253265
[];
@@ -315,6 +327,8 @@ export function placeCacheBreakpoints(messages: MessageParam[]) {
315327
}
316328
}
317329
}
330+
331+
return powers.length;
318332
}
319333

320334
const STR_CHARS_PER_TOKEN = 4;

0 commit comments

Comments
 (0)