@augmentcode/auggie-sdk 0.1.10 → 0.1.11
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/dist/auggie/sdk-acp-client.d.ts +7 -6
- package/dist/auggie/sdk-acp-client.js +299 -332
- package/dist/auggie/sdk-mcp-server.d.ts +5 -3
- package/dist/auggie/sdk-mcp-server.js +102 -112
- package/dist/context/direct-context.d.ts +19 -15
- package/dist/context/direct-context.js +556 -567
- package/dist/context/filesystem-context.d.ts +5 -3
- package/dist/context/filesystem-context.js +187 -209
- package/dist/context/internal/__mocks__/api-client.d.ts +17 -11
- package/dist/context/internal/__mocks__/api-client.js +104 -91
- package/dist/context/internal/api-client.d.ts +14 -11
- package/dist/context/internal/api-client.js +234 -239
- package/dist/context/internal/blob-name-calculator.d.ts +5 -4
- package/dist/context/internal/blob-name-calculator.js +41 -38
- package/dist/context/internal/chat-utils.d.ts +6 -3
- package/dist/context/internal/chat-utils.js +5 -18
- package/dist/context/internal/credentials.d.ts +5 -4
- package/dist/context/internal/credentials.js +24 -38
- package/dist/context/internal/retry-utils.d.ts +5 -4
- package/dist/context/internal/retry-utils.js +60 -114
- package/dist/context/internal/search-utils.d.ts +3 -2
- package/dist/context/internal/search-utils.js +8 -9
- package/dist/context/internal/session-reader.d.ts +4 -3
- package/dist/context/internal/session-reader.js +14 -22
- package/dist/context/types.d.ts +17 -12
- package/dist/context/types.js +0 -5
- package/dist/index.d.ts +8 -7
- package/dist/index.js +14 -9
- package/dist/version.d.ts +3 -2
- package/dist/version.js +24 -38
- package/package.json +3 -2
- package/dist/auggie/sdk-acp-client.d.ts.map +0 -1
- package/dist/auggie/sdk-acp-client.js.map +0 -1
- package/dist/auggie/sdk-mcp-server.d.ts.map +0 -1
- package/dist/auggie/sdk-mcp-server.js.map +0 -1
- package/dist/context/direct-context.d.ts.map +0 -1
- package/dist/context/direct-context.js.map +0 -1
- package/dist/context/filesystem-context.d.ts.map +0 -1
- package/dist/context/filesystem-context.js.map +0 -1
- package/dist/context/internal/__mocks__/api-client.d.ts.map +0 -1
- package/dist/context/internal/__mocks__/api-client.js.map +0 -1
- package/dist/context/internal/api-client.d.ts.map +0 -1
- package/dist/context/internal/api-client.js.map +0 -1
- package/dist/context/internal/blob-name-calculator.d.ts.map +0 -1
- package/dist/context/internal/blob-name-calculator.js.map +0 -1
- package/dist/context/internal/chat-utils.d.ts.map +0 -1
- package/dist/context/internal/chat-utils.js.map +0 -1
- package/dist/context/internal/credentials.d.ts.map +0 -1
- package/dist/context/internal/credentials.js.map +0 -1
- package/dist/context/internal/retry-utils.d.ts.map +0 -1
- package/dist/context/internal/retry-utils.js.map +0 -1
- package/dist/context/internal/search-utils.d.ts.map +0 -1
- package/dist/context/internal/search-utils.js.map +0 -1
- package/dist/context/internal/session-reader.d.ts.map +0 -1
- package/dist/context/internal/session-reader.js.map +0 -1
- package/dist/context/types.d.ts.map +0 -1
- package/dist/context/types.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/version.d.ts.map +0 -1
- package/dist/version.js.map +0 -1
|
@@ -1,355 +1,322 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { Readable, Writable } from "node:stream";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* 1. Launch Auggie in ACP mode
|
|
12
|
-
* 2. Establish a bidirectional communication channel
|
|
13
|
-
* 3. Initialize a session
|
|
14
|
-
* 4. Send prompts and receive responses
|
|
15
|
-
*/
|
|
4
|
+
import {
|
|
5
|
+
ClientSideConnection,
|
|
6
|
+
ndJsonStream,
|
|
7
|
+
PROTOCOL_VERSION
|
|
8
|
+
} from "@agentclientprotocol/sdk";
|
|
9
|
+
import { getSDKVersion } from "../version.js";
|
|
10
|
+
import { AuggieMCPServer } from "./sdk-mcp-server.js";
|
|
16
11
|
class Auggie {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
12
|
+
static PATH_SPLIT_REGEX = /\s+/;
|
|
13
|
+
connection = null;
|
|
14
|
+
sessionId = null;
|
|
15
|
+
auggiePath;
|
|
16
|
+
workspaceRoot = void 0;
|
|
17
|
+
auggieProcess = null;
|
|
18
|
+
sessionUpdateCallback = null;
|
|
19
|
+
debug = false;
|
|
20
|
+
mcpServer = null;
|
|
21
|
+
model;
|
|
22
|
+
allowIndexing;
|
|
23
|
+
apiKey;
|
|
24
|
+
apiUrl;
|
|
25
|
+
rules;
|
|
26
|
+
excludedTools;
|
|
27
|
+
constructor({
|
|
28
|
+
auggiePath = "auggie",
|
|
29
|
+
workspaceRoot,
|
|
30
|
+
model = "haiku4.5",
|
|
31
|
+
allowIndexing = true,
|
|
32
|
+
apiKey,
|
|
33
|
+
apiUrl = process.env.AUGMENT_API_URL || "https://api.augmentcode.com",
|
|
34
|
+
rules,
|
|
35
|
+
excludedTools
|
|
36
|
+
} = {}) {
|
|
37
|
+
this.auggiePath = auggiePath;
|
|
38
|
+
this.workspaceRoot = workspaceRoot;
|
|
39
|
+
this.model = model;
|
|
40
|
+
this.allowIndexing = allowIndexing;
|
|
41
|
+
this.apiKey = apiKey;
|
|
42
|
+
this.apiUrl = apiUrl;
|
|
43
|
+
this.rules = rules;
|
|
44
|
+
this.excludedTools = excludedTools;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create and initialize a new Auggie instance
|
|
48
|
+
* This static factory method automatically connects and creates a session
|
|
49
|
+
*/
|
|
50
|
+
static async create(options = {}) {
|
|
51
|
+
const instance = new Auggie(options);
|
|
52
|
+
await instance.connect();
|
|
53
|
+
const tools = options.tools || options.toolsMap;
|
|
54
|
+
await instance.createSession(tools);
|
|
55
|
+
return instance;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Connect to Auggie ACP server
|
|
59
|
+
*/
|
|
60
|
+
async connect() {
|
|
61
|
+
const args = ["--acp", "--log-level=debug"];
|
|
62
|
+
if (this.model) {
|
|
63
|
+
args.push("--model", this.model);
|
|
33
64
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
* This static factory method automatically connects and creates a session
|
|
37
|
-
*/
|
|
38
|
-
static async create(options = {}) {
|
|
39
|
-
const instance = new Auggie(options);
|
|
40
|
-
await instance.connect();
|
|
41
|
-
// Support both 'tools' and 'toolsMap' for backwards compatibility
|
|
42
|
-
const tools = options.tools || options.toolsMap;
|
|
43
|
-
await instance.createSession(tools);
|
|
44
|
-
return instance;
|
|
65
|
+
if (this.allowIndexing) {
|
|
66
|
+
args.push("--allow-indexing");
|
|
45
67
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
if (this.allowIndexing) {
|
|
55
|
-
args.push("--allow-indexing");
|
|
56
|
-
}
|
|
57
|
-
if (this.workspaceRoot) {
|
|
58
|
-
args.push("--workspace-root", this.workspaceRoot);
|
|
68
|
+
if (this.workspaceRoot) {
|
|
69
|
+
args.push("--workspace-root", this.workspaceRoot);
|
|
70
|
+
}
|
|
71
|
+
if (this.rules && this.rules.length > 0) {
|
|
72
|
+
for (const rulePath of this.rules) {
|
|
73
|
+
if (!existsSync(rulePath)) {
|
|
74
|
+
throw new Error(`Rule file does not exist: ${rulePath}`);
|
|
59
75
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
76
|
+
args.push("--rules", rulePath);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (this.excludedTools && this.excludedTools.length > 0) {
|
|
80
|
+
for (const tool of this.excludedTools) {
|
|
81
|
+
args.push("--remove-tool", tool);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const pathParts = this.auggiePath.trim().split(Auggie.PATH_SPLIT_REGEX);
|
|
85
|
+
if (pathParts.length === 0 || !pathParts[0]) {
|
|
86
|
+
throw new Error("Invalid auggiePath: cannot be empty");
|
|
87
|
+
}
|
|
88
|
+
const command = pathParts[0];
|
|
89
|
+
const commandArgs = [...pathParts.slice(1), ...args];
|
|
90
|
+
const env = { ...process.env };
|
|
91
|
+
if (this.apiKey) {
|
|
92
|
+
env.AUGMENT_API_TOKEN = this.apiKey;
|
|
93
|
+
}
|
|
94
|
+
if (this.apiUrl) {
|
|
95
|
+
env.AUGMENT_API_URL = this.apiUrl;
|
|
96
|
+
}
|
|
97
|
+
this.auggieProcess = spawn(command, commandArgs, {
|
|
98
|
+
stdio: ["pipe", "pipe", "inherit"],
|
|
99
|
+
env
|
|
100
|
+
});
|
|
101
|
+
this.auggieProcess.on("error", (error) => {
|
|
102
|
+
console.error("Failed to start Auggie process:", error);
|
|
103
|
+
throw error;
|
|
104
|
+
});
|
|
105
|
+
this.auggieProcess.on("exit", (code, signal) => {
|
|
106
|
+
if (this.debug)
|
|
107
|
+
console.log(
|
|
108
|
+
`Auggie process exited with code ${code} and signal ${signal}`
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
if (!(this.auggieProcess.stdout && this.auggieProcess.stdin)) {
|
|
112
|
+
throw new Error("Failed to create stdio pipes for Auggie process");
|
|
113
|
+
}
|
|
114
|
+
const input = Readable.toWeb(
|
|
115
|
+
this.auggieProcess.stdout
|
|
116
|
+
);
|
|
117
|
+
const output = Writable.toWeb(this.auggieProcess.stdin);
|
|
118
|
+
const stream = ndJsonStream(output, input);
|
|
119
|
+
this.connection = new ClientSideConnection(
|
|
120
|
+
(agent) => this.createClient(agent),
|
|
121
|
+
stream
|
|
122
|
+
);
|
|
123
|
+
if (this.debug) {
|
|
124
|
+
console.log("Connected to Auggie ACP server");
|
|
125
|
+
}
|
|
126
|
+
await this.initializeConnection();
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Create a Client implementation to handle agent requests
|
|
130
|
+
*/
|
|
131
|
+
createClient(_agent) {
|
|
132
|
+
return {
|
|
133
|
+
// Handle session updates from the agent
|
|
134
|
+
sessionUpdate: (params) => {
|
|
135
|
+
if (this.sessionUpdateCallback) {
|
|
136
|
+
this.sessionUpdateCallback(params);
|
|
68
137
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
138
|
+
return Promise.resolve();
|
|
139
|
+
},
|
|
140
|
+
// Handle permission requests from the agent
|
|
141
|
+
requestPermission: (params) => {
|
|
142
|
+
if (this.debug) console.log("Permission requested:", params);
|
|
143
|
+
if (params.options && params.options.length > 0) {
|
|
144
|
+
return Promise.resolve({
|
|
145
|
+
outcome: {
|
|
146
|
+
outcome: "selected",
|
|
147
|
+
optionId: params.options?.[0]?.name || ""
|
|
73
148
|
}
|
|
149
|
+
});
|
|
74
150
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
const command = pathParts[0];
|
|
81
|
-
const commandArgs = [...pathParts.slice(1), ...args];
|
|
82
|
-
// Build environment variables for the Auggie process
|
|
83
|
-
const env = { ...process.env };
|
|
84
|
-
if (this.apiKey) {
|
|
85
|
-
env.AUGMENT_API_TOKEN = this.apiKey;
|
|
86
|
-
}
|
|
87
|
-
if (this.apiUrl) {
|
|
88
|
-
env.AUGMENT_API_URL = this.apiUrl;
|
|
89
|
-
}
|
|
90
|
-
this.auggieProcess = spawn(command, commandArgs, {
|
|
91
|
-
stdio: ["pipe", "pipe", "inherit"],
|
|
92
|
-
env,
|
|
151
|
+
return Promise.resolve({
|
|
152
|
+
outcome: {
|
|
153
|
+
outcome: "cancelled"
|
|
154
|
+
}
|
|
93
155
|
});
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// Create streams for ACP communication
|
|
104
|
-
// Note: Auggie writes to stdout and reads from stdin
|
|
105
|
-
if (!(this.auggieProcess.stdout && this.auggieProcess.stdin)) {
|
|
106
|
-
throw new Error("Failed to create stdio pipes for Auggie process");
|
|
107
|
-
}
|
|
108
|
-
const input = Readable.toWeb(this.auggieProcess.stdout);
|
|
109
|
-
const output = Writable.toWeb(this.auggieProcess.stdin);
|
|
110
|
-
// Create ndjson stream for ACP protocol
|
|
111
|
-
const stream = ndJsonStream(output, input);
|
|
112
|
-
// Create client-side connection with a Client implementation
|
|
113
|
-
this.connection = new ClientSideConnection((agent) => this.createClient(agent), stream);
|
|
114
|
-
if (this.debug) {
|
|
115
|
-
console.log("Connected to Auggie ACP server");
|
|
116
|
-
}
|
|
117
|
-
// Initialize the connection
|
|
118
|
-
await this.initializeConnection();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Initialize the ACP connection
|
|
161
|
+
*/
|
|
162
|
+
async initializeConnection() {
|
|
163
|
+
if (!this.connection) {
|
|
164
|
+
throw new Error("Not connected. Call connect() first.");
|
|
119
165
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (params.options && params.options.length > 0) {
|
|
139
|
-
return Promise.resolve({
|
|
140
|
-
outcome: {
|
|
141
|
-
outcome: "selected",
|
|
142
|
-
optionId: params.options?.[0]?.name || "",
|
|
143
|
-
},
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
// If no options, return cancelled
|
|
147
|
-
return Promise.resolve({
|
|
148
|
-
outcome: {
|
|
149
|
-
outcome: "cancelled",
|
|
150
|
-
},
|
|
151
|
-
});
|
|
152
|
-
},
|
|
153
|
-
};
|
|
166
|
+
if (this.debug) console.log("Initializing ACP connection...");
|
|
167
|
+
const initResponse = await this.connection.initialize({
|
|
168
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
169
|
+
clientInfo: {
|
|
170
|
+
name: "auggie-sdk-typescript",
|
|
171
|
+
version: getSDKVersion()
|
|
172
|
+
},
|
|
173
|
+
clientCapabilities: {}
|
|
174
|
+
});
|
|
175
|
+
if (this.debug)
|
|
176
|
+
console.log("Initialized:", JSON.stringify(initResponse, null, 2));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Create a new session
|
|
180
|
+
*/
|
|
181
|
+
async createSession(tools) {
|
|
182
|
+
if (!this.connection) {
|
|
183
|
+
throw new Error("Not connected. Call connect() first.");
|
|
154
184
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
version: getSDKVersion(),
|
|
169
|
-
},
|
|
170
|
-
clientCapabilities: {},
|
|
171
|
-
});
|
|
172
|
-
if (this.debug)
|
|
173
|
-
console.log("Initialized:", JSON.stringify(initResponse, null, 2));
|
|
185
|
+
if (this.debug) console.log("Creating new session...");
|
|
186
|
+
const mcpServers = [];
|
|
187
|
+
if (tools && Object.keys(tools).length > 0) {
|
|
188
|
+
if (this.debug) console.log("Starting MCP server with provided tools...");
|
|
189
|
+
this.mcpServer = new AuggieMCPServer(tools, void 0);
|
|
190
|
+
const serverUrl = await this.mcpServer.start();
|
|
191
|
+
if (this.debug) console.log(`MCP server started at: ${serverUrl}`);
|
|
192
|
+
mcpServers.push({
|
|
193
|
+
type: "http",
|
|
194
|
+
name: "auggie_sdk",
|
|
195
|
+
url: serverUrl,
|
|
196
|
+
headers: []
|
|
197
|
+
});
|
|
174
198
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
console.log("Creating new session...");
|
|
184
|
-
// Build MCP servers array for the session
|
|
185
|
-
const mcpServers = [];
|
|
186
|
-
// If tools are provided, create and start an MCP server
|
|
187
|
-
if (tools && Object.keys(tools).length > 0) {
|
|
188
|
-
if (this.debug)
|
|
189
|
-
console.log("Starting MCP server with provided tools...");
|
|
190
|
-
this.mcpServer = new AuggieMCPServer(tools, undefined);
|
|
191
|
-
const serverUrl = await this.mcpServer.start();
|
|
192
|
-
if (this.debug)
|
|
193
|
-
console.log(`MCP server started at: ${serverUrl}`);
|
|
194
|
-
// Add the MCP server to the session configuration
|
|
195
|
-
mcpServers.push({
|
|
196
|
-
type: "http",
|
|
197
|
-
name: "auggie_sdk",
|
|
198
|
-
url: serverUrl,
|
|
199
|
-
headers: [],
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
const sessionResponse = await this.connection.newSession({
|
|
203
|
-
cwd: this.workspaceRoot ? this.workspaceRoot : process.cwd(),
|
|
204
|
-
mcpServers,
|
|
205
|
-
});
|
|
206
|
-
this.sessionId = sessionResponse.sessionId;
|
|
207
|
-
if (this.debug) {
|
|
208
|
-
console.log(`Session created: ${this.sessionId}`);
|
|
209
|
-
console.log(" Available modes:", sessionResponse.modes?.availableModes);
|
|
210
|
-
}
|
|
211
|
-
return this.sessionId;
|
|
199
|
+
const sessionResponse = await this.connection.newSession({
|
|
200
|
+
cwd: this.workspaceRoot ? this.workspaceRoot : process.cwd(),
|
|
201
|
+
mcpServers
|
|
202
|
+
});
|
|
203
|
+
this.sessionId = sessionResponse.sessionId;
|
|
204
|
+
if (this.debug) {
|
|
205
|
+
console.log(`Session created: ${this.sessionId}`);
|
|
206
|
+
console.log(" Available modes:", sessionResponse.modes?.availableModes);
|
|
212
207
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (update.update.sessionUpdate === "agent_message_chunk" &&
|
|
245
|
-
update.update.content.type === "text") {
|
|
246
|
-
const textChunk = update.update.content.text;
|
|
247
|
-
accumulatedText += textChunk;
|
|
248
|
-
// If we've seen a tool call, accumulate text for final response
|
|
249
|
-
// This captures the agent's response after tool execution
|
|
250
|
-
if (hasSeenToolCall) {
|
|
251
|
-
finalResponseText += textChunk;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
// Also call the original callback if it exists
|
|
255
|
-
if (originalCallback) {
|
|
256
|
-
originalCallback(update);
|
|
257
|
-
}
|
|
258
|
-
};
|
|
259
|
-
// Temporarily replace the callback
|
|
260
|
-
this.sessionUpdateCallback = accumulatorCallback;
|
|
261
|
-
try {
|
|
262
|
-
// Build content blocks
|
|
263
|
-
const content = [
|
|
264
|
-
{
|
|
265
|
-
type: "text",
|
|
266
|
-
text: message,
|
|
267
|
-
},
|
|
268
|
-
];
|
|
269
|
-
// Send the prompt and wait for response
|
|
270
|
-
const response = await this.connection.prompt({
|
|
271
|
-
sessionId: this.sessionId,
|
|
272
|
-
prompt: content,
|
|
273
|
-
});
|
|
274
|
-
if (this.debug)
|
|
275
|
-
console.log("Prompt completed with stop reason:", response.stopReason);
|
|
276
|
-
// Restore original callback
|
|
277
|
-
this.sessionUpdateCallback = originalCallback;
|
|
278
|
-
// Return final response if requested and we had tool calls, otherwise return all text
|
|
279
|
-
if (isAnswerOnly && hasSeenToolCall) {
|
|
280
|
-
return finalResponseText;
|
|
281
|
-
}
|
|
282
|
-
return accumulatedText;
|
|
208
|
+
return this.sessionId;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Send a prompt to the agent and accumulate the text response
|
|
212
|
+
* @param message - The text prompt to send to the agent
|
|
213
|
+
* @param isAnswerOnly - If true, returns only the final response after all tool calls complete.
|
|
214
|
+
* If false or undefined, returns all agent message chunks (default: false)
|
|
215
|
+
* @returns The accumulated text response from the agent
|
|
216
|
+
*/
|
|
217
|
+
async prompt(message, { isAnswerOnly = false } = {
|
|
218
|
+
isAnswerOnly: false
|
|
219
|
+
}) {
|
|
220
|
+
if (!(this.connection && this.sessionId)) {
|
|
221
|
+
throw new Error("Not connected or no session. Initialization failed.");
|
|
222
|
+
}
|
|
223
|
+
if (this.debug) console.log(`
|
|
224
|
+
Sending prompt: "${message}"`);
|
|
225
|
+
let accumulatedText = "";
|
|
226
|
+
let finalResponseText = "";
|
|
227
|
+
let hasSeenToolCall = false;
|
|
228
|
+
const originalCallback = this.sessionUpdateCallback;
|
|
229
|
+
const accumulatorCallback = (update) => {
|
|
230
|
+
if (update.update.sessionUpdate === "tool_call") {
|
|
231
|
+
hasSeenToolCall = true;
|
|
232
|
+
finalResponseText = "";
|
|
233
|
+
}
|
|
234
|
+
if (update.update.sessionUpdate === "agent_message_chunk" && update.update.content.type === "text") {
|
|
235
|
+
const textChunk = update.update.content.text;
|
|
236
|
+
accumulatedText += textChunk;
|
|
237
|
+
if (hasSeenToolCall) {
|
|
238
|
+
finalResponseText += textChunk;
|
|
283
239
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
240
|
+
}
|
|
241
|
+
if (originalCallback) {
|
|
242
|
+
originalCallback(update);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
this.sessionUpdateCallback = accumulatorCallback;
|
|
246
|
+
try {
|
|
247
|
+
const content = [
|
|
248
|
+
{
|
|
249
|
+
type: "text",
|
|
250
|
+
text: message
|
|
291
251
|
}
|
|
252
|
+
];
|
|
253
|
+
const response = await this.connection.prompt({
|
|
254
|
+
sessionId: this.sessionId,
|
|
255
|
+
prompt: content
|
|
256
|
+
});
|
|
257
|
+
if (this.debug)
|
|
258
|
+
console.log("Prompt completed with stop reason:", response.stopReason);
|
|
259
|
+
this.sessionUpdateCallback = originalCallback;
|
|
260
|
+
if (isAnswerOnly && hasSeenToolCall) {
|
|
261
|
+
return finalResponseText;
|
|
262
|
+
}
|
|
263
|
+
return accumulatedText;
|
|
264
|
+
} catch (error) {
|
|
265
|
+
console.error("Error during prompt:", error);
|
|
266
|
+
this.sessionUpdateCallback = originalCallback;
|
|
267
|
+
await this.close();
|
|
268
|
+
throw error;
|
|
292
269
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Listen for session update events from the agent
|
|
273
|
+
*/
|
|
274
|
+
onSessionUpdate(callback) {
|
|
275
|
+
this.sessionUpdateCallback = callback;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Cancel the current request
|
|
279
|
+
*/
|
|
280
|
+
async cancel() {
|
|
281
|
+
if (!(this.connection && this.sessionId)) {
|
|
282
|
+
throw new Error("Not connected or no session.");
|
|
298
283
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
284
|
+
if (this.debug) console.log(" Cancelling current request...");
|
|
285
|
+
await this.connection.cancel({
|
|
286
|
+
sessionId: this.sessionId
|
|
287
|
+
});
|
|
288
|
+
if (this.debug) console.log(" Request cancelled");
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Close the connection and kill the Auggie process
|
|
292
|
+
*/
|
|
293
|
+
async close() {
|
|
294
|
+
if (this.debug) console.log(" Closing connection...");
|
|
295
|
+
this.killProcess();
|
|
296
|
+
if (this.mcpServer) {
|
|
297
|
+
if (this.debug) console.log(" Stopping MCP server...");
|
|
298
|
+
await Promise.race([
|
|
299
|
+
this.mcpServer.stop(),
|
|
300
|
+
new Promise((resolve) => setTimeout(resolve, 100))
|
|
301
|
+
]);
|
|
302
|
+
this.mcpServer = null;
|
|
313
303
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
async close() {
|
|
318
|
-
if (this.debug)
|
|
319
|
-
console.log(" Closing connection...");
|
|
320
|
-
// Kill the Auggie process first - this will naturally close all MCP connections
|
|
321
|
-
this.killProcess();
|
|
322
|
-
// Stop the MCP server (should be fast since Auggie is already killed)
|
|
323
|
-
if (this.mcpServer) {
|
|
324
|
-
if (this.debug)
|
|
325
|
-
console.log(" Stopping MCP server...");
|
|
326
|
-
// Use a short timeout since the connections are already closed
|
|
327
|
-
await Promise.race([
|
|
328
|
-
this.mcpServer.stop(),
|
|
329
|
-
new Promise((resolve) => setTimeout(resolve, 100)),
|
|
330
|
-
]);
|
|
331
|
-
this.mcpServer = null;
|
|
332
|
-
}
|
|
333
|
-
// Clean up connection state
|
|
334
|
-
if (this.connection) {
|
|
335
|
-
this.connection = null;
|
|
336
|
-
this.sessionId = null;
|
|
337
|
-
}
|
|
338
|
-
if (this.debug)
|
|
339
|
-
console.log(" Connection closed");
|
|
304
|
+
if (this.connection) {
|
|
305
|
+
this.connection = null;
|
|
306
|
+
this.sessionId = null;
|
|
340
307
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
308
|
+
if (this.debug) console.log(" Connection closed");
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Kill the Auggie process if it's still running
|
|
312
|
+
*/
|
|
313
|
+
killProcess() {
|
|
314
|
+
if (this.auggieProcess && !this.auggieProcess.killed) {
|
|
315
|
+
if (this.debug) console.log(" Killing Auggie process...");
|
|
316
|
+
this.auggieProcess.kill("SIGKILL");
|
|
351
317
|
}
|
|
318
|
+
}
|
|
352
319
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
320
|
+
export {
|
|
321
|
+
Auggie
|
|
322
|
+
};
|