@icaruk/zai-peak-hours 0.0.8 → 0.1.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/dist/config.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  export interface PluginConfig {
2
2
  enabled: boolean;
3
- updateIntervalMinutes: number;
4
3
  }
5
4
  export declare const DEFAULT_CONFIG: PluginConfig;
6
- export declare function getPluginConfig(config: any): PluginConfig;
package/dist/config.js CHANGED
@@ -1,11 +1,3 @@
1
1
  export const DEFAULT_CONFIG = {
2
- enabled: true,
3
- updateIntervalMinutes: 15 // 15 minutes
2
+ enabled: true
4
3
  };
5
- export function getPluginConfig(config) {
6
- const userConfig = config?.peakHours || {};
7
- return {
8
- enabled: userConfig.enabled !== undefined ? userConfig.enabled : DEFAULT_CONFIG.enabled,
9
- updateIntervalMinutes: userConfig.updateIntervalMinutes !== undefined ? userConfig.updateIntervalMinutes : DEFAULT_CONFIG.updateIntervalMinutes
10
- };
11
- }
package/dist/index.js CHANGED
@@ -1,10 +1,7 @@
1
- import { tool } from '@opencode-ai/plugin';
2
1
  import { getPeakHoursStatus, formatPeakHoursMessage } from './peak-hours';
3
2
  import { DEFAULT_CONFIG } from './config';
4
3
  import * as fs from 'node:fs';
5
4
  import * as path from 'node:path';
