@librechat/agents 4.0.3 → 4.0.5

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.
Files changed (39) hide show
  1. package/dist/cjs/llm/bedrock/index.cjs +115 -76
  2. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  3. package/dist/cjs/llm/bedrock/utils/harmony.cjs +147 -0
  4. package/dist/cjs/llm/bedrock/utils/harmony.cjs.map +1 -0
  5. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +13 -6
  6. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
  7. package/dist/cjs/llm/bedrock/utils/message_outputs.cjs +68 -0
  8. package/dist/cjs/llm/bedrock/utils/message_outputs.cjs.map +1 -1
  9. package/dist/cjs/llm/invoke.cjs +13 -3
  10. package/dist/cjs/llm/invoke.cjs.map +1 -1
  11. package/dist/cjs/main.cjs +8 -0
  12. package/dist/cjs/tools/GitHubCompare.cjs +181 -0
  13. package/dist/cjs/tools/GitHubCompare.cjs.map +1 -0
  14. package/dist/esm/llm/bedrock/index.mjs +118 -80
  15. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  16. package/dist/esm/llm/bedrock/utils/harmony.mjs +146 -0
  17. package/dist/esm/llm/bedrock/utils/harmony.mjs.map +1 -0
  18. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +13 -7
  19. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
  20. package/dist/esm/llm/bedrock/utils/message_outputs.mjs +68 -2
  21. package/dist/esm/llm/bedrock/utils/message_outputs.mjs.map +1 -1
  22. package/dist/esm/llm/invoke.mjs +13 -3
  23. package/dist/esm/llm/invoke.mjs.map +1 -1
  24. package/dist/esm/main.mjs +2 -1
  25. package/dist/esm/tools/GitHubCompare.mjs +175 -0
  26. package/dist/esm/tools/GitHubCompare.mjs.map +1 -0
  27. package/dist/types/index.d.ts +1 -0
  28. package/dist/types/llm/bedrock/index.d.ts +14 -1
  29. package/dist/types/llm/bedrock/utils/harmony.d.ts +34 -0
  30. package/dist/types/llm/bedrock/utils/message_inputs.d.ts +10 -1
  31. package/dist/types/llm/invoke.d.ts +7 -0
  32. package/dist/types/tools/GitHubCompare.d.ts +104 -0
  33. package/package.json +1 -1
  34. package/src/index.ts +1 -0
  35. package/src/llm/bedrock/index.ts +83 -5
  36. package/src/llm/bedrock/utils/harmony.ts +235 -0
  37. package/src/llm/bedrock/utils/message_inputs.ts +34 -11
  38. package/src/llm/invoke.ts +32 -10
  39. package/src/tools/GitHubCompare.ts +319 -0
