Skip to content

Commit cc72785

Browse files
committed
Add configurable update check frequency with caching and forced checks
- in settings > General > Updates (new group below 'Open...'), allow setting 'check frequency' (package.json key: 'update check frequency'). Available options: hourly, daily, weekly, monthly. (default to hourly) - continue 5-minute interval, but compare when checked last time (store in state key) and what's the value of 'check frequency'. If required time passed, e.g. 24h or a week, run the update check. - too much update-related logic in provider, use `apps/editor/src/views/prompt/backend/utils` dir - no need to store `apps/editor/src/utils/check-for-updates.ts` in separate file - is the logic solid? [Changes] └── apps └── editor ├── package.json └── src ├── commands │ └── check-for-updates-command.ts ├── constants │ ├── state-keys.ts │ └── values.ts ├── extension.ts ├── utils │ └── check-for-updates.ts └── views ├── prompt │ └── backend │ ├── prompt-view-provider.ts │ └── utils │ ├── check-for-updates.ts │ ├── estimate-token-count.ts │ ├── get-display-path-for-editor.ts │ ├── get-valid-web-configurations.ts │ ├── index.ts │ ├── insert-text-at-caret.ts │ ├── 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 └── settings ├── backend │ ├── message-handlers │ │ ├── handle-get-update-check-frequency.ts │ │ ├── handle-update-update-check-frequency.ts │ │ └── index.ts │ └── settings-view-provider.ts ├── frontend │ ├── Home │ │ ├── Home.tsx │ │ └── sections │ │ └── GeneralSection.tsx │ ├── Settings.tsx │ ├── hooks │ │ └── use-settings.ts │ └── i18n │ └── translations │ └── general.ts └── types └── messages.ts
1 parent b059c56 commit cc72785

18 files changed

