fix: updated time

This commit is contained in:
Han Xiao 2025-03-13 10:30:35 +08:00
parent 1b2833885b
commit 59b2daf66b
3 changed files with 83 additions and 9 deletions

View File

@ -41,6 +41,7 @@ import {
removeHTMLtags
} from "./utils/text-tools";
import {MAX_QUERIES_PER_STEP, MAX_REFLECT_PER_STEP, MAX_URLS_PER_STEP, Schemas} from "./utils/schemas";
import {formatDateBasedOnType, formatDateRange} from "./utils/date-tools";
async function sleep(ms: number) {
const seconds = Math.ceil(ms / 1000);
@ -84,7 +85,7 @@ ${k.question}
<answer>
${k.answer}
</answer>
${k.updated && k.type === 'url' ? `
${k.updated && (k.type === 'url' || k.type === 'side-info') ? `
<answer-datetime>
${k.updated}
</answer-datetime>
@ -252,6 +253,15 @@ function updateContext(step: any) {
allContext.push(step)
}
function replaceLastUserMsg(messages: Array<CoreMessage>, content: string) {
return messages.map((m, i) => {
if (m.role === 'user' && i === messages.length - 1) {
return {...m, content}
}
return m
});
}
export async function getResponse(question?: string,
tokenBudget: number = 1_000_000,
@ -375,7 +385,7 @@ export async function getResponse(question?: string,
model: 'agent',
schema,
system,
messages,
messages: replaceLastUserMsg(messages, currentQuestion),
});
thisStep = {
action: result.object.action,
@ -475,7 +485,8 @@ Your journey ends here. You have successfully answered the original question. Co
`);
thisStep.isFinal = true;
break
} else {
}
else {
if (evaluation.type === 'strict') {
finalAnswerPIP = evaluation.improvement_plan || '';
// remove 'strict' from the evaluation metrics
@ -484,7 +495,8 @@ Your journey ends here. You have successfully answered the original question. Co
if (badAttempts >= maxBadAttempts) {
thisStep.isFinal = false;
break
} else {
}
else {
diaryContext.push(`
At step ${step}, you took **answer** action but evaluator thinks it is not a good answer:
@ -540,7 +552,7 @@ Although you solved a sub-question, you still need to find the answer to the ori
answer: thisStep.answer,
references: thisStep.references,
type: 'qa',
updated: new Date().toISOString()
updated: formatDateBasedOnType(new Date(), 'full')
});
}
} else if (thisStep.action === 'reflect' && thisStep.questionsToAnswer) {
@ -650,7 +662,7 @@ But then you realized you have asked them before. You decided to to think out of
question: `What do Internet say about "${oldQuery}"?`,
answer: removeHTMLtags(minResults.map(r => r.description).join('; ')),
type: 'side-info',
updated: new Date().toISOString()
updated: query.tbs? formatDateRange(query): undefined
});
}
@ -745,7 +757,7 @@ You decided to think out of the box or cut from a completely different angle.`);
answer: result.solution.output,
sourceCode: result.solution.code,
type: 'coding',
updated: new Date().toISOString()
updated: formatDateBasedOnType(new Date(), 'full')
});
diaryContext.push(`
At step ${step}, you took the **coding** action and try to solve the coding issue: ${thisStep.codingIssue}.

61
src/utils/date-tools.ts Normal file
View File

@ -0,0 +1,61 @@
import {SERPQuery} from "../types";
export function formatDateRange(query: SERPQuery) {
let searchDateTime;
const currentDate = new Date();
let format = 'full'; // Default format
switch (query.tbs) {
case 'qdr:h':
searchDateTime = new Date(Date.now() - 60 * 60 * 1000);
format = 'hour';
break;
case 'qdr:d':
searchDateTime = new Date(Date.now() - 24 * 60 * 60 * 1000);
format = 'day';
break;
case 'qdr:w':
searchDateTime = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
format = 'day';
break;
case 'qdr:m':
searchDateTime = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
format = 'day';
break;
case 'qdr:y':
searchDateTime = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000);
format = 'year';
break;
default:
searchDateTime = undefined;
}
if (searchDateTime !== undefined) {
const startDate = formatDateBasedOnType(searchDateTime, format);
const endDate = formatDateBasedOnType(currentDate, format);
return `Between ${startDate} and ${endDate}`;
}
return '';
}
export function formatDateBasedOnType(date: Date, formatType: string) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
switch (formatType) {
case 'year':
return `${year}-${month}-${day}`;
case 'day':
return `${year}-${month}-${day}`;
case 'hour':
return `${year}-${month}-${day} ${hours}:${minutes}`;
case 'full':
default:
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
}

View File

@ -4,6 +4,7 @@ import {rerankDocuments} from "../tools/jina-rerank";
import {readUrl} from "../tools/read";
import {Schemas} from "./schemas";
import {cherryPick} from "../tools/jina-latechunk";
import {formatDateBasedOnType} from "./date-tools";
export function normalizeUrl(urlString: string, debug = false, options = {
removeAnchors: true,
@ -427,11 +428,11 @@ export async function processURLs(
// Add to knowledge base
allKnowledge.push({
question: `What do expert say about "${data.title}"?`,
question: `What do expert say about "${question}"?`,
answer: await cherryPick(question, data.content, {}, context, schemaGen, url),
references: [data.url],
type: 'url',
updated: guessedTime
updated: guessedTime? formatDateBasedOnType(new Date(guessedTime), 'full'): undefined
});
// Process page links