@@ -0,0 +1,319 @@
1
+ import { DynamicStructuredTool } from '@langchain/core/tools';
2
+ import { INTENT_PROPERTY } from '@/tools/intentArg';
3
+
4
+ export const GitHubCompareToolName = 'github_compare';
5
+ export const GitHubCompareToolDescription =
6
+ 'Read-only GitHub comparison of two full commit SHAs in a public repository. Returns the merge-base, relation and commit counts without Git, a worktree, or worker admission. Does not fetch code or execute commands. Private repositories and arbitrary URLs are not supported.';
7
+ export const GitHubCompareToolSchema = {
8
+ type: 'object',
9
+ properties: {
10
+ intent: { ...INTENT_PROPERTY },
11
+ owner: { type: 'string', description: 'GitHub repository owner.' },
12
+ repo: { type: 'string', description: 'GitHub repository name.' },
13
+ base: {
14
+ type: 'string',
15
+ description: 'Full lowercase base commit SHA, not a moving branch.',
16
+ },
17
+ head: {
18
+ type: 'string',
19
+ description: 'Full lowercase head commit SHA, not a moving branch.',
20
+ },
21
+ },
22
+ required: ['owner', 'repo', 'base', 'head'],
23
+ } as const;
24
+ export const GitHubCompareToolDefinition = {
25
+ name: GitHubCompareToolName,
26
+ description: GitHubCompareToolDescription,
27
+ parameters: GitHubCompareToolSchema,
28
+ } as const;
29
+
30
+ export interface GitHubComparisonInput {
31
+ owner: string;
32
+ repo: string;
33
+ base: string;
34
+ head: string;
35
+ }
36
+
37
+ export interface GitHubComparison {
38
+ base: string;
39
+ head: string;
40
+ mergeBase: string;
41
+ status: 'ahead' | 'behind' | 'identical' | 'diverged';
42
+ aheadBy: number;
43
+ behindBy: number;
44
+ totalCommits: number;
45
+ }
46
+
47
+ export interface GitHubCompareOptions {
48
+ /** Caller-owned transport, including proxy and instrumentation policy. */
49
+ fetch: typeof fetch;
50
+ /** Entire request budget, including body reads. Defaults to 10 seconds; maximum 30 seconds. */
51
+ timeoutMs?: number;
52
+ }
53
+
54
+ export type GitHubComparisonErrorCode =
55
+ | 'INVALID_INPUT'
56
+ | 'INVALID_CONFIGURATION'
57
+ | 'HTTP_ERROR'
58
+ | 'INVALID_RESPONSE'
59
+ | 'RESPONSE_TOO_LARGE'
60
+ | 'TRANSPORT_ERROR';
61
+
62
+ export class GitHubComparisonError extends Error {
63
+ constructor(
64
+ public readonly code: GitHubComparisonErrorCode,
65
+ message: string,
66
+ public readonly status?: number
67
+ ) {
68
+ super(message);
69
+ this.name = 'GitHubComparisonError';
70
+ }
71
+ }
72
+
73
+ interface GitHubComparisonResponse {
74
+ base_commit?: { sha?: string };
75
+ merge_base_commit?: { sha?: string };
76
+ status?: GitHubComparison['status'];
77
+ ahead_by?: number;
78
+ behind_by?: number;
79
+ total_commits?: number;
80
+ }
81
+
82
+ const SHA = /^[a-f0-9]{40}$/;
83
+ const REPO = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/;
84
+ const MAX_RESPONSE_BYTES = 2 * 1024 * 1024;
85
+
86
+ function awaitWithSignal<T>(
87
+ operation: () => Promise<T>,
88
+ signal: AbortSignal
89
+ ): Promise<T> {
90
+ signal.throwIfAborted();
91
+ return new Promise<T>((resolve, reject) => {
92
+ const onAbort = (): void => {
93
+ signal.removeEventListener('abort', onAbort);
94
+ reject(signal.reason);
95
+ };
96
+ signal.addEventListener('abort', onAbort, { once: true });
97
+ try {
98
+ operation().then(
99
+ (value) => {
100
+ signal.removeEventListener('abort', onAbort);
101
+ resolve(value);
102
+ },
103
+ (error) => {
104
+ signal.removeEventListener('abort', onAbort);
105
+ reject(error);
106
+ }
107
+ );
108
+ } catch (error) {
109
+ signal.removeEventListener('abort', onAbort);
110
+ reject(error);
111
+ }
112
+ });
113
+ }
114
+
115
+ function cancelBody(response: Response): void {
116
+ void response.body?.cancel().catch(() => undefined);
117
+ }
118
+
119
+ async function readResponse(
120
+ response: Response,
121
+ signal: AbortSignal
122
+ ): Promise<string> {
123
+ const reader = response.body?.getReader();
124
+ if (reader == null) {
125
+ throw new GitHubComparisonError(
126
+ 'INVALID_RESPONSE',
127
+ 'GitHub comparison returned an invalid response.'
128
+ );
129
+ }
130
+ const chunks: Uint8Array[] = [];
131
+ let bytes = 0;
132
+ try {
133
+ for (;;) {
134
+ const item = await awaitWithSignal(() => reader.read(), signal);
135
+ if (item.done) {
136
+ break;
137
+ }
138
+ bytes += item.value.byteLength;
139
+ if (bytes > MAX_RESPONSE_BYTES) {
140
+ throw new GitHubComparisonError(
141
+ 'RESPONSE_TOO_LARGE',
142
+ 'GitHub comparison response exceeded its bound.'
143
+ );
144
+ }
145
+ chunks.push(item.value);
146
+ }
147
+ return Buffer.concat(chunks, bytes).toString('utf8');
148
+ } finally {
149
+ void reader.cancel().catch(() => undefined);
150
+ reader.releaseLock();
151
+ }
152
+ }
153
+
154
+ function parseComparison(
155
+ text: string,
156
+ input: GitHubComparisonInput
157
+ ): GitHubComparison {
158
+ let data: GitHubComparisonResponse | null;
159
+ try {
160
+ data = JSON.parse(text) as GitHubComparisonResponse | null;
161
+ } catch {
162
+ throw new GitHubComparisonError(
163
+ 'INVALID_RESPONSE',
164
+ 'GitHub comparison returned an invalid response.'
165
+ );
166
+ }
167
+ const mergeBase = data?.merge_base_commit?.sha;
168
+ const status = data?.status;
169
+ const aheadBy = data?.ahead_by;
170
+ const behindBy = data?.behind_by;
171
+ const totalCommits = data?.total_commits;
172
+ if (
173
+ data?.base_commit?.sha !== input.base ||
174
+ typeof mergeBase !== 'string' ||
175
+ !SHA.test(mergeBase) ||
176
+ (status !== 'ahead' &&
177
+ status !== 'behind' &&
178
+ status !== 'identical' &&
179
+ status !== 'diverged') ||
180
+ typeof aheadBy !== 'number' ||
181
+ !Number.isSafeInteger(aheadBy) ||
182
+ aheadBy < 0 ||
183
+ typeof behindBy !== 'number' ||
184
+ !Number.isSafeInteger(behindBy) ||
185
+ behindBy < 0 ||
186
+ typeof totalCommits !== 'number' ||
187
+ !Number.isSafeInteger(totalCommits) ||
188
+ totalCommits < 0
189
+ ) {
190
+ throw new GitHubComparisonError(
191
+ 'INVALID_RESPONSE',
192
+ 'GitHub comparison returned an invalid response.'
193
+ );
194
+ }
195
+ return {
196
+ base: input.base,
197
+ head: input.head,
198
+ mergeBase,
199
+ status,
200
+ aheadBy,
201
+ behindBy,
202
+ totalCommits,
203
+ };
204
+ }
205
+
206
+ export async function compareGitHubCommits(
207
+ input: GitHubComparisonInput,
208
+ options: GitHubCompareOptions,
209
+ signal?: AbortSignal
210
+ ): Promise<GitHubComparison> {
211
+ signal?.throwIfAborted();
212
+ if (
213
+ typeof input.owner !== 'string' ||
214
+ !REPO.test(input.owner) ||
215
+ typeof input.repo !== 'string' ||
216
+ !REPO.test(input.repo) ||
217
+ typeof input.base !== 'string' ||
218
+ !SHA.test(input.base) ||
219
+ typeof input.head !== 'string' ||
220
+ !SHA.test(input.head)
221
+ ) {
222
+ throw new GitHubComparisonError(
223
+ 'INVALID_INPUT',
224
+ 'Supply a repository and full lowercase commit SHAs.'
225
+ );
226
+ }
227
+ const timeoutMs = options.timeoutMs ?? 10000;
228
+ if (
229
+ typeof options.fetch !== 'function' ||
230
+ !Number.isSafeInteger(timeoutMs) ||
231
+ timeoutMs < 1 ||
232
+ timeoutMs > 30000
233
+ ) {
234
+ throw new GitHubComparisonError(
235
+ 'INVALID_CONFIGURATION',
236
+ 'Supply a GitHub transport and a timeout between 1 and 30000 ms.'
237
+ );
238
+ }
239
+ const timeout = new AbortController();
240
+ const timer = setTimeout(
241
+ () =>
242
+ timeout.abort(
243
+ new DOMException('GitHub comparison timed out.', 'TimeoutError')
244
+ ),
245
+ timeoutMs
246
+ );
247
+ timer.unref();
248
+ let pendingResponse: Response | undefined;
249
+ const requestSignal =
250
+ signal == null ? timeout.signal : AbortSignal.any([signal, timeout.signal]);
251
+ try {
252
+ const response = await awaitWithSignal(
253
+ () =>
254
+ options
255
+ .fetch(
256
+ `https://api.github.com/repos/${encodeURIComponent(input.owner)}/${encodeURIComponent(input.repo)}/compare/${input.base}...${input.head}?per_page=1&page=2`,
257
+ {
258
+ method: 'GET',
259
+ redirect: 'error',
260
+ credentials: 'omit',
261
+ signal: requestSignal,
262
+ headers: {
263
+ Accept: 'application/vnd.github+json',
264
+ 'X-GitHub-Api-Version': '2022-11-28',
265
+ },
266
+ }
267
+ )
268
+ .then((response) => {
269
+ pendingResponse = response;
270
+ if (requestSignal.aborted) {
271
+ pendingResponse = undefined;
272
+ cancelBody(response);
273
+ requestSignal.throwIfAborted();
274
+ }
275
+ return response;
276
+ }),
277
+ requestSignal
278
+ );
279
+ if (!response.ok) {
280
+ throw new GitHubComparisonError(
281
+ 'HTTP_ERROR',
282
+ `GitHub comparison unavailable (HTTP ${response.status}).`,
283
+ response.status
284
+ );
285
+ }
286
+ pendingResponse = undefined;
287
+ const text = await readResponse(response, requestSignal);
288
+ requestSignal.throwIfAborted();
289
+ return parseComparison(text, input);
290
+ } catch (error) {
291
+ requestSignal.throwIfAborted();
292
+ if (error instanceof GitHubComparisonError) {
293
+ throw error;
294
+ }
295
+ throw new GitHubComparisonError(
296
+ 'TRANSPORT_ERROR',
297
+ 'GitHub comparison transport failed.'
298
+ );
299
+ } finally {
300
+ if (pendingResponse != null) {
301
+ cancelBody(pendingResponse);
302
+ }
303
+ clearTimeout(timer);
304
+ }
305
+ }
306
+
307
+ export class GitHubCompareTool extends DynamicStructuredTool {
308
+ constructor(options: GitHubCompareOptions) {
309
+ super({
310
+ name: GitHubCompareToolName,
311
+ description: GitHubCompareToolDescription,
312
+ schema: structuredClone(GitHubCompareToolSchema),
313
+ func: async (input: GitHubComparisonInput, _manager, config) =>
314
+ JSON.stringify(
315
+ await compareGitHubCommits(input, options, config?.signal)
316
+ ),
317
+ });
318
+ }
319
+ }