File size: 4,213 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import type { ExtensionUpdateInfo } from '../../config/extension.js';
import { checkExhaustive } from '@google/gemini-cli-core';

export enum ExtensionUpdateState {
  CHECKING_FOR_UPDATES = 'checking for updates',
  UPDATED_NEEDS_RESTART = 'updated, needs restart',
  UPDATED = 'updated',
  UPDATING = 'updating',
  UPDATE_AVAILABLE = 'update available',
  UP_TO_DATE = 'up to date',
  ERROR = 'error',
  NOT_UPDATABLE = 'not updatable',
  UNKNOWN = 'unknown',
}

export interface ExtensionUpdateStatus {
  status: ExtensionUpdateState;
  notified: boolean;
}

export interface ExtensionUpdatesState {
  extensionStatuses: Map<string, ExtensionUpdateStatus>;
  batchChecksInProgress: number;
  // Explicitly scheduled updates.
  scheduledUpdate: ScheduledUpdate | null;
}

export interface ScheduledUpdate {
  names: string[] | null;
  all: boolean;
  onCompleteCallbacks: OnCompleteUpdate[];
}

export interface ScheduleUpdateArgs {
  names: string[] | null;
  all: boolean;
  onComplete: OnCompleteUpdate;
}

type OnCompleteUpdate = (updateInfos: ExtensionUpdateInfo[]) => void;

export const initialExtensionUpdatesState: ExtensionUpdatesState = {
  extensionStatuses: new Map(),
  batchChecksInProgress: 0,
  scheduledUpdate: null,
};

export type ExtensionUpdateAction =
  | {
      type: 'SET_STATE';
      payload: { name: string; state: ExtensionUpdateState };
    }
  | {
      type: 'SET_NOTIFIED';
      payload: { name: string; notified: boolean };
    }
  | { type: 'BATCH_CHECK_START' }
  | { type: 'BATCH_CHECK_END' }
  | { type: 'SCHEDULE_UPDATE'; payload: ScheduleUpdateArgs }
  | { type: 'CLEAR_SCHEDULED_UPDATE' }
  | { type: 'RESTARTED'; payload: { name: string } };

export function extensionUpdatesReducer(
  state: ExtensionUpdatesState,
  action: ExtensionUpdateAction,
): ExtensionUpdatesState {
  switch (action.type) {
    case 'SET_STATE': {
      const existing = state.extensionStatuses.get(action.payload.name);
      if (existing?.status === action.payload.state) {
        return state;
      }
      const newStatuses = new Map(state.extensionStatuses);
      newStatuses.set(action.payload.name, {
        status: action.payload.state,
        notified: false,
      });
      return { ...state, extensionStatuses: newStatuses };
    }
    case 'SET_NOTIFIED': {
      const existing = state.extensionStatuses.get(action.payload.name);
      if (!existing || existing.notified === action.payload.notified) {
        return state;
      }
      const newStatuses = new Map(state.extensionStatuses);
      newStatuses.set(action.payload.name, {
        ...existing,
        notified: action.payload.notified,
      });
      return { ...state, extensionStatuses: newStatuses };
    }
    case 'BATCH_CHECK_START':
      return {
        ...state,
        batchChecksInProgress: state.batchChecksInProgress + 1,
      };
    case 'BATCH_CHECK_END':
      return {
        ...state,
        batchChecksInProgress: state.batchChecksInProgress - 1,
      };
    case 'SCHEDULE_UPDATE':
      return {
        ...state,
        // If there is a pre-existing scheduled update, we merge them.
        scheduledUpdate: {
          all: state.scheduledUpdate?.all || action.payload.all,
          names: [
            ...(state.scheduledUpdate?.names ?? []),
            ...(action.payload.names ?? []),
          ],
          onCompleteCallbacks: [
            ...(state.scheduledUpdate?.onCompleteCallbacks ?? []),
            action.payload.onComplete,
          ],
        },
      };
    case 'CLEAR_SCHEDULED_UPDATE':
      return {
        ...state,
        scheduledUpdate: null,
      };
    case 'RESTARTED': {
      const existing = state.extensionStatuses.get(action.payload.name);
      if (existing?.status !== ExtensionUpdateState.UPDATED_NEEDS_RESTART) {
        return state;
      }

      const newStatuses = new Map(state.extensionStatuses);
      newStatuses.set(action.payload.name, {
        ...existing,
        status: ExtensionUpdateState.UPDATED,
      });

      return { ...state, extensionStatuses: newStatuses };
    }
    default:
      checkExhaustive(action);
  }
}