@react-grab/claude-code 0.0.55
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/LICENSE +21 -0
- package/dist/client.cjs +98 -0
- package/dist/client.d.cts +10 -0
- package/dist/client.d.ts +10 -0
- package/dist/client.js +96 -0
- package/dist/server.cjs +14654 -0
- package/dist/server.d.cts +6 -0
- package/dist/server.d.ts +6 -0
- package/dist/server.js +14629 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Aiden Bai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/constants.ts
|
|
4
|
+
var DEFAULT_PORT = 4567;
|
|
5
|
+
|
|
6
|
+
// src/client.ts
|
|
7
|
+
var DEFAULT_SERVER_URL = `http://localhost:${DEFAULT_PORT}`;
|
|
8
|
+
var STORAGE_KEY = "react-grab:agent-sessions";
|
|
9
|
+
var DEFAULT_OPTIONS = {
|
|
10
|
+
systemPrompt: {
|
|
11
|
+
type: "preset",
|
|
12
|
+
preset: "claude_code",
|
|
13
|
+
append: `You are helping a user make changes to a React component based on a selected element.
|
|
14
|
+
The user has selected an element from their UI and wants you to help modify it.
|
|
15
|
+
Provide clear, concise status updates as you work.`
|
|
16
|
+
},
|
|
17
|
+
model: "haiku",
|
|
18
|
+
permissionMode: "bypassPermissions",
|
|
19
|
+
maxTurns: 10
|
|
20
|
+
};
|
|
21
|
+
var parseSSEEvent = (eventBlock) => {
|
|
22
|
+
let eventType = "";
|
|
23
|
+
let data = "";
|
|
24
|
+
for (const line of eventBlock.split("\n")) {
|
|
25
|
+
if (line.startsWith("event:")) eventType = line.slice(6).trim();
|
|
26
|
+
else if (line.startsWith("data:")) data = line.slice(5).trim();
|
|
27
|
+
}
|
|
28
|
+
return { eventType, data };
|
|
29
|
+
};
|
|
30
|
+
async function* streamSSE(stream) {
|
|
31
|
+
const reader = stream.getReader();
|
|
32
|
+
const decoder = new TextDecoder();
|
|
33
|
+
let buffer = "";
|
|
34
|
+
try {
|
|
35
|
+
while (true) {
|
|
36
|
+
const { done, value } = await reader.read();
|
|
37
|
+
if (value) buffer += decoder.decode(value, { stream: true });
|
|
38
|
+
let boundary;
|
|
39
|
+
while ((boundary = buffer.indexOf("\n\n")) !== -1) {
|
|
40
|
+
const { eventType, data } = parseSSEEvent(buffer.slice(0, boundary));
|
|
41
|
+
buffer = buffer.slice(boundary + 2);
|
|
42
|
+
if (eventType === "done") return;
|
|
43
|
+
if (eventType === "error") throw new Error(data || "Agent error");
|
|
44
|
+
if (data) yield data;
|
|
45
|
+
}
|
|
46
|
+
if (done) break;
|
|
47
|
+
}
|
|
48
|
+
} finally {
|
|
49
|
+
reader.releaseLock();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function* streamFromServer(serverUrl, context, signal) {
|
|
53
|
+
const response = await fetch(`${serverUrl}/agent`, {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: { "Content-Type": "application/json" },
|
|
56
|
+
body: JSON.stringify(context),
|
|
57
|
+
signal
|
|
58
|
+
});
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
throw new Error(`Server error: ${response.status}`);
|
|
61
|
+
}
|
|
62
|
+
if (!response.body) {
|
|
63
|
+
throw new Error("No response body");
|
|
64
|
+
}
|
|
65
|
+
yield* streamSSE(response.body);
|
|
66
|
+
}
|
|
67
|
+
var createClaudeAgentProvider = (providerOptions = {}) => {
|
|
68
|
+
const { serverUrl = DEFAULT_SERVER_URL, getOptions } = providerOptions;
|
|
69
|
+
const mergeOptions = (contextOptions) => ({
|
|
70
|
+
...DEFAULT_OPTIONS,
|
|
71
|
+
...getOptions?.() ?? {},
|
|
72
|
+
...contextOptions ?? {}
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
send: async function* (context, signal) {
|
|
76
|
+
const mergedContext = { ...context, options: mergeOptions(context.options) };
|
|
77
|
+
yield* streamFromServer(serverUrl, mergedContext, signal);
|
|
78
|
+
},
|
|
79
|
+
resume: async function* (sessionId, signal) {
|
|
80
|
+
const savedSessions = sessionStorage.getItem(STORAGE_KEY);
|
|
81
|
+
if (!savedSessions) {
|
|
82
|
+
throw new Error("No sessions to resume");
|
|
83
|
+
}
|
|
84
|
+
const sessionsObject = JSON.parse(savedSessions);
|
|
85
|
+
const session = sessionsObject[sessionId];
|
|
86
|
+
if (!session) {
|
|
87
|
+
throw new Error(`Session ${sessionId} not found`);
|
|
88
|
+
}
|
|
89
|
+
const context = session.context;
|
|
90
|
+
const mergedContext = { ...context, options: mergeOptions(context.options) };
|
|
91
|
+
yield "Resuming...";
|
|
92
|
+
yield* streamFromServer(serverUrl, mergedContext, signal);
|
|
93
|
+
},
|
|
94
|
+
supportsResume: true
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
exports.createClaudeAgentProvider = createClaudeAgentProvider;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AgentProvider } from 'react-grab/core';
|
|
2
|
+
import { Options } from '@anthropic-ai/claude-agent-sdk';
|
|
3
|
+
|
|
4
|
+
interface ClaudeAgentProviderOptions {
|
|
5
|
+
serverUrl?: string;
|
|
6
|
+
getOptions?: () => Partial<Options>;
|
|
7
|
+
}
|
|
8
|
+
declare const createClaudeAgentProvider: (providerOptions?: ClaudeAgentProviderOptions) => AgentProvider<Options>;
|
|
9
|
+
|
|
10
|
+
export { createClaudeAgentProvider };
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AgentProvider } from 'react-grab/core';
|
|
2
|
+
import { Options } from '@anthropic-ai/claude-agent-sdk';
|
|
3
|
+
|
|
4
|
+
interface ClaudeAgentProviderOptions {
|
|
5
|
+
serverUrl?: string;
|
|
6
|
+
getOptions?: () => Partial<Options>;
|
|
7
|
+
}
|
|
8
|
+
declare const createClaudeAgentProvider: (providerOptions?: ClaudeAgentProviderOptions) => AgentProvider<Options>;
|
|
9
|
+
|
|
10
|
+
export { createClaudeAgentProvider };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var DEFAULT_PORT = 4567;
|
|
3
|
+
|
|
4
|
+
// src/client.ts
|
|
5
|
+
var DEFAULT_SERVER_URL = `http://localhost:${DEFAULT_PORT}`;
|
|
6
|
+
var STORAGE_KEY = "react-grab:agent-sessions";
|
|
7
|
+
var DEFAULT_OPTIONS = {
|
|
8
|
+
systemPrompt: {
|
|
9
|
+
type: "preset",
|
|
10
|
+
preset: "claude_code",
|
|
11
|
+
append: `You are helping a user make changes to a React component based on a selected element.
|
|
12
|
+
The user has selected an element from their UI and wants you to help modify it.
|
|
13
|
+
Provide clear, concise status updates as you work.`
|
|
14
|
+
},
|
|
15
|
+
model: "haiku",
|
|
16
|
+
permissionMode: "bypassPermissions",
|
|
17
|
+
maxTurns: 10
|
|
18
|
+
};
|
|
19
|
+
var parseSSEEvent = (eventBlock) => {
|
|
20
|
+
let eventType = "";
|
|
21
|
+
let data = "";
|
|
22
|
+
for (const line of eventBlock.split("\n")) {
|
|
23
|
+
if (line.startsWith("event:")) eventType = line.slice(6).trim();
|
|
24
|
+
else if (line.startsWith("data:")) data = line.slice(5).trim();
|
|
25
|
+
}
|
|
26
|
+
return { eventType, data };
|
|
27
|
+
};
|
|
28
|
+
async function* streamSSE(stream) {
|
|
29
|
+
const reader = stream.getReader();
|
|
30
|
+
const decoder = new TextDecoder();
|
|
31
|
+
let buffer = "";
|
|
32
|
+
try {
|
|
33
|
+
while (true) {
|
|
34
|
+
const { done, value } = await reader.read();
|
|
35
|
+
if (value) buffer += decoder.decode(value, { stream: true });
|
|
36
|
+
let boundary;
|
|
37
|
+
while ((boundary = buffer.indexOf("\n\n")) !== -1) {
|
|
38
|
+
const { eventType, data } = parseSSEEvent(buffer.slice(0, boundary));
|
|
39
|
+
buffer = buffer.slice(boundary + 2);
|
|
40
|
+
if (eventType === "done") return;
|
|
41
|
+
if (eventType === "error") throw new Error(data || "Agent error");
|
|
42
|
+
if (data) yield data;
|
|
43
|
+
}
|
|
44
|
+
if (done) break;
|
|
45
|
+
}
|
|
46
|
+
} finally {
|
|
47
|
+
reader.releaseLock();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async function* streamFromServer(serverUrl, context, signal) {
|
|
51
|
+
const response = await fetch(`${serverUrl}/agent`, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { "Content-Type": "application/json" },
|
|
54
|
+
body: JSON.stringify(context),
|
|
55
|
+
signal
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
throw new Error(`Server error: ${response.status}`);
|
|
59
|
+
}
|
|
60
|
+
if (!response.body) {
|
|
61
|
+
throw new Error("No response body");
|
|
62
|
+
}
|
|
63
|
+
yield* streamSSE(response.body);
|
|
64
|
+
}
|
|
65
|
+
var createClaudeAgentProvider = (providerOptions = {}) => {
|
|
66
|
+
const { serverUrl = DEFAULT_SERVER_URL, getOptions } = providerOptions;
|
|
67
|
+
const mergeOptions = (contextOptions) => ({
|
|
68
|
+
...DEFAULT_OPTIONS,
|
|
69
|
+
...getOptions?.() ?? {},
|
|
70
|
+
...contextOptions ?? {}
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
send: async function* (context, signal) {
|
|
74
|
+
const mergedContext = { ...context, options: mergeOptions(context.options) };
|
|
75
|
+
yield* streamFromServer(serverUrl, mergedContext, signal);
|
|
76
|
+
},
|
|
77
|
+
resume: async function* (sessionId, signal) {
|
|
78
|
+
const savedSessions = sessionStorage.getItem(STORAGE_KEY);
|
|
79
|
+
if (!savedSessions) {
|
|
80
|
+
throw new Error("No sessions to resume");
|
|
81
|
+
}
|
|
82
|
+
const sessionsObject = JSON.parse(savedSessions);
|
|
83
|
+
const session = sessionsObject[sessionId];
|
|
84
|
+
if (!session) {
|
|
85
|
+
throw new Error(`Session ${sessionId} not found`);
|
|
86
|
+
}
|
|
87
|
+
const context = session.context;
|
|
88
|
+
const mergedContext = { ...context, options: mergeOptions(context.options) };
|
|
89
|
+
yield "Resuming...";
|
|
90
|
+
yield* streamFromServer(serverUrl, mergedContext, signal);
|
|
91
|
+
},
|
|
92
|
+
supportsResume: true
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export { createClaudeAgentProvider };
|