Lines changed: 438 additions & 90 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,17 @@
10191019
"scope": "resource",
10201020
"default": false
10211021
},
1022+
"codeWebChat.updateCheckFrequency": {
1023+
"type": "string",
1024+
"scope": "application",
1025+
"enum": [
1026+
"hourly",
1027+
"daily",
1028+
"weekly",
1029+
"monthly"
1030+
],
1031+
"default": "hourly"
1032+
},
10221033
"codeWebChat.ignorePatterns": {
10231034
"type": "array",
10241035
"scope": "resource",

‎apps/editor/src/commands/check-for-updates-command.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const check_for_updates_command = (
2121
},
2222
async () => {
2323
const start_time = Date.now()
24-
await prompt_view_provider.check_for_updates()
24+
await prompt_view_provider.check_for_updates({ force: true })
2525
const elapsed_time = Date.now() - start_time
2626

2727
if (elapsed_time < MINIMUM_WAIT_TIME) {

‎apps/editor/src/constants/state-keys.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export const TEMPORARY_CHECKPOINT_STATE_KEY = 'temporary-checkpoint'
2424
export const CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY =
2525
'checkpoint-operation-in-progress'
2626
export const COMPLETED_TASKS_STATE_KEY = 'completed-tasks'
27+
export const LAST_UPDATE_CHECK_TIMESTAMP_STATE_KEY =
28+
'last-update-check-timestamp'
29+
export const CACHED_UPDATE_INFO_STATE_KEY = 'cached-update-info'
2730

2831
export const CHATS_VIEW_CHAT_HISTORY_STATE_KEY = 'chats-view-chat-history'
2932

‎apps/editor/src/constants/values.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const MAX_FILE_TOKENS_FOR_COMMIT_MESSAGE = 20000
33
export const MAX_PROMPT_CHARS_IN_COMMIT_MESSAGE = 1000
44
export const GIT_LOG_SINCE_DURATION = '6 months ago'
55
export const SYMBOL_CACHE_DURATION = 5 * 60 * 1000
6-
export const UPDATE_CHECK_INTERVAL = 1 * 60 * 60 * 1000 // 1 hour
6+
export const UPDATE_CHECK_INTERVAL = 5 * 60 * 1000 // 5 minutes
77

88
export const DIFF_PLACEHOLDERS = {
99
FILE_CREATED: 'File created',

‎apps/editor/src/utils/check-for-updates.ts‎

Lines changed: 0 additions & 73 deletions
This file was deleted.

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
} from '@shared/types/prompt-types'
3030
import { Logger } from '@shared/utils/logger'
3131
import { get_error_message } from '@/utils/get-error-message'
32-
import { check_for_updates } from '@/utils/check-for-updates'
3332
import { ResponseHistoryItem } from '@shared/types/response-history-item'
3433
import { ProvidersManager } from '@/services/providers-manager'
3534
import { SharedContextState } from '@/context/shared-context-state'
@@ -45,7 +44,8 @@ import {
4544
get_valid_web_configurations,
4645
get_display_path_for_editor,
4746
insert_text_at_caret,
48-
estimate_token_count
47+
estimate_token_count,
48+
check_for_updates
4949
} from './utils'
5050

5151
export class PromptViewProvider implements vscode.WebviewViewProvider {
@@ -448,16 +448,8 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
448448
)
449449
}
450450

451-
async check_for_updates() {
452-
const current_version = this.extension_context.extension.packageJSON.version
453-
if (!current_version) return
454-
455-
this.update_info = await check_for_updates(current_version)
456-
457-
this.send_message({
458-
command: 'UPDATE_INFO',
459-
update_info: this.update_info
460-
})
451+
async check_for_updates(params?: { force?: boolean }) {
452+
await check_for_updates(this, params)
461453
}
462454

463455
cancel_all_patch_repairs() {
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import * as vscode from 'vscode'
2+
import axios from 'axios'
3+
import * as state_keys from '@/constants/state-keys'
4+
import { UpdateInfo } from '@/views/prompt/types/messages'
5+
import { PromptViewProvider } from '../prompt-view-provider'
6+
7+
const is_newer = (latest: string, current: string) => {
8+
const l = latest.split('.').map(Number)
9+
const c = current.split('.').map(Number)
10+
for (let i = 0; i < Math.max(l.length, c.length); i++) {
11+
const l_val = l[i] || 0
12+
const c_val = c[i] || 0
13+
if (l_val > c_val) return true
14+
if (l_val < c_val) return false
15+
}
16+
return false
17+
}
18+
19+
export const check_for_updates = async (
20+
provider: PromptViewProvider,
21+
params?: { force?: boolean }
22+
) => {
23+
const force = params?.force ?? false
24+
25+
const current_version =
26+
provider.extension_context.extension.packageJSON.version
27+
if (!current_version) return
28+
29+
if (!force) {
30+
const last_check = provider.extension_context.globalState.get<number>(
31+
state_keys.LAST_UPDATE_CHECK_TIMESTAMP_STATE_KEY,
32+
0
33+
)
34+
const now = Date.now()
35+
const config = vscode.workspace.getConfiguration('codeWebChat')
36+
const frequency = config.get<'hourly' | 'daily' | 'weekly' | 'monthly'>(
37+
'updateCheckFrequency',
38+
'hourly'
39+
)
40+
41+
let required_elapsed_time = 60 * 60 * 1000
42+
if (frequency === 'daily') {
43+
required_elapsed_time = 24 * 60 * 60 * 1000
44+
} else if (frequency === 'weekly') {
45+
required_elapsed_time = 7 * 24 * 60 * 60 * 1000
46+
} else if (frequency === 'monthly') {
47+
required_elapsed_time = 30 * 24 * 60 * 60 * 1000
48+
}
49+
50+
if (now - last_check < required_elapsed_time) {
51+
const cached = provider.extension_context.globalState.get<UpdateInfo>(
52+
state_keys.CACHED_UPDATE_INFO_STATE_KEY
53+
)
54+
if (cached) {
55+
if (cached.is_available && cached.latest_version) {
56+
if (!is_newer(cached.latest_version, current_version)) {
57+
cached.is_available = false
58+
provider.extension_context.globalState.update(
59+
state_keys.CACHED_UPDATE_INFO_STATE_KEY,
60+
cached
61+
)
62+
}
63+
}
64+
provider.update_info = cached
65+
}
66+
67+
provider.send_message({
68+
command: 'UPDATE_INFO',
69+
update_info: provider.update_info
70+
})
71+
72+
return
73+
}
74+
}
75+
76+
provider.update_info = await fetch_update_info(current_version)
77+
78+
provider.extension_context.globalState.update(
79+
state_keys.LAST_UPDATE_CHECK_TIMESTAMP_STATE_KEY,
80+
Date.now()
81+
)
82+
provider.extension_context.globalState.update(
83+
state_keys.CACHED_UPDATE_INFO_STATE_KEY,
84+
provider.update_info
85+
)
86+
87+
provider.send_message({
88+
command: 'UPDATE_INFO',
89+
update_info: provider.update_info
90+
})
91+
}
92+
93+
const fetch_update_info = async (
94+
current_version: string
95+
): Promise<UpdateInfo> => {
96+
if (!current_version) return { is_available: false }
97+
98+
try {
99+
let page = 1
100+
while (page <= 10) {
101+
const res = await axios.get(
102+
`https://api.github.com/repos/CodeWebChat/CWC/tags?page=${page}`,
103+
{ validateStatus: () => true }
104+
)
105+
if (res.status !== 200) break
106+
const tags = res.data
107+
if (!Array.isArray(tags) || tags.length === 0) break
108+
109+
const vscode_tag = tags.find((t: any) => t.name?.endsWith('-vscode'))
110+
if (vscode_tag) {
111+
const latest_version = vscode_tag.name.replace('-vscode', '')
112+
if (is_newer(latest_version, current_version)) {
113+
let age_days = 0
114+
try {
115+
const commit_res = await axios.get(
116+
`https://api.github.com/repos/CodeWebChat/CWC/commits/${current_version}-vscode`,
117+
{ validateStatus: () => true }
118+
)
119+
if (commit_res.status === 200) {
120+
const commit_data = commit_res.data
121+
const date = commit_data.commit?.committer?.date
122+
if (date) {
123+
age_days = Math.floor(
124+
(Date.now() - new Date(date).getTime()) /
125+
(1000 * 60 * 60 * 24)
126+
)
127+
}
128+
}
129+
} catch (e) {
130+
console.error('Failed to fetch commit date:', e)
131+
}
132+
133+
return {
134+
is_available: true,
135+
latest_version,
136+
latest_version_tag: vscode_tag.name,
137+
current_version_age_days: age_days
138+
}
139+
} else {
140+
return { is_available: false }
141+
}
142+
}
143+
page++
144+
}
145+
} catch (e) {
146+
console.error('Failed to check for updates:', e)
147+
}
148+
149+
return { is_available: false }
150+
}

‎apps/editor/src/views/prompt/backend/utils/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './insert-text-at-caret'
55
export * from './persistent-text-storage'
66
export * from './preview-text-in-temp-file'
77
export * from './website-fetcher'
8+
export * from './check-for-updates'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as vscode from 'vscode'
2+
import { SettingsViewProvider } from '../settings-view-provider'
3+
4+
export const handle_get_update_check_frequency = async (
5+
provider: SettingsViewProvider
6+
) => {
7+
const config = vscode.workspace.getConfiguration('codeWebChat')
8+
const value = config.get<'hourly' | 'daily' | 'weekly' | 'monthly'>(
9+
'updateCheckFrequency',
10+
'hourly'
11+
)
12+
provider.postMessage({
13+
command: 'UPDATE_CHECK_FREQUENCY',
14+
value
15+
})
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as vscode from 'vscode'
2+
import { UpdateUpdateCheckFrequencyMessage } from '../../types/messages'
3+
4+
export const handle_update_update_check_frequency = async (
5+
message: UpdateUpdateCheckFrequencyMessage
6+
) => {
7+
const config = vscode.workspace.getConfiguration('codeWebChat')
8+
await config.update(
9+
'updateCheckFrequency',
10+
message.value,
11+
vscode.ConfigurationTarget.Global
12+
)
13+
}

0 commit comments

Comments
 (0)