anyclaude-sdk 0.6.0 → 0.6.2
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/agent.js +32 -10
- package/dist/queue.d.ts +13 -2
- package/dist/queue.js +21 -2
- package/dist/types/index.d.ts +10 -0
- package/package.json +1 -1
package/dist/agent.js
CHANGED
|
@@ -480,7 +480,12 @@ export async function* runAgent(options) {
|
|
|
480
480
|
yield {
|
|
481
481
|
type: 'system',
|
|
482
482
|
subtype: 'compact_boundary',
|
|
483
|
-
compact_metadata: {
|
|
483
|
+
compact_metadata: {
|
|
484
|
+
trigger: 'manual',
|
|
485
|
+
pre_tokens: 0,
|
|
486
|
+
status: 'end',
|
|
487
|
+
post_tokens: Math.round(estimateTokens(history)),
|
|
488
|
+
},
|
|
484
489
|
uuid: uuid(),
|
|
485
490
|
session_id: sessionId,
|
|
486
491
|
};
|
|
@@ -581,21 +586,38 @@ export async function* runAgent(options) {
|
|
|
581
586
|
if (options.autoCompact && autoCompactCount < 3 && history.length > 3) {
|
|
582
587
|
const limit = options.contextLimit ?? (contextWindowFor(resultModel) || 200_000);
|
|
583
588
|
const threshold = (options.compactThreshold ?? 0.8) * limit;
|
|
584
|
-
|
|
589
|
+
const preTokens = estimateTokens(history);
|
|
590
|
+
if (preTokens > threshold) {
|
|
585
591
|
await runHooks('PreCompact', { hook_event_name: 'PreCompact', trigger: 'auto' });
|
|
592
|
+
// Emit a START boundary BEFORE the (possibly slow) summarization so a UI
|
|
593
|
+
// can show a live "compacting…" indicator while the LLM call is in flight.
|
|
594
|
+
yield {
|
|
595
|
+
type: 'system',
|
|
596
|
+
subtype: 'compact_boundary',
|
|
597
|
+
compact_metadata: { trigger: 'auto', pre_tokens: Math.round(preTokens), status: 'start' },
|
|
598
|
+
uuid: uuid(),
|
|
599
|
+
session_id: sessionId,
|
|
600
|
+
};
|
|
586
601
|
const compacted = await summarizeHistory(history, llm, { model, signal });
|
|
587
602
|
if (compacted) {
|
|
588
603
|
history.splice(0, history.length, ...compacted);
|
|
589
604
|
autoCompactCount++;
|
|
590
|
-
yield {
|
|
591
|
-
type: 'system',
|
|
592
|
-
subtype: 'compact_boundary',
|
|
593
|
-
compact_metadata: { trigger: 'auto', pre_tokens: Math.round(threshold) },
|
|
594
|
-
uuid: uuid(),
|
|
595
|
-
session_id: sessionId,
|
|
596
|
-
};
|
|
597
|
-
await runHooks('PostCompact', { hook_event_name: 'PostCompact', trigger: 'auto' });
|
|
598
605
|
}
|
|
606
|
+
// END boundary closes the live indicator either way (compacted or not).
|
|
607
|
+
yield {
|
|
608
|
+
type: 'system',
|
|
609
|
+
subtype: 'compact_boundary',
|
|
610
|
+
compact_metadata: {
|
|
611
|
+
trigger: 'auto',
|
|
612
|
+
pre_tokens: Math.round(preTokens),
|
|
613
|
+
status: 'end',
|
|
614
|
+
post_tokens: Math.round(estimateTokens(history)),
|
|
615
|
+
},
|
|
616
|
+
uuid: uuid(),
|
|
617
|
+
session_id: sessionId,
|
|
618
|
+
};
|
|
619
|
+
if (compacted)
|
|
620
|
+
await runHooks('PostCompact', { hook_event_name: 'PostCompact', trigger: 'auto' });
|
|
599
621
|
}
|
|
600
622
|
}
|
|
601
623
|
let streamedText = '';
|
package/dist/queue.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { ContentBlockParam } from './types/index.js';
|
|
2
2
|
export type QueuedContent = string | ContentBlockParam[];
|
|
3
3
|
export interface QueuedMessage {
|
|
4
|
+
/** Stable id for this queued item — use it to `remove()` a single message. */
|
|
5
|
+
id: string;
|
|
4
6
|
content: QueuedContent;
|
|
5
7
|
/** Epoch ms when enqueued. */
|
|
6
8
|
at: number;
|
|
@@ -8,10 +10,19 @@ export interface QueuedMessage {
|
|
|
8
10
|
export declare class MessageQueue {
|
|
9
11
|
private items;
|
|
10
12
|
private listeners;
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Enqueue a user message to be delivered at the next turn boundary.
|
|
15
|
+
* Returns the item's stable `id` (pass it to `remove()` to cancel just this one).
|
|
16
|
+
*/
|
|
17
|
+
push(content: QueuedContent): string;
|
|
13
18
|
/** Remove and return the oldest queued message (FIFO), or undefined if empty. */
|
|
14
19
|
shift(): QueuedMessage | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Remove a single pending message by its `id` (e.g. a per-pill ✕ in the UI).
|
|
22
|
+
* Returns true if an item was removed. No-op if the id isn't pending — already
|
|
23
|
+
* drained (shifted) items can't be cancelled.
|
|
24
|
+
*/
|
|
25
|
+
remove(id: string): boolean;
|
|
15
26
|
peek(): QueuedMessage | undefined;
|
|
16
27
|
get size(): number;
|
|
17
28
|
/** Snapshot of pending messages (does not drain). */
|
package/dist/queue.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
import { uuid } from './util/ids.js';
|
|
1
2
|
export class MessageQueue {
|
|
2
3
|
constructor() {
|
|
3
4
|
this.items = [];
|
|
4
5
|
this.listeners = new Set();
|
|
5
6
|
}
|
|
6
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Enqueue a user message to be delivered at the next turn boundary.
|
|
9
|
+
* Returns the item's stable `id` (pass it to `remove()` to cancel just this one).
|
|
10
|
+
*/
|
|
7
11
|
push(content) {
|
|
8
|
-
|
|
12
|
+
const id = uuid();
|
|
13
|
+
this.items.push({ id, content, at: Date.now() });
|
|
9
14
|
this.emit();
|
|
15
|
+
return id;
|
|
10
16
|
}
|
|
11
17
|
/** Remove and return the oldest queued message (FIFO), or undefined if empty. */
|
|
12
18
|
shift() {
|
|
@@ -15,6 +21,19 @@ export class MessageQueue {
|
|
|
15
21
|
this.emit();
|
|
16
22
|
return m;
|
|
17
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Remove a single pending message by its `id` (e.g. a per-pill ✕ in the UI).
|
|
26
|
+
* Returns true if an item was removed. No-op if the id isn't pending — already
|
|
27
|
+
* drained (shifted) items can't be cancelled.
|
|
28
|
+
*/
|
|
29
|
+
remove(id) {
|
|
30
|
+
const i = this.items.findIndex((m) => m.id === id);
|
|
31
|
+
if (i < 0)
|
|
32
|
+
return false;
|
|
33
|
+
this.items.splice(i, 1);
|
|
34
|
+
this.emit();
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
18
37
|
peek() {
|
|
19
38
|
return this.items[0];
|
|
20
39
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -162,7 +162,17 @@ export type SDKCompactBoundaryMessage = {
|
|
|
162
162
|
subtype: 'compact_boundary';
|
|
163
163
|
compact_metadata: {
|
|
164
164
|
trigger: 'manual' | 'auto';
|
|
165
|
+
/** Transcript token estimate when compaction began. */
|
|
165
166
|
pre_tokens: number;
|
|
167
|
+
/**
|
|
168
|
+
* Compaction phase: `'start'` is emitted BEFORE the (possibly slow)
|
|
169
|
+
* summarization so a UI can show a live "compacting…" indicator; `'end'`
|
|
170
|
+
* is emitted after, with `post_tokens` set. Absent ⇒ treat as `'end'`
|
|
171
|
+
* (back-compat: pre-0.6.2 only emitted the post-compaction boundary).
|
|
172
|
+
*/
|
|
173
|
+
status?: 'start' | 'end';
|
|
174
|
+
/** Transcript token estimate after compaction (only on `status: 'end'`). */
|
|
175
|
+
post_tokens?: number;
|
|
166
176
|
};
|
|
167
177
|
uuid: string;
|
|
168
178
|
session_id: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anyclaude-sdk",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Standalone, browser-compatible SDK providing Claude Code agent capabilities (tools, tool loop, multi-turn, MCP, sub-agents, sessions) against any OpenAI/Anthropic-compatible LLM endpoint. Runs in the browser (WebContainer), Node, and Bun — no backend required.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|