6
- let timerId = null;
7
- let configCache = null;
8
5
  function getConfigPath() {
9
6
  const configDir = process.env.XDG_CONFIG_HOME
10
7
  ? path.join(process.env.XDG_CONFIG_HOME, 'opencode')
@@ -12,119 +9,117 @@ function getConfigPath() {
12
9
  return path.join(configDir, 'peak-hours.json');
13
10
  }
14
11
  function loadConfig() {
15
- if (configCache) {
16
- return configCache;
17
- }
18
12
  const configPath = getConfigPath();
19
13
  if (fs.existsSync(configPath)) {
20
14
  try {
21
15
  const configContent = fs.readFileSync(configPath, 'utf-8');
22
16
  const userConfig = JSON.parse(configContent);
23
- configCache = {
24
- enabled: userConfig.enabled !== undefined ? userConfig.enabled : DEFAULT_CONFIG.enabled,
25
- updateIntervalMinutes: userConfig.updateIntervalMinutes !== undefined ? userConfig.updateIntervalMinutes : DEFAULT_CONFIG.updateIntervalMinutes
17
+ return {
18
+ enabled: userConfig.enabled !== undefined ? userConfig.enabled : DEFAULT_CONFIG.enabled
26
19
  };
27
- return configCache;
28
20
  }
29
21
  catch (error) {
30
22
  console.error('Error loading peak-hours config:', error);
31
23
  }
32
24
  }
33
- configCache = DEFAULT_CONFIG;
34
- return configCache;
25
+ return DEFAULT_CONFIG;
35
26
  }
36
- async function showPeakHoursToast(client) {
37
- const config = loadConfig();
38
- if (!config.enabled) {
39
- await client.tui.showToast({
40
- body: {
41
- message: 'Peak Hours plugin is disabled in config'
42
- }
43
- });
44
- return;
45
- }
46
- const status = getPeakHoursStatus();
47
- const message = formatPeakHoursMessage(status);
48
- await client.tui.showToast({
49
- body: {
50
- message
51
- }
52
- });
27
+ function renderCommandHeading(title) {
28
+ return `${title}\n${'='.repeat(title.length)}`;
53
29
  }
54
- async function showDiagnostics(client) {
55
- const config = loadConfig();
56
- const status = getPeakHoursStatus();
30
+ function renderStatusReport(status, config) {
57
31
  const currentTime = new Date().toISOString();
58
- const diagnostics = `=== Peak Hours Plugin Diagnostics ===
59
- Plugin enabled: ${config.enabled}
60
- Update interval: ${config.updateIntervalMinutes} minutes
61
- Current time: ${currentTime}
62
- In peak hours: ${status.inPeakHours}
63
- Time until ${status.transitionType}: ${status.timeUntilTransition}
64
- ===================================`;
65
- await client.tui.showToast({
66
- body: {
67
- message: diagnostics
68
- }
69
- });
32
+ const lines = [
33
+ renderCommandHeading('Peak Hours Status'),
34
+ '',
35
+ 'Configuration:',
36
+ `- enabled: ${config.enabled}`,
37
+ '',
38
+ 'Current Status:',
39
+ `- current_time: ${currentTime}`,
40
+ `- in_peak_hours: ${status.inPeakHours}`,
41
+ `- transition: ${status.transitionType}`,
42
+ `- time_until_${status.transitionType}: ${status.timeUntilTransition}`,
43
+ '',
44
+ 'Peak Hours (UTC+8):',
45
+ `- start: 14:00`,
46
+ `- end: 18:00`
47
+ ];
48
+ return lines.join('\n');
70
49
  }
71
50
  export const PeakHours = async ({ client }) => {
72
51
  const config = loadConfig();
52
+ const typedClient = client;
53
+ async function injectRawOutput(sessionID, output) {
54
+ try {
55
+ await typedClient.session.prompt({
56
+ path: { id: sessionID },
57
+ body: {
58
+ noReply: true,
59
+ parts: [{ type: 'text', text: output, ignored: true }]
60
+ }
61
+ });
62
+ }
63
+ catch (err) {
64
+ console.error('Failed to inject output:', err);
65
+ }
66
+ }
67
+ async function handlePeakHoursCommand(sessionID) {
68
+ const status = getPeakHoursStatus();
69
+ const output = formatPeakHoursMessage(status);
70
+ await injectRawOutput(sessionID, output);
71
+ handled();
72
+ }
73
+ async function handlePeakHoursStatusCommand(sessionID) {
74
+ const status = getPeakHoursStatus();
75
+ const output = renderStatusReport(status, config);
76
+ await injectRawOutput(sessionID, output);
77
+ handled();
78
+ }
73
79
  return {
74
80
  config: async (input) => {
75
- input.command ?? (input.command = {});
76
- input.command['peak_hours'] = {
77
- template: '/peak_hours',
78
- description: 'Display current peak hours status'
79
- };
80
- input.command['peak_hours_status'] = {
81
- template: '/peak_hours_status',
82
- description: 'Display peak hours plugin diagnostics'
81
+ input.output.command = {
82
+ peak_hours: {
83
+ template: '',
84
+ description: 'Display current z.ai peak hours status and time until next transition'
85
+ },
86
+ peak_hours_status: {
87
+ template: '',
88
+ description: 'Display peak hours plugin diagnostics and configuration status'
89
+ }
83
90
  };
84
91
  },
85
- 'tool.execute.after': async (input) => {
92
+ 'tui.command.execute': async (input) => {
93
+ const command = input.command;
94
+ const sessionID = input.sessionID;
86
95
  if (!config.enabled) {
96
+ await injectRawOutput(sessionID, 'Peak Hours plugin is disabled');
87
97
  return;
88
98
  }
89
- if (input.tool === 'peak_hours') {
90
- await showPeakHoursToast(client);
99
+ if (command === 'peak_hours') {
100
+ await handlePeakHoursCommand(sessionID);
91
101
  }
92
- else if (input.tool === 'peak_hours_status') {
93
- await showDiagnostics(client);
102
+ else if (command === 'peak_hours_status') {
103
+ await handlePeakHoursStatusCommand(sessionID);
94
104
  }
95
105
  },
96
- 'session.created': async () => {
106
+ 'session.created': async (input) => {
97
107
  if (!config.enabled) {
98
108
  return;
99
109
  }
100
- await showPeakHoursToast(client);
101
- },
102
- tool: {
103
- 'peak_hours': tool({
104
- description: 'Display current z.ai peak hours status and time until next transition',
105
- args: {},
106
- async execute(_args, context) {
107
- const status = getPeakHoursStatus();
108
- const message = formatPeakHoursMessage(status);
109
- return `Peak Hours Status:\n${message}\n\nIn peak hours: ${status.inPeakHours}\nTime until ${status.transitionType}: ${status.timeUntilTransition}`;
110
- }
111
- }),
112
- 'peak_hours_status': tool({
113
- description: 'Display peak hours plugin diagnostics and configuration status',
114
- args: {},
115
- async execute(_args, context) {
116
- const config = loadConfig();
117
- const status = getPeakHoursStatus();
118
- const currentTime = new Date().toISOString();
119
- return `=== Peak Hours Plugin Diagnostics ===
120
- Plugin enabled: ${config.enabled}
121
- Update interval: ${config.updateIntervalMinutes} minutes
122
- Current time: ${currentTime}
123
- In peak hours: ${status.inPeakHours}
124
- Time until ${status.transitionType}: ${status.timeUntilTransition}
125
- ====================================`;
126
- }
127
- })
110
+ const status = getPeakHoursStatus();
111
+ const message = formatPeakHoursMessage(status);
112
+ try {
113
+ await typedClient.tui.showToast({
114
+ body: {
115
+ message,
116
+ variant: status.inPeakHours ? 'warning' : 'info'
117
+ }
118
+ });
119
+ }
120
+ catch (err) {
121
+ console.error('Failed to show toast:', err);
122
+ }
128
123
  }
129
124
  };
130
125
  };
@@ -1,32 +1,33 @@
1
- import dayjs from 'dayjs';
2
- import utc from 'dayjs/plugin/utc.js';
3
- import timezone from 'dayjs/plugin/timezone.js';
4
- dayjs.extend(utc);
5
- dayjs.extend(timezone);
6
1
  export function getPeakHoursStatus() {
7
2
  const peakHoursStart = 14;
8
3
  const peakHoursEnd = 18;
9
- const currentTime = dayjs().utc().utcOffset(8);
10
- const currentHour = currentTime.hour();
4
+ // Calcular hora actual en UTC+8 (China timezone)
5
+ const now = new Date();
6
+ const utcMs = now.getTime() + now.getTimezoneOffset() * 60000;
7
+ const chinaTime = new Date(utcMs + 8 * 3600000);
8
+ const currentHour = chinaTime.getHours();
11
9
  let inPeakHours;
12
10
  let transitionTime;
13
11
  let transitionType;
14
12
  if (currentHour >= peakHoursStart && currentHour < peakHoursEnd) {
15
13
  inPeakHours = true;
16
14
  transitionType = 'end';
17
- transitionTime = currentTime.hour(peakHoursEnd).minute(0).second(0);
15
+ transitionTime = new Date(chinaTime);
16
+ transitionTime.setHours(peakHoursEnd, 0, 0, 0);
18
17
  }
19
18
  else {
20
19
  inPeakHours = false;
21
20
  transitionType = 'start';
22
- const transitionDay = currentHour >= peakHoursEnd
23
- ? currentTime.add(1, 'day')
24
- : currentTime;
25
- transitionTime = transitionDay.hour(peakHoursStart).minute(0).second(0);
21
+ let transitionDay = new Date(chinaTime);
22
+ if (currentHour >= peakHoursEnd) {
23
+ transitionDay.setDate(transitionDay.getDate() + 1);
24
+ }
25
+ transitionDay.setHours(peakHoursStart, 0, 0, 0);
26
+ transitionTime = transitionDay;
26
27
  }
27
- const diff = transitionTime.diff(currentTime);
28
- const hours = Math.floor(diff / (1000 * 60 * 60));
29
- const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
28
+ const diffMs = transitionTime.getTime() - chinaTime.getTime();
29
+ const hours = Math.floor(diffMs / (1000 * 60 * 60));
30
+ const minutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
30
31
  let timeUntilTransition;
31
32
  if (hours > 0 && minutes > 0) {
32
33
  timeUntilTransition = `${hours} hours ${minutes} minutes`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icaruk/zai-peak-hours",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "type": "module",
5
5
  "description": "OpenCode plugin to display z.ai peak hours information with automatic timezone detection",
6
6
  "main": "dist/index.js",
@@ -26,9 +26,7 @@
26
26
  "author": "Icaruk",
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
- "@opencode-ai/plugin": "^1.4.0",
30
- "@opencode-ai/sdk": "^1.4.3",
31
- "dayjs": "^1.11.10"
29
+ "@opencode-ai/plugin": "^1.4.3"
32
30
  },
33
31
  "devDependencies": {
34
32
  "@types/node": "^20.0.0",