Skip to content

Commit 3cea2f1

Browse files
committed
feat: make systemPrompt sync and optional
1 parent d5c324f commit 3cea2f1

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

docs/src/docs/core/context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type RequestContext = {
2424
resolvedEntities: EntityInfo;
2525

2626
/** Function for generating a system prompt */
27-
systemPrompt: () => Promise<string> | string;
27+
systemPrompt: () => string | null;
2828

2929
/**
3030
* Received partial response update with response streaming.

examples/bare/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const chatModel = new VercelChatModelAdapter({
1717
languageModel: openAiModel,
1818
});
1919

20-
const SYSTEM_PROMPT = 'Your name is Byorg. You are an AI Assistant.';
20+
const SYSTEM_PROMPT = 'Your name is Byorg. You are a helpful AI Assistant.';
2121

2222
const app = createApp({
2323
chatModel,

examples/slack/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const chatModel = new VercelChatModelAdapter({
1818
languageModel: openAiProvider.languageModel(LANGUAGE_MODEL),
1919
});
2020

21-
const SYSTEM_PROMPT = 'Your name is Byorg. You are an AI Assistant.';
21+
const SYSTEM_PROMPT = 'Your name is Byorg. You are a helpful AI Assistant.';
2222

2323
const app = createApp({
2424
chatModel,

packages/core/src/ai/vercel.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,18 @@ export class VercelChatModelAdapter implements ChatModel {
5757
constructor(private readonly _options: VercelChatModelAdapterOptions) {}
5858

5959
async generateResponse(context: RequestContext): Promise<AssistantResponse> {
60-
const messages = context.messages;
61-
62-
const systemPrompt = await context.systemPrompt();
60+
let systemPrompt = context.systemPrompt();
6361
const entitiesPrompt = formatResolvedEntities(context.resolvedEntities);
64-
const finalSystemPrompt = [systemPrompt, entitiesPrompt].join('\n\n');
62+
if (systemPrompt && entitiesPrompt) {
63+
systemPrompt += '\n\n' + entitiesPrompt;
64+
}
65+
66+
const messages: CoreMessage[] = [];
67+
if (systemPrompt) {
68+
messages.push({ role: 'system', content: systemPrompt });
69+
}
6570

66-
// TODO: Use userId in anonymous case
67-
const resolvedMessages: CoreMessage[] = [
68-
{ role: 'system' as const, content: finalSystemPrompt },
69-
...messages.map(toMessageParam),
70-
];
71+
messages.push(...context.messages.map(toMessageParam));
7172

7273
const getRunToolFunction =
7374
<TParams extends z.ZodSchema, TOutput>(
@@ -94,7 +95,7 @@ export class VercelChatModelAdapter implements ChatModel {
9495

9596
const executionContext: AiExecutionContext = {
9697
tools,
97-
messages: resolvedMessages,
98+
messages: messages,
9899
};
99100

100101
const executionResult = context.onPartialResponse
@@ -229,14 +230,14 @@ function toNumberOrZero(n: number): number {
229230
return Number.isNaN(n) ? 0 : n;
230231
}
231232

232-
function formatResolvedEntities(entities: Record<string, unknown>): string {
233+
function formatResolvedEntities(entities: Record<string, unknown>): string | null {
233234
if (Object.keys(entities).length === 0) {
234-
return '';
235+
return null;
235236
}
236237

237-
return `ENTITY DICTIONARY: \n
238-
${Object.entries(entities)
239-
.map(([key, value]) => `'${key}' is '${JSON.stringify(value)}'`)
240-
.join('\n')}
241-
`;
238+
return `### ENTITY DICTIONARY ###\n
239+
${Object.entries(entities)
240+
.map(([key, value]) => `'${key}' is '${JSON.stringify(value)}'`)
241+
.join('\n')}
242+
`;
242243
}

packages/core/src/application.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type ErrorHandler = (
3333

3434
export type ApplicationConfig = {
3535
chatModel: ChatModel;
36-
systemPrompt: (context: RequestContext) => Promise<string> | string;
36+
systemPrompt?: ((context: RequestContext) => string | null) | string;
3737
plugins?: ApplicationPlugin[];
3838
errorHandler?: ErrorHandler;
3939
};
@@ -89,7 +89,6 @@ export function createApp(config: ApplicationConfig): Application {
8989
const performance = new PerformanceTimeline();
9090
performance.markStart(PerformanceMarks.processMessages);
9191

92-
const onPartialResponse = options?.onPartialResponse;
9392
const context: RequestContext = {
9493
messages,
9594
get lastMessage() {
@@ -101,14 +100,16 @@ export function createApp(config: ApplicationConfig): Application {
101100

102101
return lastMessage;
103102
},
104-
103+
systemPrompt: () =>
104+
typeof config.systemPrompt === 'function'
105+
? config.systemPrompt(context)
106+
: (config.systemPrompt ?? null),
107+
onPartialResponse: options?.onPartialResponse,
105108
tools,
106109
references: getReferenceStorage(),
107110
resolvedEntities: {},
108-
onPartialResponse,
109111
extras: options?.extras ?? {},
110112
performance,
111-
systemPrompt: () => config.systemPrompt(context),
112113
};
113114

114115
const handler = async () => {

packages/core/src/domain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type RequestContext = {
3030
tools: ApplicationTool[];
3131
references: ReferenceStorage;
3232
resolvedEntities: EntityInfo;
33-
systemPrompt: () => Promise<string> | string;
33+
systemPrompt: () => string | null;
3434
onPartialResponse?: (text: string) => void;
3535
extras: MessageRequestExtras;
3636
performance: PerformanceTimeline;

0 commit comments

Comments
 (0)