@pivanov/claude-wire 0.0.3 → 0.1.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 +8 -4
- package/dist/async.d.ts +10 -0
- package/dist/async.js +27 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +21 -3
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +7 -0
- package/dist/cost.d.ts +8 -0
- package/dist/cost.js +32 -11
- package/dist/errors.d.ts +5 -2
- package/dist/errors.js +38 -6
- package/dist/index.d.ts +10 -6
- package/dist/index.js +10 -3
- package/dist/json.d.ts +35 -0
- package/dist/json.js +43 -0
- package/dist/parser/translator.js +7 -2
- package/dist/pipeline.d.ts +14 -4
- package/dist/pipeline.js +39 -19
- package/dist/process.d.ts +15 -3
- package/dist/process.js +86 -25
- package/dist/reader.d.ts +3 -0
- package/dist/reader.js +32 -30
- package/dist/runtime.d.ts +5 -4
- package/dist/runtime.js +8 -5
- package/dist/session.d.ts +31 -2
- package/dist/session.js +138 -71
- package/dist/stderr.d.ts +10 -0
- package/dist/stderr.js +31 -0
- package/dist/stream.js +61 -26
- package/dist/tools/handler.d.ts +1 -0
- package/dist/tools/registry.d.ts +4 -2
- package/dist/tools/registry.js +7 -4
- package/dist/types/events.d.ts +1 -1
- package/dist/types/options.d.ts +43 -6
- package/dist/types/protocol.d.ts +1 -5
- package/dist/types/results.d.ts +6 -6
- package/dist/validation.d.ts +10 -0
- package/dist/validation.js +23 -0
- package/dist/warnings.d.ts +2 -0
- package/dist/warnings.js +24 -0
- package/dist/writer.d.ts +10 -1
- package/dist/writer.js +14 -8
- package/package.json +1 -1
package/dist/types/protocol.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type TModelUsageEntry = {
|
|
|
3
3
|
outputTokens: number;
|
|
4
4
|
cacheReadInputTokens?: number;
|
|
5
5
|
cacheCreationInputTokens?: number;
|
|
6
|
-
contextWindow
|
|
6
|
+
contextWindow?: number;
|
|
7
7
|
};
|
|
8
8
|
export type TClaudeContentType = "text" | "thinking" | "tool_use" | "tool_result" | (string & {});
|
|
9
9
|
export type TClaudeContent = {
|
|
@@ -32,11 +32,7 @@ export type TClaudeEvent = {
|
|
|
32
32
|
model?: string;
|
|
33
33
|
tools?: string[];
|
|
34
34
|
duration_ms?: number;
|
|
35
|
-
duration_api_ms?: number;
|
|
36
|
-
cost_usd?: number;
|
|
37
35
|
total_cost_usd?: number;
|
|
38
36
|
is_error?: boolean;
|
|
39
|
-
num_turns?: number;
|
|
40
37
|
modelUsage?: Record<string, TModelUsageEntry>;
|
|
41
|
-
usage?: unknown;
|
|
42
38
|
};
|
package/dist/types/results.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type { TRelayEvent } from "./events.js";
|
|
2
|
+
export type TTokens = {
|
|
3
|
+
input: number;
|
|
4
|
+
output: number;
|
|
5
|
+
};
|
|
2
6
|
export type TCostSnapshot = {
|
|
3
7
|
totalUsd: number;
|
|
4
|
-
|
|
5
|
-
outputTokens: number;
|
|
8
|
+
tokens: TTokens;
|
|
6
9
|
};
|
|
7
10
|
export type TAskResult = {
|
|
8
11
|
text: string;
|
|
9
12
|
costUsd: number;
|
|
10
|
-
tokens:
|
|
11
|
-
input: number;
|
|
12
|
-
output: number;
|
|
13
|
-
};
|
|
13
|
+
tokens: TTokens;
|
|
14
14
|
duration: number;
|
|
15
15
|
sessionId?: string;
|
|
16
16
|
events: TRelayEvent[];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rejects non-finite values and negatives. Zero is intentionally allowed
|
|
3
|
+
* so callers can express "disallow any spend" (useful in tests).
|
|
4
|
+
*/
|
|
5
|
+
export declare const assertPositiveNumber: (value: number | undefined, name: string) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Rejects empty strings. Used on writer payloads where an empty value
|
|
8
|
+
* would produce a malformed JSON line on the CLI's stdin.
|
|
9
|
+
*/
|
|
10
|
+
export declare const requireNonEmpty: (value: string, name: string) => void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ClaudeError } from "./errors.js";
|
|
2
|
+
// Boundary validators -- throw ClaudeError with a stable message shape so
|
|
3
|
+
// callers (SDK tests, consumer apps) can pattern-match on field name.
|
|
4
|
+
// Both helpers are cheap; performance-sensitive paths should still prefer
|
|
5
|
+
// type-level guards, but runtime checks catch undeclared-undefined drift.
|
|
6
|
+
/**
|
|
7
|
+
* Rejects non-finite values and negatives. Zero is intentionally allowed
|
|
8
|
+
* so callers can express "disallow any spend" (useful in tests).
|
|
9
|
+
*/
|
|
10
|
+
export const assertPositiveNumber = (value, name) => {
|
|
11
|
+
if (value !== undefined && (!Number.isFinite(value) || value < 0)) {
|
|
12
|
+
throw new ClaudeError(`${name} must be a finite non-negative number`);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Rejects empty strings. Used on writer payloads where an empty value
|
|
17
|
+
* would produce a malformed JSON line on the CLI's stdin.
|
|
18
|
+
*/
|
|
19
|
+
export const requireNonEmpty = (value, name) => {
|
|
20
|
+
if (!value) {
|
|
21
|
+
throw new ClaudeError(`${name} must be a non-empty string`);
|
|
22
|
+
}
|
|
23
|
+
};
|
package/dist/warnings.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// One-line library-warning emitter. Consumers set `onWarning` on
|
|
2
|
+
// IClaudeOptions to route warnings anywhere; when unset we fall back to
|
|
3
|
+
// `console.warn` so behavior is unchanged for casual users.
|
|
4
|
+
const DEFAULT = (message, cause) => {
|
|
5
|
+
if (cause === undefined) {
|
|
6
|
+
console.warn(`[claude-wire] ${message}`);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
console.warn(`[claude-wire] ${message}`, cause);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
export const createWarn = (onWarning) => {
|
|
13
|
+
return onWarning
|
|
14
|
+
? (message, cause) => {
|
|
15
|
+
try {
|
|
16
|
+
onWarning(message, cause);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// A user hook that itself throws shouldn't take down the stream.
|
|
20
|
+
DEFAULT(message, cause);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
: DEFAULT;
|
|
24
|
+
};
|
package/dist/writer.d.ts
CHANGED
|
@@ -2,6 +2,15 @@ export declare const writer: {
|
|
|
2
2
|
user: (content: string) => string;
|
|
3
3
|
approve: (toolUseId: string) => string;
|
|
4
4
|
deny: (toolUseId: string) => string;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Send a tool result in response to a `tool_use` event. Pass
|
|
7
|
+
* `{ isError: true }` to mark the result as a tool-side error -- the model
|
|
8
|
+
* will see it as an error and can react (retry, apologize, pick another
|
|
9
|
+
* tool) rather than treating it as success. The protocol supports the
|
|
10
|
+
* flag natively; without it, results are assumed successful.
|
|
11
|
+
*/
|
|
12
|
+
toolResult: (toolUseId: string, content: string, options?: {
|
|
13
|
+
isError?: boolean;
|
|
14
|
+
}) => string;
|
|
6
15
|
abort: () => string;
|
|
7
16
|
};
|
package/dist/writer.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { requireNonEmpty } from "./validation.js";
|
|
2
2
|
const ABORT_LINE = `${JSON.stringify({ type: "abort" })}\n`;
|
|
3
|
-
const requireNonEmpty = (value, name) => {
|
|
4
|
-
if (!value) {
|
|
5
|
-
throw new ClaudeError(`${name} must be a non-empty string`);
|
|
6
|
-
}
|
|
7
|
-
};
|
|
8
3
|
export const writer = {
|
|
9
4
|
user: (content) => {
|
|
10
5
|
requireNonEmpty(content, "content");
|
|
@@ -18,9 +13,20 @@ export const writer = {
|
|
|
18
13
|
requireNonEmpty(toolUseId, "toolUseId");
|
|
19
14
|
return `${JSON.stringify({ type: "deny", tool_use_id: toolUseId })}\n`;
|
|
20
15
|
},
|
|
21
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Send a tool result in response to a `tool_use` event. Pass
|
|
18
|
+
* `{ isError: true }` to mark the result as a tool-side error -- the model
|
|
19
|
+
* will see it as an error and can react (retry, apologize, pick another
|
|
20
|
+
* tool) rather than treating it as success. The protocol supports the
|
|
21
|
+
* flag natively; without it, results are assumed successful.
|
|
22
|
+
*/
|
|
23
|
+
toolResult: (toolUseId, content, options) => {
|
|
22
24
|
requireNonEmpty(toolUseId, "toolUseId");
|
|
23
|
-
|
|
25
|
+
const payload = { type: "tool_result", tool_use_id: toolUseId, content };
|
|
26
|
+
if (options?.isError) {
|
|
27
|
+
payload.is_error = true;
|
|
28
|
+
}
|
|
29
|
+
return `${JSON.stringify(payload)}\n`;
|
|
24
30
|
},
|
|
25
31
|
abort: () => ABORT_LINE,
|
|
26
32
|
};
|