@opengeni/codemode 0.2.2

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.
@@ -0,0 +1,52 @@
1
+ import type { AttemptToolResult } from "@opengeni/contracts";
2
+ import type { CodemodeCallOptions, CodemodeClient } from "./index";
3
+ import { environmentCodemodeClient, type CodemodeClientProvider } from "./environment";
4
+
5
+ export class CodemodeToolExecutionError extends Error {
6
+ readonly code: string;
7
+ readonly retryable: boolean;
8
+
9
+ constructor(readonly result: AttemptToolResult) {
10
+ const error = toolError(result);
11
+ super(error.message);
12
+ this.name = "CodemodeToolExecutionError";
13
+ this.code = error.code;
14
+ this.retryable = error.retryable;
15
+ }
16
+ }
17
+
18
+ export function codemodeClientProvider(
19
+ client: CodemodeClient | CodemodeClientProvider = () => environmentCodemodeClient(),
20
+ ): CodemodeClientProvider {
21
+ return typeof client === "function" ? client : () => client;
22
+ }
23
+
24
+ export async function callStructured<T>(
25
+ client: CodemodeClientProvider,
26
+ path: readonly string[],
27
+ args: Record<string, unknown>,
28
+ options: CodemodeCallOptions,
29
+ ): Promise<T> {
30
+ const result = await (await client()).callPath(path, args, options);
31
+ if (result.isError) throw new CodemodeToolExecutionError(result);
32
+ if (!result.structuredContent) {
33
+ throw new Error(`Codemode tool ${path.join(".")} returned no structured content`);
34
+ }
35
+ return result.structuredContent as T;
36
+ }
37
+
38
+ function toolError(result: AttemptToolResult): {
39
+ code: string;
40
+ message: string;
41
+ retryable: boolean;
42
+ } {
43
+ const structured = result.structuredContent as
44
+ | { error?: { code?: unknown; message?: unknown; retryable?: unknown } }
45
+ | undefined;
46
+ const error = structured?.error;
47
+ return {
48
+ code: typeof error?.code === "string" ? error.code : "tool_error",
49
+ message: typeof error?.message === "string" ? error.message : "Codemode tool failed",
50
+ retryable: error?.retryable === true,
51
+ };
52
+ }