feat(server): add time context for chat (#11933)

fix AI-84
This commit is contained in:
darkskygit
2025-04-23 14:39:45 +00:00
parent ea391779dd
commit b1500cb4df
2 changed files with 15 additions and 3 deletions
@@ -117,6 +117,13 @@ export class ChatPrompt {
}
}
private preDefinedParams(params: PromptParams) {
return {
'affine::date': new Date().toLocaleDateString(),
'affine::language': params.language || 'English',
};
}
/**
* render prompt messages with params
* @param params record of params, e.g. { name: 'Alice' }
@@ -125,7 +132,9 @@ export class ChatPrompt {
finish(params: PromptParams, sessionId?: string): PromptMessage[] {
this.checkParams(params, sessionId);
const { attachments: attach, ...restParams } = params;
const { attachments: attach, ...restParams } = Object.fromEntries(
Object.entries(params).filter(([k]) => !k.startsWith('affine::'))
);
const paramsAttach = Array.isArray(attach) ? attach : [];
return this.messages.map(
@@ -133,7 +142,10 @@ export class ChatPrompt {
const result: PromptMessage = {
...rest,
params,
content: Mustache.render(content, restParams),
content: Mustache.render(
content,
Object.assign({}, restParams, this.preDefinedParams(restParams))
),
};
const attachments = [