@silverprotocol/claude-agent-sdk 0.7.2 → 0.9.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,85 @@ 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. With parallel tool calls the SDK can deliver one
109
+ call's result while a later call's input is still streaming; the normalizer
110
+ lands that result in its own tool message and never seals the streamed
111
+ assistant message on a tool result: the message closes with the next message
112
+ or the turn's close, so each call is started once and carries its complete
113
+ input, and the message keeps its output count.
97
114
 
98
115
  Then fold the resulting `AgEvent`s into messages and turns with
99
116
  `@silverprotocol/core`'s `reduce()` — the same client code regardless of which
100
117
  framework produced the stream.
101
118
 
119
+ ### Deferred tools
120
+
121
+ When a host's `PreToolUse` hook defers a tool call, the SDK's result names the
122
+ parked call in `deferred_tool_use`, with `stop_reason: "tool_deferred"`. The
123
+ normalizer then emits one `hitl.ask` (`kind: "approval"`, `toolCallId` = the
124
+ parked call) and closes the turn with
125
+ `turn.done {outcome: {type: "paused", asks}, finishReason: "paused"}`. The
126
+ result's `deferred_tool_use` also rides `ext.anthropic.result-meta` verbatim.
127
+ An error result is never a pause. The host answers through its own hook on a
128
+ resumed session; the resumed invoke opens its own turn, and the call's result
129
+ lands there as a `role: "tool"` message.
130
+
131
+ ### Usage
132
+
133
+ Usage follows the AgJSON draft.5 accounting rules (spec §4):
134
+
135
+ - **`inputTokens` includes cache tokens** on every usage object the normalizer
136
+ emits: turn, message and `byModel` entries. Anthropic's `input_tokens`
137
+ excludes cache reads and writes, so the normalizer adds
138
+ `cache_read_input_tokens` and `cache_creation_input_tokens` in.
139
+ `cacheReadTokens` and `cacheWriteTokens` are breakdowns of `inputTokens`;
140
+ never add them to it.
141
+ - **A turn terminal's token counters cover that turn**, so its `usage` carries
142
+ `cumulative: false`. Its `costUsd` is the SDK's `total_cost_usd`, a running
143
+ estimate over the whole `query()` call, and carries `costScope: "query"`:
144
+ take the latest value rather than summing across turns. Per the SDK's docs, a
145
+ resumed or forked session continues from the total its transcript saved, and
146
+ a mid-session `/clear` resets it. The cost covers every call the query made,
147
+ so it does not reconcile with one turn's token counters.
148
+ - **`byModel` entries are the SDK's per-model running totals** over the same
149
+ `query()` call (`cumulative: true`). They cover every model call in the query
150
+ pipeline, subagents and internal calls such as compaction included, so they
151
+ can exceed the turn's own counters.
152
+ - **Message-level usage** is the message stream's running total
153
+ (`cumulative: true`). Without partial messages, an assistant message's
154
+ `output_tokens` is a placeholder for a count the SDK reports on the result,
155
+ so `message.end.usage` omits `outputTokens`; the turn terminal carries the
156
+ turn's output count.
157
+ - **Error results:** a success-subtype result carries its usage whether it
158
+ closes the turn as `turn.done` or, on an API error, as `turn.error`. An
159
+ error-subtype result (`error_max_turns`, `error_during_execution`, …) carries
160
+ its usage only when an API-error assistant message had already ended the
161
+ turn; otherwise its `turn.error` carries none.
162
+
163
+ ### Turn trigger
164
+
165
+ When the frame that opens a top-level turn is an assistant message, a streamed
166
+ `stream_event` whose `event.type` is `message_start`, or a success result, and
167
+ it carries `user_message_uuid`, that turn's `turn.start` carries
168
+ `trigger: {kind: "user", ref: <that uuid>}`, and the folded
169
+ `AgTurnRecord.trigger` equals it. The SDK echoes a uuid when the host set
170
+ `uuid` on the `SDKUserMessage` it sent in streaming input, and also for a meta
171
+ turn the host vouches for. `kind: "user"` means a user-role message submitted
172
+ with that uuid; it does not claim that a human typed it. Provenance, where the
173
+ SDK reports it, stays on `ext.anthropic.result-meta.origin`. `ref` names one
174
+ send: for a batch the host merged into one turn, it is the batch's last
175
+ member, and the full list stays in `user_message_uuids`. Two turns in one fold
176
+ can carry the same `ref`. The facet sets `trigger` only from the frame that
177
+ opens the turn and never re-sends `turn.start` to add one. So a turn opened by
178
+ a notice, a tool result or an error result carries no `trigger`, and neither
179
+ does a turn whose uuid first appears on a later frame. A missing `trigger`
180
+ does not mean the turn answers no send of yours. Turns the CLI starts itself
181
+ and subagent turns never carry it.
182
+
102
183
  Spec: [silverprotocol.io/AgJSON](https://silverprotocol.io/AgJSON) — canonical
103
184
  in [silverprotocol/AgJSON](https://github.com/silverprotocol/AgJSON); wire
104
- version `1.0.0-draft.4`.
185
+ version `1.0.0-draft.6`.
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;AACj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