dsh-subscription-agents 1.2.0 → 1.2.1

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.
Files changed (3) hide show
  1. package/agent-runner.js +44 -20
  2. package/cli.js +14 -6
  3. package/package.json +1 -1
package/agent-runner.js CHANGED
@@ -1,9 +1,12 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { StringDecoder } from 'node:string_decoder';
3
+ import { claudeLimitWindows } from './cli.js';
3
4
 
4
5
  const MAX_PENDING = 2 * 1024 * 1024;
5
6
  const MAX_LINE = 1024 * 1024;
6
7
  const MAX_TEXT = 128 * 1024;
8
+ // An oversized line is drained without buffering; only its head is kept to find which call it closes.
9
+ const OVERSIZED_HEAD = 64 * 1024;
7
10
  const TIMEOUT = 30 * 60_000;
8
11
  const ID = /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,159}$/;
9
12
 
@@ -138,14 +141,8 @@ export async function runAgent({ provider, exe, env, cwd, permissionMode, thread
138
141
  if (event.parent_tool_use_id != null) return;
139
142
  if (typeof event.session_id === 'string') await setThread(event.session_id);
140
143
  if (event.type === 'rate_limit_event') {
141
- const info = event.rate_limit_info;
142
- const label = ({ five_hour: '5 часов', seven_day: '7 дней', seven_day_sonnet: 'Sonnet · 7 дней', seven_day_opus: 'Opus · 7 дней' })[info?.rateLimitType];
143
- const used = info?.status === 'rejected' ? 1 : info?.utilization;
144
- if (label && Number.isFinite(used) && used >= 0 && used <= 1) {
145
- const window = { label, usedPercent: used * 100 };
146
- if (Number.isFinite(info.resetsAt) && info.resetsAt > 0 && info.resetsAt <= 8.64e12) window.resetAt = info.resetsAt * 1000;
147
- await emit({ type: 'quota', usage: { updatedAt: Date.now(), windows: [window] } });
148
- }
144
+ const windows = claudeLimitWindows(event.rate_limit_info);
145
+ if (windows.length) await emit({ type: 'quota', usage: { updatedAt: Date.now(), windows } });
149
146
  }
150
147
  if (event.type === 'assistant' && Array.isArray(event.message?.content)) {
151
148
  for (const [index, block] of event.message.content.entries()) {
@@ -162,12 +159,24 @@ export async function runAgent({ provider, exe, env, cwd, permissionMode, thread
162
159
  else if (!text && event.result) await completedText(event.result, 'final-result');
163
160
  }
164
161
  }
162
+ // A command or tool result too large for one event line closes its call with a note
163
+ // instead of ending the turn. Lines that belong to no open call are skipped.
164
+ async function oversized(head, bytes) {
165
+ const id = provider === 'codex'
166
+ ? (/"type":"item\.completed"/.test(head.slice(0, 200)) ? head.slice(head.indexOf('"item":{')).match(/"id":"([^"]+)"/)?.[1] : undefined)
167
+ : head.match(/"tool_use_id":"([^"]+)"/)?.[1];
168
+ const item = id ? tools.get(id) : undefined;
169
+ if (!item || item.ended) return;
170
+ const note = `[CLI output omitted: ${(bytes / 1048576).toFixed(1)} MB is over the ${MAX_LINE / 1048576} MB event limit. The CLI still received it.]`;
171
+ await endTool(id, item.name, item.args, note, false);
172
+ }
165
173
  async function drain() {
166
174
  if (processing) return;
167
175
  processing = true;
168
176
  try {
169
177
  while (queue.length && !stopped) {
170
- const { line, bytes } = queue.shift(); queuedBytes -= bytes;
178
+ const { line, bytes, head } = queue.shift(); queuedBytes -= head === undefined ? bytes : head.length;
179
+ if (head !== undefined) { await oversized(head, bytes); continue; }
171
180
  let event;
172
181
  try { event = JSON.parse(line); } catch { throw new Error('CLI returned malformed JSONL'); }
173
182
  if (!event || typeof event !== 'object' || Array.isArray(event)) throw new Error('CLI returned invalid JSONL event');
@@ -177,19 +186,32 @@ export async function runAgent({ provider, exe, env, cwd, permissionMode, thread
177
186
  } catch (error) { stop(error); }
178
187
  finally { processing = false; if (closed && !queue.length) resolveDone(); }
179
188
  }
189
+ let skipping = null; // { head, bytes } while the rest of an oversized line is being drained
190
+ const queueOversized = ({ head, bytes }) => { queuedBytes += head.length; queue.push({ head, bytes }); };
180
191
  child.stdout.on('data', chunk => {
181
192
  if (stopped) return;
182
193
  stdoutBytes += chunk.length;
183
194
  if (stdoutBytes > 64 * MAX_PENDING) return stop('CLI output exceeds turn limit');
184
- pending += decoder.write(chunk); pendingBytes = Buffer.byteLength(pending);
185
- if (pendingBytes > MAX_PENDING) return stop('CLI JSONL buffer exceeds limit');
186
- let index;
187
- while ((index = pending.indexOf('\n')) !== -1) {
188
- const line = pending.slice(0, index).trim(); pending = pending.slice(index + 1);
189
- if (!line) continue;
190
- const bytes = Buffer.byteLength(line);
191
- if (bytes > MAX_LINE) return stop('CLI event exceeds line limit');
192
- queuedBytes += bytes; queue.push({ line, bytes });
195
+ let text = decoder.write(chunk);
196
+ while (text) {
197
+ if (skipping) {
198
+ const end = text.indexOf('\n');
199
+ skipping.bytes += Buffer.byteLength(end === -1 ? text : text.slice(0, end));
200
+ if (end === -1) break;
201
+ queueOversized(skipping); skipping = null; text = text.slice(end + 1);
202
+ continue;
203
+ }
204
+ pending += text; text = '';
205
+ let index;
206
+ while ((index = pending.indexOf('\n')) !== -1) {
207
+ const line = pending.slice(0, index).trim(); pending = pending.slice(index + 1);
208
+ if (!line) continue;
209
+ const bytes = Buffer.byteLength(line);
210
+ if (bytes > MAX_LINE) { queueOversized({ head: line.slice(0, OVERSIZED_HEAD), bytes }); continue; }
211
+ queuedBytes += bytes; queue.push({ line, bytes });
212
+ }
213
+ pendingBytes = Buffer.byteLength(pending);
214
+ if (pendingBytes > MAX_LINE) { skipping = { head: pending.slice(0, OVERSIZED_HEAD), bytes: pendingBytes }; pending = ''; }
193
215
  }
194
216
  if (queuedBytes > MAX_PENDING) return stop('CLI event queue exceeds limit');
195
217
  if (queuedBytes >= MAX_PENDING / 2) child.stdout.pause();
@@ -201,9 +223,11 @@ export async function runAgent({ provider, exe, env, cwd, permissionMode, thread
201
223
  child.once('close', (code, sig) => {
202
224
  closed = true; closeCode = code; closeSignal = sig;
203
225
  pending += decoder.end();
204
- if (pending.trim() && !stopped) {
226
+ if (skipping && !stopped) { queueOversized(skipping); skipping = null; }
227
+ else if (pending.trim() && !stopped) {
205
228
  const bytes = Buffer.byteLength(pending);
206
- if (bytes > MAX_LINE || queuedBytes + bytes > MAX_PENDING) stop('CLI trailing event exceeds limit');
229
+ if (bytes > MAX_LINE) queueOversized({ head: pending.trim().slice(0, OVERSIZED_HEAD), bytes });
230
+ else if (queuedBytes + bytes > MAX_PENDING) stop('CLI trailing event exceeds limit');
207
231
  else { queue.push({ line: pending.trim(), bytes }); queuedBytes += bytes; }
208
232
  }
209
233
  pending = '';
package/cli.js CHANGED
@@ -75,6 +75,19 @@ export function subscribed(provider, result) {
75
75
  return value.loggedIn === true && value.authMethod === 'claude.ai';
76
76
  } catch { return false; }
77
77
  }
78
+ const CLAUDE_WINDOWS = { five_hour:'5 часов', seven_day:'7 дней', seven_day_sonnet:'Sonnet · 7 дней', seven_day_opus:'Opus · 7 дней' };
79
+ /** Older Claude Code puts one window's numbers on rate_limit_info itself; 2.1.x lists every window under unifiedWindows. */
80
+ export function claudeLimitWindows(info) {
81
+ if (!info || typeof info !== 'object') return [];
82
+ const unified = info.unifiedWindows && typeof info.unifiedWindows === 'object' ? Object.entries(info.unifiedWindows) : [];
83
+ const entries = unified.some(([type]) => type === info.rateLimitType) ? unified : [[info.rateLimitType, info], ...unified];
84
+ return entries.flatMap(([type, lim]) => {
85
+ const label = CLAUDE_WINDOWS[type];
86
+ const used = lim?.status === 'rejected' ? 1 : lim?.utilization;
87
+ if (!label || !Number.isFinite(used) || used < 0 || used > 1) return [];
88
+ return [{ label, usedPercent: Math.round(used * 10000) / 100, ...(Number.isFinite(lim.resetsAt) && lim.resetsAt > 0 && lim.resetsAt <= 8.64e12 ? { resetAt: lim.resetsAt * 1000 } : {}) }];
89
+ });
90
+ }
78
91
  function readEvents(stdout) {
79
92
  return stdout.split(/\r?\n/).flatMap(line => { try { return [JSON.parse(line)]; } catch { return []; } });
80
93
  }
@@ -82,12 +95,7 @@ export function inference(provider, result) {
82
95
  const events = readEvents(result.stdout);
83
96
  const limits = events.filter(e => e?.type === 'rate_limit_event').map(e => e.rate_limit_info).filter(v => v && typeof v === 'object');
84
97
  const windows = new Map();
85
- for (const lim of limits) {
86
- const label = ({ five_hour:'5 часов', seven_day:'7 дней', seven_day_sonnet:'Sonnet · 7 дней', seven_day_opus:'Opus · 7 дней' })[lim.rateLimitType];
87
- const used = lim.status === 'rejected' ? 1 : lim.utilization;
88
- if (!label || !Number.isFinite(used) || used < 0 || used > 1) continue;
89
- windows.set(label, { label, usedPercent: used * 100, ...(Number.isFinite(lim.resetsAt) && lim.resetsAt > 0 ? { resetAt: lim.resetsAt * 1000 } : {}) });
90
- }
98
+ for (const w of limits.flatMap(claudeLimitWindows)) windows.set(w.label, w);
91
99
  const usage = windows.size ? { updatedAt: Date.now(), windows:[...windows.values()] } : undefined;
92
100
  const rejected = limits.some(x => x.status === 'rejected');
93
101
  const last = events.filter(e => e?.type === 'result').at(-1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-subscription-agents",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Use local Codex and Claude Code subscription accounts as DeepSeek Harness models and continuable child agents.",
5
5
  "keywords": [
6
6
  "dsh-plugin",