anyclaude-sdk 0.6.1 → 0.7.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/README.md CHANGED
@@ -390,6 +390,10 @@ Runnable Vite projects in [`examples/`](examples/): **`browser-ide`** (WebContai
390
390
  | MCP / slash commands / background tasks / sub-agents | Built-in | Built-in |
391
391
  | Serverless survivor + prompt projection | — | Built-in |
392
392
 
393
+ ## Telemetry
394
+
395
+ The SDK emits **anonymous, opt-out** usage telemetry (SDK version, runtime, a coarse model-family bucket, and which features are used) — never code, prompts, repo identity, paths, or keys, and a no-op unless you configure a collector. Disable with `ANYCLAUDE_TELEMETRY=0`, `DO_NOT_TRACK=1`, or `query({ disableTelemetry: true })`. Full disclosure: [TELEMETRY.md](TELEMETRY.md).
396
+
393
397
  ## License
394
398
 
395
399
  MIT
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: { trigger: 'manual', pre_tokens: 0 },
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
- if (estimateTokens(history) > threshold) {
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/index.d.ts CHANGED
@@ -25,4 +25,5 @@ export { uuid } from './util/ids.js';
25
25
  export * as paths from './util/paths.js';
26
26
  export { priceFor, computeCostUSD, contextWindowFor, type Pricing } from './util/pricing.js';
27
27
  export { estimateTokens, summarizeHistory, compactWithWindow } from './compact.js';
28
+ export { track, telemetryEnabled, detectRuntime, TELEMETRY_SDK_VERSION, type TelemetryOptions } from './telemetry.js';
28
29
  export { runToolLoop, type RunToolLoopOptions } from './loop.js';
package/dist/index.js CHANGED
@@ -27,5 +27,6 @@ export { uuid } from './util/ids.js';
27
27
  export * as paths from './util/paths.js';
28
28
  export { priceFor, computeCostUSD, contextWindowFor } from './util/pricing.js';
29
29
  export { estimateTokens, summarizeHistory, compactWithWindow } from './compact.js';
30
+ export { track, telemetryEnabled, detectRuntime, TELEMETRY_SDK_VERSION } from './telemetry.js';
30
31
  export { runToolLoop } from './loop.js';
31
32
  // (createResponsesClient is exported via ./llm/index.js)
package/dist/query.d.ts CHANGED
@@ -5,6 +5,7 @@ import type { SlashCommand } from './commands/index.js';
5
5
  import type { SessionStoreLike } from './session/index.js';
6
6
  import type { MemoryStore } from './memory/index.js';
7
7
  import { type Workspace } from './agent.js';
8
+ import { type TelemetryOptions } from './telemetry.js';
8
9
  export interface QueryOptions {
9
10
  /** A plain string (single turn) or a stream of user messages (multi-turn). */
10
11
  prompt: string | AsyncIterable<SDKUserMessage>;
@@ -109,6 +110,11 @@ export interface QueryOptions {
109
110
  settings?: boolean | import('./settings/index.js').Settings;
110
111
  /** Load `.claude/skills/*.md` as slash commands + a skill registry, or pass a Skill[]. */
111
112
  skills?: boolean | import('./skills/index.js').Skill[];
113
+ /** Anonymous, opt-out usage telemetry (version/runtime/which-features only — never
114
+ * code, prompts, repo, or keys). Configure or disable via `telemetry`; see TELEMETRY.md. */
115
+ telemetry?: TelemetryOptions;
116
+ /** Convenience to force telemetry off for this run (same as `telemetry: { disabled: true }`). */
117
+ disableTelemetry?: boolean;
112
118
  }
113
119
  /** An async iterator of SDK messages, augmented with session controls. */
114
120
  export interface Query extends AsyncGenerator<SDKMessage, void, void> {
package/dist/query.js CHANGED
@@ -3,6 +3,8 @@
3
3
  // Returns an AsyncGenerator<SDKMessage>. Accepts either a single string prompt
4
4
  // or an async iterable of SDKUserMessage (for multi-turn / interactive use).
5
5
  import { runAgent } from './agent.js';
6
+ import { track, telemetryEnabled } from './telemetry.js';
7
+ import { profileForModel } from './llm/profiles.js';
6
8
  export function query(options) {
7
9
  const prompt = typeof options.prompt === 'string'
8
10
  ? singlePrompt(options.prompt)
@@ -58,6 +60,29 @@ export function query(options) {
58
60
  skills: options.skills,
59
61
  });
60
62
  gen.interrupt = () => abortController.abort();
63
+ // Anonymous, aggregate adoption signal — one event per public run. Fire-and-forget,
64
+ // never blocks the generator, no-ops unless enabled + a collector is configured.
65
+ // Only booleans + a coarse model-family bucket leave the process (see telemetry.ts).
66
+ const telemetry = {
67
+ disabled: options.disableTelemetry,
68
+ ...options.telemetry,
69
+ };
70
+ if (telemetryEnabled(telemetry)) {
71
+ track('run', {
72
+ model_family: profileForModel(options.model).name,
73
+ client_workspace_tools: !!options.clientWorkspaceTools,
74
+ client_tools: !!options.clientTools?.length,
75
+ survivor: options.maxDurationMs != null,
76
+ mcp: !!options.mcpServers,
77
+ team: !!options.team,
78
+ background: !!options.background,
79
+ auto_compact: !!options.autoCompact,
80
+ skills: !!options.skills,
81
+ sessions: !!options.sessionStore,
82
+ partial_messages: !!options.includePartialMessages,
83
+ resumed: !!options.continueRun || !!options.resume,
84
+ }, telemetry);
85
+ }
61
86
  return gen;
62
87
  }
63
88
  /** Wrap a single text prompt into the async-iterable form runAgent expects. */
@@ -0,0 +1,17 @@
1
+ /** Bump on release so adoption can be bucketed by version. */
2
+ export declare const TELEMETRY_SDK_VERSION = "0.7.0";
3
+ export interface TelemetryOptions {
4
+ /** Force-disable for this call (highest precedence besides the global opt-outs). */
5
+ disabled?: boolean;
6
+ /** Collector URL. Defaults to `ANYCLAUDE_TELEMETRY_URL` then the built-in default. */
7
+ url?: string;
8
+ }
9
+ /** Resolve whether telemetry may run, honoring every documented opt-out. */
10
+ export declare function telemetryEnabled(opts?: TelemetryOptions): boolean;
11
+ /** Coarse runtime bucket — never anything machine-identifying. */
12
+ export declare function detectRuntime(): 'browser' | 'webcontainer' | 'bun' | 'node' | 'unknown';
13
+ /**
14
+ * Record an anonymous, aggregate event. No-op unless telemetry is enabled AND a
15
+ * collector URL is configured. Never blocks, never throws.
16
+ */
17
+ export declare function track(event: string, props?: Record<string, unknown>, opts?: TelemetryOptions): void;
@@ -0,0 +1,153 @@
1
+ // Anonymous, opt-out usage telemetry. The goal is a single, honest question:
2
+ // "are people adopting the SDK, and which parts?" — answered in AGGREGATE, never
3
+ // per-user. This module is deliberately conservative about what it can ever send.
4
+ //
5
+ // HARD GUARANTEES (enforced here, not just by convention):
6
+ // • It NEVER sends: repo/remote URLs, project/package names, file paths, source
7
+ // code, prompts, messages, tool arguments, LLM responses, API keys, or
8
+ // endpoints/base URLs. `track()` whitelists prop keys and keeps only booleans
9
+ // + a few coarse string buckets — anything else is dropped.
10
+ // • Off with one switch: `ANYCLAUDE_TELEMETRY=0`, `DO_NOT_TRACK=1`, any `CI`,
11
+ // a `disableTelemetry` option, browser `localStorage['anyclaude_telemetry']='0'`,
12
+ // or `globalThis.__ANYCLAUDE_NO_TELEMETRY__ = true`.
13
+ // • No endpoint configured ⇒ no-op (it can't send anywhere by default).
14
+ // • Fire-and-forget: never blocks, never throws, swallows all errors.
15
+ //
16
+ // See TELEMETRY.md for the full disclosure.
17
+ import { uuid } from './util/ids.js';
18
+ /** Bump on release so adoption can be bucketed by version. */
19
+ export const TELEMETRY_SDK_VERSION = '0.7.0';
20
+ // Set this (or `ANYCLAUDE_TELEMETRY_URL`) to your collector. Empty ⇒ no-op.
21
+ const DEFAULT_TELEMETRY_URL = '';
22
+ // Only these prop keys are ever transmitted, and only with safe value types.
23
+ // Booleans pass through; these specific string keys pass through as-is (they are
24
+ // coarse buckets we set ourselves — never free-form / user data).
25
+ const ALLOWED_STRING_KEYS = new Set(['model_family', 'event_detail']);
26
+ function readEnv(name) {
27
+ const p = globalThis.process;
28
+ return p?.env?.[name];
29
+ }
30
+ /** Resolve whether telemetry may run, honoring every documented opt-out. */
31
+ export function telemetryEnabled(opts) {
32
+ if (opts?.disabled)
33
+ return false;
34
+ const flag = (readEnv('ANYCLAUDE_TELEMETRY') ?? '').toLowerCase();
35
+ if (flag === '0' || flag === 'false' || flag === 'off' || flag === 'no')
36
+ return false;
37
+ const dnt = (readEnv('DO_NOT_TRACK') ?? '').toLowerCase();
38
+ if (dnt === '1' || dnt === 'true')
39
+ return false;
40
+ if (readEnv('CI'))
41
+ return false;
42
+ try {
43
+ const g = globalThis;
44
+ if (g.__ANYCLAUDE_NO_TELEMETRY__ === true)
45
+ return false;
46
+ const v = g.localStorage?.getItem('anyclaude_telemetry');
47
+ if (v === '0' || v === 'off' || v === 'false')
48
+ return false;
49
+ }
50
+ catch {
51
+ /* localStorage may throw in sandboxed contexts */
52
+ }
53
+ return true;
54
+ }
55
+ function telemetryUrl(opts) {
56
+ return opts?.url || readEnv('ANYCLAUDE_TELEMETRY_URL') || DEFAULT_TELEMETRY_URL;
57
+ }
58
+ /** Coarse runtime bucket — never anything machine-identifying. */
59
+ export function detectRuntime() {
60
+ const g = globalThis;
61
+ // WebContainer sets process.versions.webcontainer.
62
+ if (g.process?.versions?.webcontainer)
63
+ return 'webcontainer';
64
+ if (typeof g.window !== 'undefined' && typeof g.document !== 'undefined')
65
+ return 'browser';
66
+ if (g.Bun || g.process?.versions?.bun)
67
+ return 'bun';
68
+ if (g.process?.versions?.node)
69
+ return 'node';
70
+ return 'unknown';
71
+ }
72
+ // A random, NON-identifying id. Persisted in browser localStorage so repeated
73
+ // runs on one origin coalesce; per-process otherwise. Not tied to machine/user/IP.
74
+ let cachedInstallId = null;
75
+ function installId() {
76
+ if (cachedInstallId)
77
+ return cachedInstallId;
78
+ try {
79
+ const ls = globalThis.localStorage;
80
+ if (ls) {
81
+ const existing = ls.getItem('anyclaude_install_id');
82
+ if (existing)
83
+ return (cachedInstallId = existing);
84
+ const fresh = uuid();
85
+ ls.setItem('anyclaude_install_id', fresh);
86
+ return (cachedInstallId = fresh);
87
+ }
88
+ }
89
+ catch {
90
+ /* ignore */
91
+ }
92
+ return (cachedInstallId = uuid());
93
+ }
94
+ /** Keep only booleans + the allowlisted coarse string buckets. Everything else is dropped. */
95
+ function safeProps(props) {
96
+ const out = {};
97
+ for (const [k, v] of Object.entries(props)) {
98
+ if (typeof v === 'boolean')
99
+ out[k] = v;
100
+ else if (typeof v === 'number' && Number.isFinite(v))
101
+ out[k] = v;
102
+ else if (typeof v === 'string' && ALLOWED_STRING_KEYS.has(k))
103
+ out[k] = v.slice(0, 40);
104
+ }
105
+ return out;
106
+ }
107
+ let noticeShown = false;
108
+ function showNoticeOnce() {
109
+ if (noticeShown)
110
+ return;
111
+ noticeShown = true;
112
+ try {
113
+ const log = globalThis.console;
114
+ log?.error?.('[anyclaude-sdk] Anonymous usage telemetry is on (version + runtime + which features, no code/prompts/repo/keys). ' +
115
+ 'Opt out: ANYCLAUDE_TELEMETRY=0 (or DO_NOT_TRACK=1). See TELEMETRY.md.');
116
+ }
117
+ catch {
118
+ /* ignore */
119
+ }
120
+ }
121
+ /**
122
+ * Record an anonymous, aggregate event. No-op unless telemetry is enabled AND a
123
+ * collector URL is configured. Never blocks, never throws.
124
+ */
125
+ export function track(event, props = {}, opts) {
126
+ try {
127
+ if (!telemetryEnabled(opts))
128
+ return;
129
+ const url = telemetryUrl(opts);
130
+ if (!url)
131
+ return;
132
+ const f = globalThis.fetch;
133
+ if (!f)
134
+ return;
135
+ showNoticeOnce();
136
+ const body = JSON.stringify({
137
+ event,
138
+ sdk_version: TELEMETRY_SDK_VERSION,
139
+ runtime: detectRuntime(),
140
+ install: installId(),
141
+ ...safeProps(props),
142
+ });
143
+ void f(url, {
144
+ method: 'POST',
145
+ headers: { 'content-type': 'application/json' },
146
+ body,
147
+ keepalive: true,
148
+ }).catch(() => { });
149
+ }
150
+ catch {
151
+ /* telemetry must never affect the host app */
152
+ }
153
+ }
@@ -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.1",
3
+ "version": "0.7.0",
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",
@@ -71,6 +71,10 @@
71
71
  "./anthropic-endpoint": {
72
72
  "types": "./dist/anthropic-endpoint.d.ts",
73
73
  "import": "./dist/anthropic-endpoint.js"
74
+ },
75
+ "./telemetry": {
76
+ "types": "./dist/telemetry.d.ts",
77
+ "import": "./dist/telemetry.js"
74
78
  }
75
79
  },
76
80
  "sideEffects": false,