@silverprotocol/claude-agent-sdk 0.7.1 → 0.8.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
@@ -76,6 +76,17 @@ events.push(...n.flush()); // seal anything still open at end-of-stream
76
76
  Malformed input never throws — it routes through the lossless
77
77
  `ext.anthropic.unparsed` channel instead.
78
78
 
79
+ ### Thread id
80
+
81
+ Pass the host's own thread id as `createClaudeNormalizer({ threadId })`. The
82
+ normalizer stamps it as the `threadId` of every turn and message it opens,
83
+ subagent turns included: the partition root a key-value store writes each unit
84
+ under (spec §1.2). The Claude Agent SDK wire has no thread concept, only
85
+ `session_id`, so with no `threadId` option the facet stamps the SDK's
86
+ `session_id` (or, on a frame that carries none, the turn's own id) as a
87
+ facet-local placeholder, not a partition root; a host that persists by
88
+ `threadId` supplies its own.
89
+
79
90
  ### Token streaming
80
91
 
81
92
  Pass `includePartialMessages: true` to `query()` and the normalizer emits
@@ -90,15 +101,80 @@ const response = query({
90
101
  ```
91
102
 
92
103
  No other change is needed. The complete assistant message the SDK still emits
93
- after the partials is deduplicated automatically (nothing is folded twice), the
94
- final reduced state is identical either way, and streams without partials
95
- produce byte-identical output to previous releases. Time-to-first-token rides
96
- `message.metadata` as `ttft_ms` when the SDK reports it.
104
+ after the partials is deduplicated automatically (nothing is folded twice), and
105
+ the final reduced state is identical either way except for the assistant
106
+ message's `usage.outputTokens`, which only the stream reports (see
107
+ [Usage](#usage)). Time-to-first-token rides `message.metadata` as `ttft_ms`
108
+ when the SDK reports it.
97
109
 
98
110
  Then fold the resulting `AgEvent`s into messages and turns with
99
111
  `@silverprotocol/core`'s `reduce()` — the same client code regardless of which
100
112
  framework produced the stream.
101
113
 
114
+ ### Deferred tools
115
+
116
+ When a host's `PreToolUse` hook defers a tool call, the SDK's result names the
117
+ parked call in `deferred_tool_use`, with `stop_reason: "tool_deferred"`. The
118
+ normalizer then emits one `hitl.ask` (`kind: "approval"`, `toolCallId` = the
119
+ parked call) and closes the turn with
120
+ `turn.done {outcome: {type: "paused", asks}, finishReason: "paused"}`. The
121
+ result's `deferred_tool_use` also rides `ext.anthropic.result-meta` verbatim.
122
+ An error result is never a pause. The host answers through its own hook on a
123
+ resumed session; the resumed invoke opens its own turn, and the call's result
124
+ lands there as a `role: "tool"` message.
125
+
126
+ ### Usage
127
+
128
+ Usage follows the AgJSON draft.5 accounting rules (spec §4):
129
+
130
+ - **`inputTokens` includes cache tokens** on every usage object the normalizer
131
+ emits: turn, message and `byModel` entries. Anthropic's `input_tokens`
132
+ excludes cache reads and writes, so the normalizer adds
133
+ `cache_read_input_tokens` and `cache_creation_input_tokens` in.
134
+ `cacheReadTokens` and `cacheWriteTokens` are breakdowns of `inputTokens`;
135
+ never add them to it.
136
+ - **A turn terminal's token counters cover that turn**, so its `usage` carries
137
+ `cumulative: false`. Its `costUsd` is the SDK's `total_cost_usd`, a running
138
+ estimate over the whole `query()` call, and carries `costScope: "query"`:
139
+ take the latest value rather than summing across turns. Per the SDK's docs, a
140
+ resumed or forked session continues from the total its transcript saved, and
141
+ a mid-session `/clear` resets it. The cost covers every call the query made,
142
+ so it does not reconcile with one turn's token counters.
143
+ - **`byModel` entries are the SDK's per-model running totals** over the same
144
+ `query()` call (`cumulative: true`). They cover every model call in the query
145
+ pipeline, subagents and internal calls such as compaction included, so they
146
+ can exceed the turn's own counters.
147
+ - **Message-level usage** is the message stream's running total
148
+ (`cumulative: true`). Without partial messages, an assistant message's
149
+ `output_tokens` is a placeholder for a count the SDK reports on the result,
150
+ so `message.end.usage` omits `outputTokens`; the turn terminal carries the
151
+ turn's output count.
152
+ - **Error results:** a success-subtype result carries its usage whether it
153
+ closes the turn as `turn.done` or, on an API error, as `turn.error`. An
154
+ error-subtype result (`error_max_turns`, `error_during_execution`, …) carries
155
+ its usage only when an API-error assistant message had already ended the
156
+ turn; otherwise its `turn.error` carries none.
157
+
158
+ ### Turn trigger
159
+
160
+ When the frame that opens a top-level turn is an assistant message, a streamed
161
+ `stream_event` whose `event.type` is `message_start`, or a success result, and
162
+ it carries `user_message_uuid`, that turn's `turn.start` carries
163
+ `trigger: {kind: "user", ref: <that uuid>}`, and the folded
164
+ `AgTurnRecord.trigger` equals it. The SDK echoes a uuid when the host set
165
+ `uuid` on the `SDKUserMessage` it sent in streaming input, and also for a meta
166
+ turn the host vouches for. `kind: "user"` means a user-role message submitted
167
+ with that uuid; it does not claim that a human typed it. Provenance, where the
168
+ SDK reports it, stays on `ext.anthropic.result-meta.origin`. `ref` names one
169
+ send: for a batch the host merged into one turn, it is the batch's last
170
+ member, and the full list stays in `user_message_uuids`. Two turns in one fold
171
+ can carry the same `ref`. The facet sets `trigger` only from the frame that
172
+ opens the turn and never re-sends `turn.start` to add one. So a turn opened by
173
+ a notice, a tool result or an error result carries no `trigger`, and neither
174
+ does a turn whose uuid first appears on a later frame. A missing `trigger`
175
+ does not mean the turn answers no send of yours. Turns the CLI starts itself
176
+ and subagent turns never carry it.
177
+
102
178
  Spec: [silverprotocol.io/AgJSON](https://silverprotocol.io/AgJSON) — canonical
103
179
  in [silverprotocol/AgJSON](https://github.com/silverprotocol/AgJSON); wire
104
- version `1.0.0-draft.4`.
180
+ version `1.0.0-draft.5`.
package/dist/index.d.ts CHANGED
@@ -45,12 +45,13 @@ declare function mapStopReason(stop: BetaStopReason | string | null): AgFinishRe
45
45
  export interface ClaudeNormalizerOptions {
46
46
  /**
47
47
  * The partition-root `threadId` stamped on every entity this normalizer
48
- * emits (SPEC §3: the root each unit carries so a key-value persistence
49
- * layer can write it knowing only its own id, its parent, and the
50
- * partition root). The Claude Agent SDK wire has no thread concept — only
51
- * `session_id` — so with this option ABSENT the adapter relabels
52
- * `session_id` as the threadId, a placeholder in the same spirit as the
53
- * openai/vercel facets' fixed labels. That placeholder is fine for
48
+ * emits (SPEC §1.2, the partition root, and §8.0 host obligation 6: the root
49
+ * each unit carries so a key-value persistence layer can write it knowing
50
+ * only its own id, its parent, and the partition root). The Claude Agent SDK
51
+ * wire has no thread concept — only `session_id` — so with this option ABSENT
52
+ * the adapter relabels `session_id` as the threadId (or, on a leading
53
+ * tool_result frame that carries none, the turn's own id), a placeholder in
54
+ * the same spirit as the openai/vercel facets' fixed labels. That placeholder is fine for
54
55
  * self-contained streams but LEAKS into any consumer that persists
55
56
  * events verbatim under its own thread identity (guuey#415: mid-stream
56
57
  * events carried the session id while the runtime's session records
@@ -66,7 +67,7 @@ export interface ClaudeNormalizerOptions {
66
67
  * turn by its SDK message id or frame uuid, unique by construction). A host
67
68
  * folds every invoke of a conversation into ONE Reducer, so a stem that
68
69
  * restarted with each invoke repeated those ids across invokes (DC-10, from
69
- * sp-protocol's D3 bar). Absent, each normalizer draws a random
70
+ * the D3 review). Absent, each normalizer draws a random
70
71
  * `claude_<16 hex>` stem at most once, lazily (only when a fallback id is
71
72
  * first needed, so the common path draws no randomness), and holds it
72
73
  * OUTSIDE the atomic-push rebuild, so a rebuild reproduces it. Pass a fixed
@@ -77,7 +78,7 @@ export interface ClaudeNormalizerOptions {
77
78
  }
78
79
  /**
79
80
  * Build the Claude-facet normalizer: the inner normalizer wrapped in core's
80
- * withAtomicPush (the fleet guard ruling, 2026-09-24, binding). push() never
81
+ * withAtomicPush (the guard ruling, 2026-09-24, binding). push() never
81
82
  * throws (SPEC:933):
82
83
  * - each native is first read as plain JSON (core toJsonValueSafe): a host
83
84
  * may hand in the in-process object, whose members need not be JSON (an
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAGL,KAAK,cAAc,EAQnB,KAAK,UAAU,EAIhB,MAAM,sBAAsB,CAAC;AAO9B,KAAK,YAAY,GAAG,OAAO,CAAC,UAAU,EAAE;IAAE,IAAI,EAAE,WAAW,CAAA;CA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