@playpilot/tpi 8.33.2 → 8.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playpilot/tpi",
3
- "version": "8.33.2",
3
+ "version": "8.34.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite dev",
@@ -116,21 +116,34 @@ export async function pollLinkInjections(
116
116
  /**
117
117
  * Re-run fetchLinkInjections but with run_ai enabled. This makes the call to update AI results.
118
118
  * This happens when the content_modified_time has changed since the last response, likely meaning the article was edited.
119
- * Returns the response as is if ai is not enabled or content_modified_time from response matches current time.
119
+ * Returns the response as if ai is not enabled or content_modified_time from response matches current time.
120
+ *
121
+ * Will also re-run the AI if the previous attempt failed on recent articles with a cut off for how often the AI will re-run in a given time period.
120
122
  */
121
123
  export async function runAiBasedOnResponse(pageText: string, response: LinkInjectionResponse): Promise<LinkInjectionResponse> {
122
124
  if (!pageText) return response
123
125
  if (!response.ai_enabled) return response
124
126
  if (response.ai_running) return response
125
127
 
128
+ const rerun = async (): Promise<LinkInjectionResponse> => await fetchLinkInjections({ params: { run_ai: true }, method: 'POST' })
129
+
126
130
  const currentModifiedTime = getPageMetaData().content_modified_time
127
131
  const responseModifiedTime = response.content_modified_time ? getDatetime(response.content_modified_time) : null
128
132
  const isCurrentTimeEqualToResponseTime = currentModifiedTime && responseModifiedTime === currentModifiedTime
129
133
 
134
+ if (currentModifiedTime && response.ai_last_run && response.ai_progress_message === 'Failed') {
135
+ const tenMinutesAgo = new Date(Date.now() - 600_000)
136
+ const isArticleModifiedInLastDay = new Date().getTime() - new Date(currentModifiedTime).getTime() < 86_400_000
137
+
138
+ if (isArticleModifiedInLastDay && new Date(response.ai_last_run) < tenMinutesAgo) {
139
+ return await rerun()
140
+ }
141
+ }
142
+
130
143
  if (response.ai_last_run && isCurrentTimeEqualToResponseTime) return response
131
144
  if (new Date(currentModifiedTime || 0) <= new Date(responseModifiedTime || 0)) return response
132
145
 
133
- return await fetchLinkInjections({ params: { run_ai: true }, method: 'POST' })
146
+ return await rerun()
134
147
  }
135
148
 
136
149
  export async function saveLinkInjections(linkInjections: LinkInjection[]): Promise<LinkInjection[]> {
@@ -379,6 +379,38 @@ describe('$lib/api/externalPages', () => {
379
379
  await runAiBasedOnResponse('Some text', response)
380
380
  expect(api).toHaveBeenCalled()
381
381
  })
382
+
383
+ it('Should call api if ai_enabled is true, response content_modified_time time is equal to given time but response failed', async () => {
384
+ const now = new Date()
385
+ document.body.innerHTML = `<meta content="${now}" property="article:modified_time">`
386
+ const response = { ai_enabled: true, content_modified_time: now, ai_progress_message: 'Failed', ai_last_run: '2020-03-20T00:00:00Z' }
387
+
388
+ // @ts-ignore
389
+ await runAiBasedOnResponse('Some text', response)
390
+ expect(api).toHaveBeenCalled()
391
+ })
392
+
393
+ it('Should not call api if ai_enabled is true, response content_modified_time time is equal to given time, response failed, but ai was run in the last 10 minutes', async () => {
394
+ const now = new Date()
395
+ const fiveMinutesAgo = new Date(now.getTime() - 300_000)
396
+ document.body.innerHTML = `<meta content="${now}" property="article:modified_time">`
397
+ const response = { ai_enabled: true, content_modified_time: now, ai_progress_message: 'Failed', ai_last_run: fiveMinutesAgo }
398
+
399
+ // @ts-ignore
400
+ await runAiBasedOnResponse('Some text', response)
401
+ expect(api).not.toHaveBeenCalled()
402
+ })
403
+
404
+ it('Should not call api if ai_enabled is true, response content_modified_time time is equal to given time, response failed, but article modified time is past 24 hours ago', async () => {
405
+ const now = new Date()
406
+ const moreThanADayAgo = new Date(now.getTime() - 90_000_000)
407
+ document.body.innerHTML = `<meta content="${moreThanADayAgo}" property="article:modified_time">`
408
+ const response = { ai_enabled: true, content_modified_time: moreThanADayAgo, ai_progress_message: 'Failed', ai_last_run: '2020-03-20T00:00:00Z' }
409
+
410
+ // @ts-ignore
411
+ await runAiBasedOnResponse('Some text', response)
412
+ expect(api).not.toHaveBeenCalled()
413
+ })
382
414
  })
383
415
 
384
416
  describe('saveLinkInjections', () => {