Skip to content

Commit 79cb710

Browse files
committed
Bump version to 0.2.21-fork in package.json. Refactor InternalClient and SubprocessCLITransport to streamline message handling by directly writing userMessage to stdin, improving code readability and maintainability. Enhance cost handling in InternalClient to simplify the structure of the cost object.
1 parent 1f27e07 commit 79cb710

3 files changed

Lines changed: 25 additions & 50 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@botanicastudios/claude-code-sdk-ts",
3-
"version": "0.2.20-fork",
3+
"version": "0.2.21-fork",
44
"description": "Unofficial TypeScript port of the official Python Claude Code SDK",
55
"type": "module",
66
"main": "dist/index.cjs",

src/_internal/client.ts

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,7 @@ export class InternalClient {
8686
throw new Error('No active transport for streaming input');
8787
}
8888

89-
const messagePreview =
90-
typeof userMessage.content === 'string'
91-
? userMessage.content.substring(0, 50) + '...'
92-
: '[content blocks]';
93-
94-
this.debugLog(
95-
'DEBUG: Sending streaming input to active transport:',
96-
messagePreview
97-
);
98-
99-
const jsonlMessage = {
100-
type: 'user',
101-
message: {
102-
role: 'user',
103-
content: userMessage.content
104-
}
105-
};
106-
107-
const jsonlString = JSON.stringify(jsonlMessage) + '\n';
108-
109-
this.debugLog(
110-
'DEBUG: Writing JSONL to stdin:',
111-
JSON.stringify(jsonlMessage).substring(0, 100) + '...'
112-
);
113-
114-
if (this.options.debug) {
115-
this.debugLog('DEBUG stdin (raw):', jsonlString);
116-
}
117-
118-
this.transport.writeToStdin(jsonlString);
89+
this.transport.writeToStdin(userMessage);
11990

12091
this.debugLog('DEBUG: Successfully wrote JSONL message to stdin');
12192
}
@@ -186,13 +157,15 @@ export class InternalClient {
186157
content: output.result || '',
187158
result: output.result || '',
188159
usage: output.usage,
189-
cost: output.cost ? {
190-
input_cost: output.cost.input_cost,
191-
output_cost: output.cost.output_cost,
192-
cache_creation_cost: output.cost.cache_creation_cost,
193-
cache_read_cost: output.cost.cache_read_cost,
194-
total_cost: output.cost.total_cost || output.total_cost_usd
195-
} : undefined,
160+
cost: output.cost
161+
? {
162+
input_cost: output.cost.input_cost,
163+
output_cost: output.cost.output_cost,
164+
cache_creation_cost: output.cost.cache_creation_cost,
165+
cache_read_cost: output.cost.cache_read_cost,
166+
total_cost: output.cost.total_cost || output.total_cost_usd
167+
}
168+
: undefined,
196169
session_id: output.session_id
197170
};
198171

src/_internal/transport/subprocess-cli.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
import type {
1515
ClaudeCodeOptions,
1616
CLIOutput,
17-
ProcessCompleteHandler
17+
ProcessCompleteHandler,
18+
UserMessage
1819
} from '../../types.js';
1920

2021
export class SubprocessCLITransport {
@@ -85,25 +86,26 @@ export class SubprocessCLITransport {
8586
/**
8687
* Write streaming input to the active process stdin
8788
*/
88-
writeToStdin(data: string): void {
89+
writeToStdin(userMessage: UserMessage): void {
8990
if (!this.isActive()) {
9091
throw new Error('No active process to write to');
9192
}
9293

9394
if (this.process && this.process.stdin) {
9495
try {
96+
const content = userMessage.content as string;
9597
if (this.streamingMode) {
9698
// For streaming mode, wrap message in JSONL format
9799
const jsonlMessage = {
98100
type: 'user',
99101
message: {
100102
role: 'user',
101-
content: [{ type: 'text', text: data }]
103+
content: [{ type: 'text', text: content }]
102104
}
103105
};
104106

105107
const jsonlString = JSON.stringify(jsonlMessage) + '\n';
106-
108+
107109
this.debugLog(
108110
'DEBUG: [Transport] Writing JSONL to process stdin:',
109111
JSON.stringify(jsonlMessage).substring(0, 150) + '...'
@@ -120,14 +122,14 @@ export class SubprocessCLITransport {
120122
// For non-streaming mode, write as-is (though this shouldn't happen)
121123
this.debugLog(
122124
'DEBUG: [Transport] Writing raw data to stdin:',
123-
data.substring(0, 100) + '...'
125+
content.substring(0, 100) + '...'
124126
);
125-
127+
126128
if (this.options.debug) {
127-
this.debugLog('DEBUG stdin (raw):', data);
129+
this.debugLog('DEBUG stdin (raw):', content);
128130
}
129-
130-
this.process.stdin.write(data);
131+
132+
this.process.stdin.write(content);
131133
}
132134
} catch (error) {
133135
throw new Error(`Failed to write to stdin: ${error}`);
@@ -393,7 +395,7 @@ export class SubprocessCLITransport {
393395
};
394396

395397
const initialJsonlString = JSON.stringify(jsonlMessage) + '\n';
396-
398+
397399
this.debugLog(
398400
'DEBUG: [Transport] Sending initial JSONL message in streaming mode',
399401
{
@@ -415,11 +417,11 @@ export class SubprocessCLITransport {
415417
} else {
416418
// For simple queries, send as plain text and close stdin
417419
const promptString = this.prompt + '\n';
418-
420+
419421
if (this.options.debug) {
420422
this.debugLog('DEBUG stdin (raw):', promptString);
421423
}
422-
424+
423425
this.process.stdin.write(promptString);
424426
this.process.stdin.end();
425427
}

0 commit comments

Comments
 (0)