@react-grab/amp 0.0.97 → 0.1.0-beta.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 +0 -1
- package/dist/cli.cjs +11498 -6179
- package/dist/cli.js +11494 -6178
- package/dist/client.cjs +383 -203
- package/dist/client.d.cts +5 -17
- package/dist/client.d.ts +5 -17
- package/dist/client.global.js +2 -5
- package/dist/client.js +383 -203
- package/dist/handler.cjs +11474 -0
- package/dist/handler.d.cts +7 -0
- package/dist/handler.d.ts +7 -0
- package/dist/handler.js +11463 -0
- package/package.json +10 -13
- package/dist/server.cjs +0 -235
- package/dist/server.d.cts +0 -11
- package/dist/server.d.ts +0 -11
- package/dist/server.js +0 -225
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-grab/amp",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"react-grab-amp": "./dist/cli.cjs"
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"import": "./dist/client.js",
|
|
12
12
|
"require": "./dist/client.cjs"
|
|
13
13
|
},
|
|
14
|
-
"./
|
|
15
|
-
"types": "./dist/
|
|
16
|
-
"import": "./dist/
|
|
17
|
-
"require": "./dist/
|
|
14
|
+
"./handler": {
|
|
15
|
+
"types": "./dist/handler.d.ts",
|
|
16
|
+
"import": "./dist/handler.js",
|
|
17
|
+
"require": "./dist/handler.cjs"
|
|
18
18
|
},
|
|
19
19
|
"./dist/*": "./dist/*.js",
|
|
20
20
|
"./dist/*.js": "./dist/*.js"
|
|
@@ -26,16 +26,13 @@
|
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^22.10.7",
|
|
28
28
|
"tsup": "^8.4.0",
|
|
29
|
-
"@react-grab/utils": "0.0.
|
|
29
|
+
"@react-grab/utils": "0.1.0-beta.0"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"
|
|
34
|
-
"@
|
|
35
|
-
"
|
|
36
|
-
"hono": "^4.0.0",
|
|
37
|
-
"picocolors": "^1.1.1",
|
|
38
|
-
"react-grab": "0.0.97"
|
|
32
|
+
"@sourcegraph/amp": "^0.0.1767830505-ga62310",
|
|
33
|
+
"@sourcegraph/amp-sdk": "^0.1.0-20251210081226-g90e3892",
|
|
34
|
+
"@react-grab/relay": "0.1.0-beta.0",
|
|
35
|
+
"react-grab": "0.1.0-beta.0"
|
|
39
36
|
},
|
|
40
37
|
"scripts": {
|
|
41
38
|
"dev": "tsup --watch",
|
package/dist/server.cjs
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var ampSdk = require('@sourcegraph/amp-sdk');
|
|
4
|
-
var fkill = require('fkill');
|
|
5
|
-
var hono = require('hono');
|
|
6
|
-
var cors = require('hono/cors');
|
|
7
|
-
var streaming = require('hono/streaming');
|
|
8
|
-
var nodeServer = require('@hono/node-server');
|
|
9
|
-
var pc = require('picocolors');
|
|
10
|
-
var url = require('url');
|
|
11
|
-
|
|
12
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
13
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
-
|
|
15
|
-
var fkill__default = /*#__PURE__*/_interopDefault(fkill);
|
|
16
|
-
var pc__default = /*#__PURE__*/_interopDefault(pc);
|
|
17
|
-
|
|
18
|
-
// src/server.ts
|
|
19
|
-
|
|
20
|
-
// src/constants.ts
|
|
21
|
-
var DEFAULT_PORT = 9567;
|
|
22
|
-
var COMPLETED_STATUS = "Completed successfully";
|
|
23
|
-
|
|
24
|
-
// ../utils/dist/server.js
|
|
25
|
-
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
26
|
-
var VERSION = "0.0.97";
|
|
27
|
-
try {
|
|
28
|
-
fetch(
|
|
29
|
-
`https://www.react-grab.com/api/version?source=amp&t=${Date.now()}`
|
|
30
|
-
).catch(() => {
|
|
31
|
-
});
|
|
32
|
-
} catch {
|
|
33
|
-
}
|
|
34
|
-
var threadMap = /* @__PURE__ */ new Map();
|
|
35
|
-
var abortControllers = /* @__PURE__ */ new Map();
|
|
36
|
-
var lastThreadId;
|
|
37
|
-
var extractTextFromContent = (content) => {
|
|
38
|
-
return content.filter((item) => item.type === "text" && item.text).map((item) => item.text).join(" ").trim();
|
|
39
|
-
};
|
|
40
|
-
var runAgent = async function* (prompt, options) {
|
|
41
|
-
const sessionId = options?.sessionId;
|
|
42
|
-
const abortController = new AbortController();
|
|
43
|
-
if (sessionId) {
|
|
44
|
-
abortControllers.set(sessionId, abortController);
|
|
45
|
-
}
|
|
46
|
-
const isAborted = () => {
|
|
47
|
-
if (options?.signal?.aborted) return true;
|
|
48
|
-
if (abortController.signal.aborted) return true;
|
|
49
|
-
return false;
|
|
50
|
-
};
|
|
51
|
-
try {
|
|
52
|
-
yield { type: "status", content: "Thinking\u2026" };
|
|
53
|
-
const executeOptions = {
|
|
54
|
-
dangerouslyAllowAll: true
|
|
55
|
-
};
|
|
56
|
-
executeOptions.cwd = options?.cwd ?? process.env.REACT_GRAB_CWD ?? process.cwd();
|
|
57
|
-
const existingThread = sessionId ? threadMap.get(sessionId) : void 0;
|
|
58
|
-
if (existingThread) {
|
|
59
|
-
executeOptions.continue = existingThread.threadId;
|
|
60
|
-
}
|
|
61
|
-
let capturedThreadId;
|
|
62
|
-
for await (const message of ampSdk.execute({
|
|
63
|
-
prompt,
|
|
64
|
-
options: executeOptions,
|
|
65
|
-
signal: abortController.signal
|
|
66
|
-
})) {
|
|
67
|
-
if (isAborted()) break;
|
|
68
|
-
switch (message.type) {
|
|
69
|
-
case "system":
|
|
70
|
-
if (message.subtype === "init") {
|
|
71
|
-
const systemMessage = message;
|
|
72
|
-
if (systemMessage.thread_id) {
|
|
73
|
-
capturedThreadId = systemMessage.thread_id;
|
|
74
|
-
}
|
|
75
|
-
yield { type: "status", content: "Session started..." };
|
|
76
|
-
}
|
|
77
|
-
break;
|
|
78
|
-
case "assistant": {
|
|
79
|
-
const messageContent = message.message?.content;
|
|
80
|
-
if (messageContent && Array.isArray(messageContent)) {
|
|
81
|
-
const toolUse = messageContent.find(
|
|
82
|
-
(item) => item.type === "tool_use"
|
|
83
|
-
);
|
|
84
|
-
if (toolUse && "name" in toolUse) {
|
|
85
|
-
yield { type: "status", content: `Using ${toolUse.name}...` };
|
|
86
|
-
} else {
|
|
87
|
-
const textContent = extractTextFromContent(messageContent);
|
|
88
|
-
if (textContent && !isAborted()) {
|
|
89
|
-
yield { type: "status", content: textContent };
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
case "result":
|
|
96
|
-
if (message.is_error) {
|
|
97
|
-
yield { type: "error", content: message.error || "Unknown error" };
|
|
98
|
-
} else {
|
|
99
|
-
yield { type: "status", content: COMPLETED_STATUS };
|
|
100
|
-
}
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
if (sessionId && capturedThreadId && !isAborted()) {
|
|
105
|
-
threadMap.set(sessionId, { threadId: capturedThreadId });
|
|
106
|
-
}
|
|
107
|
-
if (capturedThreadId) {
|
|
108
|
-
lastThreadId = capturedThreadId;
|
|
109
|
-
}
|
|
110
|
-
if (!isAborted()) {
|
|
111
|
-
yield { type: "done", content: "" };
|
|
112
|
-
}
|
|
113
|
-
} catch (error) {
|
|
114
|
-
if (!isAborted()) {
|
|
115
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
116
|
-
yield { type: "error", content: errorMessage };
|
|
117
|
-
yield { type: "done", content: "" };
|
|
118
|
-
}
|
|
119
|
-
} finally {
|
|
120
|
-
if (sessionId) {
|
|
121
|
-
abortControllers.delete(sessionId);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
var createServer = () => {
|
|
126
|
-
const honoApplication = new hono.Hono();
|
|
127
|
-
honoApplication.use("*", cors.cors());
|
|
128
|
-
honoApplication.post("/agent", async (context) => {
|
|
129
|
-
const requestBody = await context.req.json();
|
|
130
|
-
const { content, prompt, options, sessionId } = requestBody;
|
|
131
|
-
const existingThread = sessionId ? threadMap.get(sessionId) : void 0;
|
|
132
|
-
const isFollowUp = Boolean(existingThread);
|
|
133
|
-
const contentItems = Array.isArray(content) ? content : [content];
|
|
134
|
-
return streaming.streamSSE(context, async (stream) => {
|
|
135
|
-
if (isFollowUp) {
|
|
136
|
-
for await (const message of runAgent(prompt, {
|
|
137
|
-
...options,
|
|
138
|
-
sessionId
|
|
139
|
-
})) {
|
|
140
|
-
if (message.type === "error") {
|
|
141
|
-
await stream.writeSSE({
|
|
142
|
-
data: `Error: ${message.content}`,
|
|
143
|
-
event: "error"
|
|
144
|
-
});
|
|
145
|
-
} else {
|
|
146
|
-
await stream.writeSSE({
|
|
147
|
-
data: message.content,
|
|
148
|
-
event: message.type
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
for (let i = 0; i < contentItems.length; i++) {
|
|
155
|
-
const elementContent = contentItems[i];
|
|
156
|
-
const userPrompt = `${prompt}
|
|
157
|
-
|
|
158
|
-
${elementContent}`;
|
|
159
|
-
if (contentItems.length > 1) {
|
|
160
|
-
await stream.writeSSE({
|
|
161
|
-
data: `Processing element ${i + 1} of ${contentItems.length}...`,
|
|
162
|
-
event: "status"
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
for await (const message of runAgent(userPrompt, {
|
|
166
|
-
...options,
|
|
167
|
-
sessionId
|
|
168
|
-
})) {
|
|
169
|
-
if (message.type === "error") {
|
|
170
|
-
await stream.writeSSE({
|
|
171
|
-
data: `Error: ${message.content}`,
|
|
172
|
-
event: "error"
|
|
173
|
-
});
|
|
174
|
-
} else {
|
|
175
|
-
await stream.writeSSE({
|
|
176
|
-
data: message.content,
|
|
177
|
-
event: message.type
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
honoApplication.post("/abort/:sessionId", (context) => {
|
|
185
|
-
const { sessionId } = context.req.param();
|
|
186
|
-
const abortController = abortControllers.get(sessionId);
|
|
187
|
-
if (abortController) {
|
|
188
|
-
abortController.abort();
|
|
189
|
-
abortControllers.delete(sessionId);
|
|
190
|
-
}
|
|
191
|
-
return context.json({ status: "ok" });
|
|
192
|
-
});
|
|
193
|
-
honoApplication.post("/undo", async (context) => {
|
|
194
|
-
if (!lastThreadId) {
|
|
195
|
-
return context.json({ status: "error", message: "No session to undo" });
|
|
196
|
-
}
|
|
197
|
-
try {
|
|
198
|
-
for await (const _message of ampSdk.execute({
|
|
199
|
-
prompt: "undo",
|
|
200
|
-
options: {
|
|
201
|
-
dangerouslyAllowAll: true,
|
|
202
|
-
cwd: process.env.REACT_GRAB_CWD ?? process.cwd(),
|
|
203
|
-
continue: lastThreadId
|
|
204
|
-
}
|
|
205
|
-
})) {
|
|
206
|
-
}
|
|
207
|
-
return context.json({ status: "ok" });
|
|
208
|
-
} catch (error) {
|
|
209
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
210
|
-
return context.json({ status: "error", message: errorMessage });
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
honoApplication.get("/health", (context) => {
|
|
214
|
-
return context.json({ status: "ok", provider: "amp" });
|
|
215
|
-
});
|
|
216
|
-
return honoApplication;
|
|
217
|
-
};
|
|
218
|
-
var startServer = async (port = DEFAULT_PORT) => {
|
|
219
|
-
await fkill__default.default(`:${port}`, { force: true, silent: true }).catch(() => {
|
|
220
|
-
});
|
|
221
|
-
await sleep(100);
|
|
222
|
-
const honoApplication = createServer();
|
|
223
|
-
nodeServer.serve({ fetch: honoApplication.fetch, port });
|
|
224
|
-
console.log(
|
|
225
|
-
`${pc__default.default.magenta("\u273F")} ${pc__default.default.bold("React Grab")} ${pc__default.default.gray(VERSION)} ${pc__default.default.dim("(Amp)")}`
|
|
226
|
-
);
|
|
227
|
-
console.log(`- Local: ${pc__default.default.cyan(`http://localhost:${port}`)}`);
|
|
228
|
-
};
|
|
229
|
-
if ((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('server.cjs', document.baseURI).href)) === url.pathToFileURL(process.argv[1]).href) {
|
|
230
|
-
startServer(DEFAULT_PORT).catch(console.error);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
exports.createServer = createServer;
|
|
234
|
-
exports.runAgent = runAgent;
|
|
235
|
-
exports.startServer = startServer;
|
package/dist/server.d.cts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as hono_types from 'hono/types';
|
|
2
|
-
import { Hono } from 'hono';
|
|
3
|
-
import { AgentCoreOptions, AgentMessage } from '@react-grab/utils/server';
|
|
4
|
-
|
|
5
|
-
interface AmpAgentOptions extends AgentCoreOptions {
|
|
6
|
-
}
|
|
7
|
-
declare const runAgent: (prompt: string, options?: AmpAgentOptions) => AsyncGenerator<AgentMessage>;
|
|
8
|
-
declare const createServer: () => Hono<hono_types.BlankEnv, hono_types.BlankSchema, "/">;
|
|
9
|
-
declare const startServer: (port?: number) => Promise<void>;
|
|
10
|
-
|
|
11
|
-
export { type AmpAgentOptions, createServer, runAgent, startServer };
|
package/dist/server.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as hono_types from 'hono/types';
|
|
2
|
-
import { Hono } from 'hono';
|
|
3
|
-
import { AgentCoreOptions, AgentMessage } from '@react-grab/utils/server';
|
|
4
|
-
|
|
5
|
-
interface AmpAgentOptions extends AgentCoreOptions {
|
|
6
|
-
}
|
|
7
|
-
declare const runAgent: (prompt: string, options?: AmpAgentOptions) => AsyncGenerator<AgentMessage>;
|
|
8
|
-
declare const createServer: () => Hono<hono_types.BlankEnv, hono_types.BlankSchema, "/">;
|
|
9
|
-
declare const startServer: (port?: number) => Promise<void>;
|
|
10
|
-
|
|
11
|
-
export { type AmpAgentOptions, createServer, runAgent, startServer };
|
package/dist/server.js
DELETED
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
import { execute } from '@sourcegraph/amp-sdk';
|
|
2
|
-
import fkill from 'fkill';
|
|
3
|
-
import { Hono } from 'hono';
|
|
4
|
-
import { cors } from 'hono/cors';
|
|
5
|
-
import { streamSSE } from 'hono/streaming';
|
|
6
|
-
import { serve } from '@hono/node-server';
|
|
7
|
-
import pc from 'picocolors';
|
|
8
|
-
import { pathToFileURL } from 'url';
|
|
9
|
-
|
|
10
|
-
// src/server.ts
|
|
11
|
-
|
|
12
|
-
// src/constants.ts
|
|
13
|
-
var DEFAULT_PORT = 9567;
|
|
14
|
-
var COMPLETED_STATUS = "Completed successfully";
|
|
15
|
-
|
|
16
|
-
// ../utils/dist/server.js
|
|
17
|
-
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
18
|
-
var VERSION = "0.0.97";
|
|
19
|
-
try {
|
|
20
|
-
fetch(
|
|
21
|
-
`https://www.react-grab.com/api/version?source=amp&t=${Date.now()}`
|
|
22
|
-
).catch(() => {
|
|
23
|
-
});
|
|
24
|
-
} catch {
|
|
25
|
-
}
|
|
26
|
-
var threadMap = /* @__PURE__ */ new Map();
|
|
27
|
-
var abortControllers = /* @__PURE__ */ new Map();
|
|
28
|
-
var lastThreadId;
|
|
29
|
-
var extractTextFromContent = (content) => {
|
|
30
|
-
return content.filter((item) => item.type === "text" && item.text).map((item) => item.text).join(" ").trim();
|
|
31
|
-
};
|
|
32
|
-
var runAgent = async function* (prompt, options) {
|
|
33
|
-
const sessionId = options?.sessionId;
|
|
34
|
-
const abortController = new AbortController();
|
|
35
|
-
if (sessionId) {
|
|
36
|
-
abortControllers.set(sessionId, abortController);
|
|
37
|
-
}
|
|
38
|
-
const isAborted = () => {
|
|
39
|
-
if (options?.signal?.aborted) return true;
|
|
40
|
-
if (abortController.signal.aborted) return true;
|
|
41
|
-
return false;
|
|
42
|
-
};
|
|
43
|
-
try {
|
|
44
|
-
yield { type: "status", content: "Thinking\u2026" };
|
|
45
|
-
const executeOptions = {
|
|
46
|
-
dangerouslyAllowAll: true
|
|
47
|
-
};
|
|
48
|
-
executeOptions.cwd = options?.cwd ?? process.env.REACT_GRAB_CWD ?? process.cwd();
|
|
49
|
-
const existingThread = sessionId ? threadMap.get(sessionId) : void 0;
|
|
50
|
-
if (existingThread) {
|
|
51
|
-
executeOptions.continue = existingThread.threadId;
|
|
52
|
-
}
|
|
53
|
-
let capturedThreadId;
|
|
54
|
-
for await (const message of execute({
|
|
55
|
-
prompt,
|
|
56
|
-
options: executeOptions,
|
|
57
|
-
signal: abortController.signal
|
|
58
|
-
})) {
|
|
59
|
-
if (isAborted()) break;
|
|
60
|
-
switch (message.type) {
|
|
61
|
-
case "system":
|
|
62
|
-
if (message.subtype === "init") {
|
|
63
|
-
const systemMessage = message;
|
|
64
|
-
if (systemMessage.thread_id) {
|
|
65
|
-
capturedThreadId = systemMessage.thread_id;
|
|
66
|
-
}
|
|
67
|
-
yield { type: "status", content: "Session started..." };
|
|
68
|
-
}
|
|
69
|
-
break;
|
|
70
|
-
case "assistant": {
|
|
71
|
-
const messageContent = message.message?.content;
|
|
72
|
-
if (messageContent && Array.isArray(messageContent)) {
|
|
73
|
-
const toolUse = messageContent.find(
|
|
74
|
-
(item) => item.type === "tool_use"
|
|
75
|
-
);
|
|
76
|
-
if (toolUse && "name" in toolUse) {
|
|
77
|
-
yield { type: "status", content: `Using ${toolUse.name}...` };
|
|
78
|
-
} else {
|
|
79
|
-
const textContent = extractTextFromContent(messageContent);
|
|
80
|
-
if (textContent && !isAborted()) {
|
|
81
|
-
yield { type: "status", content: textContent };
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case "result":
|
|
88
|
-
if (message.is_error) {
|
|
89
|
-
yield { type: "error", content: message.error || "Unknown error" };
|
|
90
|
-
} else {
|
|
91
|
-
yield { type: "status", content: COMPLETED_STATUS };
|
|
92
|
-
}
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (sessionId && capturedThreadId && !isAborted()) {
|
|
97
|
-
threadMap.set(sessionId, { threadId: capturedThreadId });
|
|
98
|
-
}
|
|
99
|
-
if (capturedThreadId) {
|
|
100
|
-
lastThreadId = capturedThreadId;
|
|
101
|
-
}
|
|
102
|
-
if (!isAborted()) {
|
|
103
|
-
yield { type: "done", content: "" };
|
|
104
|
-
}
|
|
105
|
-
} catch (error) {
|
|
106
|
-
if (!isAborted()) {
|
|
107
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
108
|
-
yield { type: "error", content: errorMessage };
|
|
109
|
-
yield { type: "done", content: "" };
|
|
110
|
-
}
|
|
111
|
-
} finally {
|
|
112
|
-
if (sessionId) {
|
|
113
|
-
abortControllers.delete(sessionId);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
var createServer = () => {
|
|
118
|
-
const honoApplication = new Hono();
|
|
119
|
-
honoApplication.use("*", cors());
|
|
120
|
-
honoApplication.post("/agent", async (context) => {
|
|
121
|
-
const requestBody = await context.req.json();
|
|
122
|
-
const { content, prompt, options, sessionId } = requestBody;
|
|
123
|
-
const existingThread = sessionId ? threadMap.get(sessionId) : void 0;
|
|
124
|
-
const isFollowUp = Boolean(existingThread);
|
|
125
|
-
const contentItems = Array.isArray(content) ? content : [content];
|
|
126
|
-
return streamSSE(context, async (stream) => {
|
|
127
|
-
if (isFollowUp) {
|
|
128
|
-
for await (const message of runAgent(prompt, {
|
|
129
|
-
...options,
|
|
130
|
-
sessionId
|
|
131
|
-
})) {
|
|
132
|
-
if (message.type === "error") {
|
|
133
|
-
await stream.writeSSE({
|
|
134
|
-
data: `Error: ${message.content}`,
|
|
135
|
-
event: "error"
|
|
136
|
-
});
|
|
137
|
-
} else {
|
|
138
|
-
await stream.writeSSE({
|
|
139
|
-
data: message.content,
|
|
140
|
-
event: message.type
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
for (let i = 0; i < contentItems.length; i++) {
|
|
147
|
-
const elementContent = contentItems[i];
|
|
148
|
-
const userPrompt = `${prompt}
|
|
149
|
-
|
|
150
|
-
${elementContent}`;
|
|
151
|
-
if (contentItems.length > 1) {
|
|
152
|
-
await stream.writeSSE({
|
|
153
|
-
data: `Processing element ${i + 1} of ${contentItems.length}...`,
|
|
154
|
-
event: "status"
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
for await (const message of runAgent(userPrompt, {
|
|
158
|
-
...options,
|
|
159
|
-
sessionId
|
|
160
|
-
})) {
|
|
161
|
-
if (message.type === "error") {
|
|
162
|
-
await stream.writeSSE({
|
|
163
|
-
data: `Error: ${message.content}`,
|
|
164
|
-
event: "error"
|
|
165
|
-
});
|
|
166
|
-
} else {
|
|
167
|
-
await stream.writeSSE({
|
|
168
|
-
data: message.content,
|
|
169
|
-
event: message.type
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
honoApplication.post("/abort/:sessionId", (context) => {
|
|
177
|
-
const { sessionId } = context.req.param();
|
|
178
|
-
const abortController = abortControllers.get(sessionId);
|
|
179
|
-
if (abortController) {
|
|
180
|
-
abortController.abort();
|
|
181
|
-
abortControllers.delete(sessionId);
|
|
182
|
-
}
|
|
183
|
-
return context.json({ status: "ok" });
|
|
184
|
-
});
|
|
185
|
-
honoApplication.post("/undo", async (context) => {
|
|
186
|
-
if (!lastThreadId) {
|
|
187
|
-
return context.json({ status: "error", message: "No session to undo" });
|
|
188
|
-
}
|
|
189
|
-
try {
|
|
190
|
-
for await (const _message of execute({
|
|
191
|
-
prompt: "undo",
|
|
192
|
-
options: {
|
|
193
|
-
dangerouslyAllowAll: true,
|
|
194
|
-
cwd: process.env.REACT_GRAB_CWD ?? process.cwd(),
|
|
195
|
-
continue: lastThreadId
|
|
196
|
-
}
|
|
197
|
-
})) {
|
|
198
|
-
}
|
|
199
|
-
return context.json({ status: "ok" });
|
|
200
|
-
} catch (error) {
|
|
201
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
202
|
-
return context.json({ status: "error", message: errorMessage });
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
honoApplication.get("/health", (context) => {
|
|
206
|
-
return context.json({ status: "ok", provider: "amp" });
|
|
207
|
-
});
|
|
208
|
-
return honoApplication;
|
|
209
|
-
};
|
|
210
|
-
var startServer = async (port = DEFAULT_PORT) => {
|
|
211
|
-
await fkill(`:${port}`, { force: true, silent: true }).catch(() => {
|
|
212
|
-
});
|
|
213
|
-
await sleep(100);
|
|
214
|
-
const honoApplication = createServer();
|
|
215
|
-
serve({ fetch: honoApplication.fetch, port });
|
|
216
|
-
console.log(
|
|
217
|
-
`${pc.magenta("\u273F")} ${pc.bold("React Grab")} ${pc.gray(VERSION)} ${pc.dim("(Amp)")}`
|
|
218
|
-
);
|
|
219
|
-
console.log(`- Local: ${pc.cyan(`http://localhost:${port}`)}`);
|
|
220
|
-
};
|
|
221
|
-
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
222
|
-
startServer(DEFAULT_PORT).catch(console.error);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export { createServer, runAgent, startServer };
|