@namzu/openrouter 0.1.2 → 1.0.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/CHANGELOG.md +170 -0
- package/dist/client.d.ts +1 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +0 -35
- package/dist/client.js.map +1 -1
- package/package.json +3 -3
- package/src/client.ts +0 -60
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,175 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [542f057]
|
|
8
|
+
- Updated dependencies [df09910]
|
|
9
|
+
- Updated dependencies [140bcc0]
|
|
10
|
+
- Updated dependencies [ea21863]
|
|
11
|
+
- Updated dependencies [38c4b62]
|
|
12
|
+
- Updated dependencies [265150b]
|
|
13
|
+
- Updated dependencies [a1c6694]
|
|
14
|
+
- Updated dependencies [52af97e]
|
|
15
|
+
- Updated dependencies [a71422a]
|
|
16
|
+
- Updated dependencies [d6b5bc1]
|
|
17
|
+
- Updated dependencies [8fd9349]
|
|
18
|
+
- Updated dependencies [63e44f7]
|
|
19
|
+
- Updated dependencies [63b4885]
|
|
20
|
+
- Updated dependencies [38c4b62]
|
|
21
|
+
- Updated dependencies [6b74cd0]
|
|
22
|
+
- Updated dependencies [d86b161]
|
|
23
|
+
- @namzu/sdk@1.0.0
|
|
24
|
+
|
|
25
|
+
## 0.2.0
|
|
26
|
+
|
|
27
|
+
### Minor Changes
|
|
28
|
+
|
|
29
|
+
- 2749d32: RunEvent v3 + streaming-only `LLMProvider` (ses_001-tool-stream-events).
|
|
30
|
+
|
|
31
|
+
The kernel now emits a per-message and per-tool-input lifecycle on the
|
|
32
|
+
event bus, and the provider contract collapses to a single streaming
|
|
33
|
+
entry point. Together these unlock live tool-call rendering (Calling →
|
|
34
|
+
Running → Done with incremental input) for SSE consumers — the cowork
|
|
35
|
+
workspace surface that motivated the work in the first place.
|
|
36
|
+
|
|
37
|
+
## Breaking changes
|
|
38
|
+
|
|
39
|
+
### `LLMProvider.chat()` removed
|
|
40
|
+
|
|
41
|
+
`LLMProvider` exposes a single LLM entry point: `chatStream()`. The
|
|
42
|
+
non-streaming `chat()` method is gone from every shipped provider
|
|
43
|
+
(`@namzu/anthropic`, `@namzu/openai`, `@namzu/bedrock`,
|
|
44
|
+
`@namzu/openrouter`, `@namzu/http`, `@namzu/ollama`,
|
|
45
|
+
`@namzu/lmstudio`).
|
|
46
|
+
|
|
47
|
+
Consumers that need an aggregated `ChatCompletionResponse` use the new
|
|
48
|
+
helper:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { collect } from "@namzu/sdk";
|
|
52
|
+
|
|
53
|
+
const response = await collect(provider.chatStream(params));
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`collect()` drains the stream and assembles the legacy response shape:
|
|
57
|
+
text concatenated in arrival order, tool calls bucketed by index,
|
|
58
|
+
latest `finishReason` and `usage` win, defaults to `{ finishReason:
|
|
59
|
+
'stop', zero usage }` when the provider omits them (defensive against
|
|
60
|
+
SDK quirks like dropped `message_stop` frames).
|
|
61
|
+
|
|
62
|
+
The orchestrator consumes the stream directly so it can emit per-delta
|
|
63
|
+
RunEvents — it does NOT call `collect()`.
|
|
64
|
+
|
|
65
|
+
### `RunEvent` envelope `schemaVersion: 2 → 3`
|
|
66
|
+
|
|
67
|
+
`RUN_EVENT_SCHEMA_VERSION` is now `3`. The envelope narrows from `2 |
|
|
68
|
+
3` to `3`; sub-session lifecycle events stamp `3` automatically via
|
|
69
|
+
`RunEventSchemaVersion`.
|
|
70
|
+
|
|
71
|
+
### `llm_response` removed
|
|
72
|
+
|
|
73
|
+
The coarse `llm_response` event is replaced by a message lifecycle:
|
|
74
|
+
|
|
75
|
+
- `message_started { runId, iteration, messageId }` — first chunk arrives.
|
|
76
|
+
- `text_delta { runId, iteration, messageId, text }` — per-chunk text.
|
|
77
|
+
- `message_completed { runId, iteration, messageId, stopReason, usage?, content? }` — provider stream closes.
|
|
78
|
+
|
|
79
|
+
`message_completed.content` is the aggregated text and is optional —
|
|
80
|
+
consumers that already accumulate `text_delta` themselves can ignore
|
|
81
|
+
it; consumers that only care about the completed message (telemetry,
|
|
82
|
+
A2A bridge) read it directly.
|
|
83
|
+
|
|
84
|
+
`stopReason` is the new `MessageStopReason` union: `'end_turn' |
|
|
85
|
+
'tool_use' | 'max_tokens' | 'stop_sequence' | 'pause_turn' | 'refusal'
|
|
86
|
+
| 'forced_finalize'`.
|
|
87
|
+
|
|
88
|
+
### Tool input lifecycle
|
|
89
|
+
|
|
90
|
+
Tool calls now traverse a five-event lifecycle keyed by `toolUseId`:
|
|
91
|
+
|
|
92
|
+
- `tool_input_started { runId, iteration, messageId, toolUseId, toolName }`
|
|
93
|
+
- `tool_input_delta { runId, toolUseId, partialJson }` — raw fragment
|
|
94
|
+
- `tool_input_completed { runId, toolUseId, input }` — parsed object
|
|
95
|
+
- `tool_executing { runId, toolUseId, toolName, input }` — runtime invokes
|
|
96
|
+
- `tool_completed { runId, toolUseId, toolName, result, isError }` — required `isError`
|
|
97
|
+
|
|
98
|
+
`tool_executing` and `tool_completed` payloads tighten: `toolUseId`
|
|
99
|
+
becomes required on both, `isError` becomes required on
|
|
100
|
+
`tool_completed`. The wire-level `tool.error` event is dropped — the
|
|
101
|
+
boolean carries the same signal without ambiguity.
|
|
102
|
+
|
|
103
|
+
Probe veto, malformed JSON args, plugin hook errors, and exception
|
|
104
|
+
throws inside `tools.execute()` all now emit a terminal
|
|
105
|
+
`tool_completed { isError: true }` so consumer UI cards can finalise
|
|
106
|
+
instead of being orphaned.
|
|
107
|
+
|
|
108
|
+
### Ephemeral events skip persistence
|
|
109
|
+
|
|
110
|
+
`text_delta` and `tool_input_delta` are flagged `isEphemeralEvent()`
|
|
111
|
+
and bypass `transcript.jsonl`. They live only on the in-memory bus
|
|
112
|
+
for live UI rendering. Replay is unaffected (it reads checkpoints,
|
|
113
|
+
not transcripts). The bus has a 1000-event soft cap; under pressure
|
|
114
|
+
the oldest ephemeral is dropped while lifecycle events are preserved.
|
|
115
|
+
|
|
116
|
+
### `StreamChunk.delta.toolCallEnd`
|
|
117
|
+
|
|
118
|
+
New optional field signalling per-tool-block boundary closure. The
|
|
119
|
+
orchestrator translates it into `tool_input_completed`. Providers
|
|
120
|
+
that emit a per-tool-block close (Anthropic `content_block_stop` of
|
|
121
|
+
type `tool_use`, Bedrock equivalent) populate it; providers that
|
|
122
|
+
don't fall back to end-of-stream flushing.
|
|
123
|
+
|
|
124
|
+
## Migration
|
|
125
|
+
|
|
126
|
+
Most consumers only use the iteration orchestrator's emitted
|
|
127
|
+
`RunEvent` stream. They:
|
|
128
|
+
|
|
129
|
+
1. Replace `case 'llm_response':` handlers with a `case
|
|
130
|
+
'message_completed':` handler reading `event.content`.
|
|
131
|
+
2. Drop any reads of `event.hasToolCalls` — derive from the
|
|
132
|
+
subsequent absence/presence of `tool_executing` events keyed by
|
|
133
|
+
the same `runId`.
|
|
134
|
+
3. Optional: subscribe to `text_delta` and `tool_input_*` for live
|
|
135
|
+
rendering. The events are interleaved by `toolUseId` to support
|
|
136
|
+
parallel tool calls.
|
|
137
|
+
|
|
138
|
+
Consumers calling `provider.chat()` directly:
|
|
139
|
+
|
|
140
|
+
```diff
|
|
141
|
+
- const response = await provider.chat(params)
|
|
142
|
+
+ import { collect } from '@namzu/sdk'
|
|
143
|
+
+ const response = await collect(provider.chatStream(params))
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Aggregated response shape is identical.
|
|
147
|
+
|
|
148
|
+
## Internal surface (not externally consumed)
|
|
149
|
+
|
|
150
|
+
- `runtime/query/iteration/index.ts` — new `streamProviderTurn()`
|
|
151
|
+
helper, replaces synthesised `message_started`/`message_completed`
|
|
152
|
+
with native streaming. `forced_finalize` path uses `collect()`.
|
|
153
|
+
- `provider/instrumentation.ts` — captures `usage` from the last
|
|
154
|
+
chunk that supplies one (`extractStreamUsage`).
|
|
155
|
+
- `runtime/query/events.ts` — `EventTranslator.emitEvent` skips
|
|
156
|
+
`appendEvent()` for ephemeral events, applies the queue cap.
|
|
157
|
+
- `bridge/sse/mapper.ts` — six new wire types
|
|
158
|
+
(`message.created/delta/completed`,
|
|
159
|
+
`tool.input_started/delta/completed`); `tool.error` removed;
|
|
160
|
+
`tool.executing/completed` carry `tool_use_id` + `is_error`.
|
|
161
|
+
- `bridge/a2a/mapper.ts` — `message_completed.content` routes to A2A
|
|
162
|
+
status update, replacing the per-iteration `llm_response` mapping.
|
|
163
|
+
|
|
164
|
+
## Tests
|
|
165
|
+
|
|
166
|
+
SDK suite at 958 (was 943 + new contracts − removed
|
|
167
|
+
`chat()`/`llm_response` invariants). `pnpm typecheck && pnpm lint &&
|
|
168
|
+
pnpm test && pnpm build` all green across every package. The
|
|
169
|
+
`@namzu/http` request-construction and response-parsing suites have
|
|
170
|
+
10 tests marked `.skip` pending an SSE-mock rewrite — the streaming
|
|
171
|
+
path is still covered by the existing streaming-suite tests.
|
|
172
|
+
|
|
3
173
|
## 0.1.2
|
|
4
174
|
|
|
5
175
|
### Patch Changes
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ChatCompletionParams,
|
|
1
|
+
import type { ChatCompletionParams, LLMProvider, ModelInfo, StreamChunk } from '@namzu/sdk';
|
|
2
2
|
import type { OpenRouterConfig } from './types.js';
|
|
3
3
|
export declare class OpenRouterProvider implements LLMProvider {
|
|
4
4
|
readonly id = "openrouter";
|
|
@@ -9,7 +9,6 @@ export declare class OpenRouterProvider implements LLMProvider {
|
|
|
9
9
|
private getHeaders;
|
|
10
10
|
private formatMessages;
|
|
11
11
|
private buildRequestBody;
|
|
12
|
-
chat(params: ChatCompletionParams): Promise<ChatCompletionResponse>;
|
|
13
12
|
chatStream(params: ChatCompletionParams): AsyncIterable<StreamChunk>;
|
|
14
13
|
listModels(): Promise<ModelInfo[]>;
|
|
15
14
|
healthCheck(): Promise<boolean>;
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,oBAAoB,EACpB,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,oBAAoB,EACpB,WAAW,EACX,SAAS,EACT,WAAW,EAGX,MAAM,YAAY,CAAA;AACnB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAwClD,qBAAa,kBAAmB,YAAW,WAAW;IACrD,QAAQ,CAAC,EAAE,gBAAe;IAC1B,QAAQ,CAAC,IAAI,gBAAe;IAE5B,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,OAAO,CAAQ;gBAEX,MAAM,EAAE,gBAAgB;IAQpC,OAAO,CAAC,UAAU;IAclB,OAAO,CAAC,cAAc;IAwBtB,OAAO,CAAC,gBAAgB;IAqCjB,UAAU,CAAC,MAAM,EAAE,oBAAoB,GAAG,aAAa,CAAC,WAAW,CAAC;IAyFrE,UAAU,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IA+BlC,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;CAWrC"}
|
package/dist/client.js
CHANGED
|
@@ -109,41 +109,6 @@ export class OpenRouterProvider {
|
|
|
109
109
|
}
|
|
110
110
|
return body;
|
|
111
111
|
}
|
|
112
|
-
async chat(params) {
|
|
113
|
-
const body = this.buildRequestBody(params, false);
|
|
114
|
-
const response = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
115
|
-
method: 'POST',
|
|
116
|
-
headers: this.getHeaders(),
|
|
117
|
-
body: JSON.stringify(body),
|
|
118
|
-
signal: AbortSignal.timeout(this.config.timeout ?? 120_000),
|
|
119
|
-
});
|
|
120
|
-
if (!response.ok) {
|
|
121
|
-
const errorBody = await response.text();
|
|
122
|
-
throw new Error(`OpenRouter API error (${response.status}): ${errorBody}`);
|
|
123
|
-
}
|
|
124
|
-
const data = (await response.json());
|
|
125
|
-
const choice = data.choices[0];
|
|
126
|
-
if (!choice) {
|
|
127
|
-
throw new Error('OpenRouter returned empty choices');
|
|
128
|
-
}
|
|
129
|
-
const usage = parseUsage(data.usage);
|
|
130
|
-
const result = {
|
|
131
|
-
id: data.id,
|
|
132
|
-
model: data.model,
|
|
133
|
-
message: {
|
|
134
|
-
role: 'assistant',
|
|
135
|
-
content: choice.message.content,
|
|
136
|
-
toolCalls: choice.message.tool_calls?.map((tc) => ({
|
|
137
|
-
id: tc.id,
|
|
138
|
-
type: 'function',
|
|
139
|
-
function: tc.function,
|
|
140
|
-
})),
|
|
141
|
-
},
|
|
142
|
-
finishReason: choice.finish_reason,
|
|
143
|
-
usage,
|
|
144
|
-
};
|
|
145
|
-
return result;
|
|
146
|
-
}
|
|
147
112
|
async *chatStream(params) {
|
|
148
113
|
const body = this.buildRequestBody(params, true);
|
|
149
114
|
const response = await fetch(`${this.baseUrl}/chat/completions`, {
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAUA,MAAM,mBAAmB,GAAG,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,8BAA8B,CAAA;AAc7F,SAAS,UAAU,CAAC,GAAc;IACjC,IAAI,CAAC,GAAG,EAAE,CAAC;QACV,OAAO;YACN,YAAY,EAAE,CAAC;YACf,gBAAgB,EAAE,CAAC;YACnB,WAAW,EAAE,CAAC;YACd,YAAY,EAAE,CAAC;YACf,gBAAgB,EAAE,CAAC;SACnB,CAAA;IACF,CAAC;IACD,OAAO;QACN,YAAY,EAAE,GAAG,CAAC,aA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B,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,KAAK,CAAC,WAAW;QAChB,IAAI,CAAC;YACJ,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,SAAS,EAAE;gBACtD,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE;gBAC1B,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC;aACjC,CAAC,CAAA;YACF,OAAO,QAAQ,CAAC,EAAE,CAAA;QACnB,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,KAAK,CAAA;QACb,CAAC;IACF,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@namzu/openrouter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "OpenRouter LLM provider for @namzu/sdk. Access to 200+ models (Claude, GPT-4, Llama, Mixtral) via OpenRouter's unified API.",
|
|
5
5
|
"license": "FSL-1.1-MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,14 +35,14 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@namzu/sdk": ">=
|
|
38
|
+
"@namzu/sdk": ">=1.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@biomejs/biome": "^1.9.4",
|
|
42
42
|
"@types/node": "^22.19.17",
|
|
43
43
|
"typescript": "^5.5.0",
|
|
44
44
|
"vitest": "^2.0.0",
|
|
45
|
-
"@namzu/sdk": "^0.
|
|
45
|
+
"@namzu/sdk": "^1.0.0"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsc --build",
|
package/src/client.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ChatCompletionParams,
|
|
3
|
-
ChatCompletionResponse,
|
|
4
3
|
LLMProvider,
|
|
5
4
|
ModelInfo,
|
|
6
5
|
StreamChunk,
|
|
@@ -137,65 +136,6 @@ export class OpenRouterProvider implements LLMProvider {
|
|
|
137
136
|
return body
|
|
138
137
|
}
|
|
139
138
|
|
|
140
|
-
async chat(params: ChatCompletionParams): Promise<ChatCompletionResponse> {
|
|
141
|
-
const body = this.buildRequestBody(params, false)
|
|
142
|
-
|
|
143
|
-
const response = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
144
|
-
method: 'POST',
|
|
145
|
-
headers: this.getHeaders(),
|
|
146
|
-
body: JSON.stringify(body),
|
|
147
|
-
signal: AbortSignal.timeout(this.config.timeout ?? 120_000),
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
if (!response.ok) {
|
|
151
|
-
const errorBody = await response.text()
|
|
152
|
-
throw new Error(`OpenRouter API error (${response.status}): ${errorBody}`)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const data = (await response.json()) as {
|
|
156
|
-
id: string
|
|
157
|
-
model: string
|
|
158
|
-
choices: Array<{
|
|
159
|
-
message: {
|
|
160
|
-
role: string
|
|
161
|
-
content: string | null
|
|
162
|
-
tool_calls?: Array<{
|
|
163
|
-
id: string
|
|
164
|
-
type: string
|
|
165
|
-
function: { name: string; arguments: string }
|
|
166
|
-
}>
|
|
167
|
-
}
|
|
168
|
-
finish_reason: string
|
|
169
|
-
}>
|
|
170
|
-
usage?: RawUsage
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const choice = data.choices[0]
|
|
174
|
-
if (!choice) {
|
|
175
|
-
throw new Error('OpenRouter returned empty choices')
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const usage = parseUsage(data.usage)
|
|
179
|
-
|
|
180
|
-
const result: ChatCompletionResponse = {
|
|
181
|
-
id: data.id,
|
|
182
|
-
model: data.model,
|
|
183
|
-
message: {
|
|
184
|
-
role: 'assistant',
|
|
185
|
-
content: choice.message.content,
|
|
186
|
-
toolCalls: choice.message.tool_calls?.map((tc) => ({
|
|
187
|
-
id: tc.id,
|
|
188
|
-
type: 'function' as const,
|
|
189
|
-
function: tc.function,
|
|
190
|
-
})),
|
|
191
|
-
},
|
|
192
|
-
finishReason: choice.finish_reason as ChatCompletionResponse['finishReason'],
|
|
193
|
-
usage,
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return result
|
|
197
|
-
}
|
|
198
|
-
|
|
199
139
|
async *chatStream(params: ChatCompletionParams): AsyncIterable<StreamChunk> {
|
|
200
140
|
const body = this.buildRequestBody(params, true)
|
|
201
141
|
|