Skip to content

Commit 57be0b7

Browse files
committed
Extract update check logic into check-for-updates utility and use it in PromptViewProvider
- place the update checking logic in utils └── apps └── editor └── src ├── commands │ └── check-for-updates-command.ts ├── utils │ └── check-for-updates.ts └── views └── prompt └── backend ├── prompt-view-provider.ts └── utils ├── persistent-text-storage.ts ├── preview-text-in-temp-file.ts ├── symbols │ ├── fragment │ │ └── replace-fragment-symbol.ts │ ├── git │ │ └── replace-git-symbols.ts │ ├── image │ │ └── replace-image-symbol.ts │ ├── pasted-text │ │ └── replace-pasted-text-symbol.ts │ ├── replace-symbols.ts │ ├── saved-context │ │ └── replace-saved-context-symbol.ts │ ├── skill │ │ └── replace-skill-symbol.ts │ ├── snippet │ │ └── replace-snippet-symbol.ts │ ├── symbol-cache.ts │ └── website │ └── replace-website-symbol.ts └── website-fetcher.ts
1 parent 1390dbf commit 57be0b7

2 files changed

Lines changed: 79 additions & 71 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import axios from 'axios'
2+
import { UpdateInfo } from '@/views/prompt/types/messages'
3+
4+
export const check_for_updates = async (
5+
current_version: string
6+
): Promise<UpdateInfo> => {
7+
if (!current_version) return { is_available: false }
8+
9+
const is_newer = (latest: string, current: string) => {
10+
const l = latest.split('.').map(Number)
11+
const c = current.split('.').map(Number)
12+
for (let i = 0; i < Math.max(l.length, c.length); i++) {
13+
const l_val = l[i] || 0
14+
const c_val = c[i] || 0
15+
if (l_val > c_val) return true
16+
if (l_val < c_val) return false
17+
}
18+
return false
19+
}
20+
21+
try {
22+
let page = 1
23+
while (page <= 10) {
24+
const res = await axios.get(
25+
`https://api.github.com/repos/CodeWebChat/CWC/tags?page=${page}`,
26+
{ validateStatus: () => true }
27+
)
28+
if (res.status !== 200) break
29+
const tags = res.data
30+
if (!Array.isArray(tags) || tags.length === 0) break
31+
32+
const vscode_tag = tags.find((t: any) => t.name?.endsWith('-vscode'))
33+
if (vscode_tag) {
34+
const latest_version = vscode_tag.name.replace('-vscode', '')
35+
if (is_newer(latest_version, current_version)) {
36+
let age_days = 0
37+
try {
38+
const commit_res = await axios.get(
39+
`https://api.github.com/repos/CodeWebChat/CWC/commits/${current_version}-vscode`,
40+
{ validateStatus: () => true }
41+
)
42+
if (commit_res.status === 200) {
43+
const commit_data = commit_res.data
44+
const date = commit_data.commit?.committer?.date
45+
if (date) {
46+
age_days = Math.floor(
47+
(Date.now() - new Date(date).getTime()) /
48+
(1000 * 60 * 60 * 24)
49+
)
50+
}
51+
}
52+
} catch (e) {
53+
console.error('Failed to fetch commit date:', e)
54+
}
55+
56+
return {
57+
is_available: true,
58+
latest_version,
59+
current_version_age_days: age_days
60+
}
61+
} else {
62+
return { is_available: false }
63+
}
64+
}
65+
page++
66+
}
67+
} catch (e) {
68+
console.error('Failed to check for updates:', e)
69+
}
70+
71+
return { is_available: false }
72+
}

‎apps/editor/src/views/prompt/backend/prompt-view-provider.ts‎

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ChildProcessWithoutNullStreams } from 'child_process'
22
import * as path from 'path'
3-
import axios from 'axios'
43
import * as vscode from 'vscode'
54
import { WebSocketManager } from '@/services/websocket-manager'
65
import {
@@ -137,6 +136,7 @@ import {
137136
} from '@shared/types/prompt-types'
138137
import { Logger } from '@shared/utils/logger'
139138
import { get_error_message } from '@/utils/get-error-message'
139+
import { check_for_updates } from '@/utils/check-for-updates'
140140
import { ResponseHistoryItem } from '@shared/types/response-history-item'
141141
import { ProvidersManager } from '@/services/providers-manager'
142142
import { SharedContextState } from '@/context/shared-context-state'
@@ -571,76 +571,12 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
571571
const current_version = this.extension_context.extension.packageJSON.version
572572
if (!current_version) return
573573

574-
const is_newer = (latest: string, current: string) => {
575-
const l = latest.split('.').map(Number)
576-
const c = current.split('.').map(Number)
577-
for (let i = 0; i < Math.max(l.length, c.length); i++) {
578-
const l_val = l[i] || 0
579-
const c_val = c[i] || 0
580-
if (l_val > c_val) return true
581-
if (l_val < c_val) return false
582-
}
583-
return false
584-
}
585-
586-
try {
587-
let page = 1
588-
while (page <= 10) {
589-
const res = await axios.get(
590-
`https://api.github.com/repos/CodeWebChat/CWC/tags?page=${page}`,
591-
{ validateStatus: () => true }
592-
)
593-
if (res.status !== 200) break
594-
const tags = res.data
595-
if (!Array.isArray(tags) || tags.length === 0) break
596-
597-
const vscode_tag = tags.find((t: any) => t.name?.endsWith('-vscode'))
598-
if (vscode_tag) {
599-
const latest_version = vscode_tag.name.replace('-vscode', '')
600-
if (is_newer(latest_version, current_version)) {
601-
let age_days = 0
602-
try {
603-
const commit_res = await axios.get(
604-
`https://api.github.com/repos/CodeWebChat/CWC/commits/${current_version}-vscode`,
605-
{ validateStatus: () => true }
606-
)
607-
if (commit_res.status === 200) {
608-
const commit_data = commit_res.data
609-
const date = commit_data.commit?.committer?.date
610-
if (date) {
611-
age_days = Math.floor(
612-
(Date.now() - new Date(date).getTime()) /
613-
(1000 * 60 * 60 * 24)
614-
)
615-
}
616-
}
617-
} catch (e) {
618-
console.error('Failed to fetch commit date:', e)
619-
}
620-
621-
this.update_info = {
622-
is_available: true,
623-
latest_version,
624-
current_version_age_days: age_days
625-
}
626-
this.send_message({
627-
command: 'UPDATE_INFO',
628-
update_info: this.update_info
629-
})
630-
} else {
631-
this.update_info = { is_available: false }
632-
this.send_message({
633-
command: 'UPDATE_INFO',
634-
update_info: this.update_info
635-
})
636-
}
637-
break
638-
}
639-
page++
640-
}
641-
} catch (e) {
642-
console.error('Failed to check for updates:', e)
643-
}
574+
this.update_info = await check_for_updates(current_version)
575+
576+
this.send_message({
577+
command: 'UPDATE_INFO',
578+
update_info: this.update_info
579+
})
644580
}
645581

646582
cancel_all_patch_repairs() {

0 commit comments

Comments
 (0)