@alint-js/agent-apeira 0.0.25 → 0.0.28

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/index.d.mts CHANGED
@@ -1,7 +1,6 @@
1
+ import { AgentAdapter, AgentTool } from "@alint-js/core/agent";
1
2
  import { Runner, Tool } from "apeira";
2
3
  import { ResolvedModel } from "@alint-js/core";
3
- import { AgentAdapter, AgentTool } from "@alint-js/core/agent";
4
-
5
4
  //#region src/index.d.ts
6
5
  interface ApeiraAdapterOptions {
7
6
  createRunner: (model: ResolvedModel, maxSteps: number) => Runner;
@@ -9,6 +8,6 @@ interface ApeiraAdapterOptions {
9
8
  }
10
9
  declare function createApeiraAdapter(options?: Partial<ApeiraAdapterOptions>): AgentAdapter;
11
10
  declare function createApeiraRunner(model: ResolvedModel, maxSteps?: number): Runner;
12
- declare function toRunnerTools(tools: AgentTool[]): Tool[];
11
+ declare function toRunnerTools(tools: AgentTool[], onToolStart?: () => void): Tool[];
13
12
  //#endregion
14
13
  export { ApeiraAdapterOptions, createApeiraAdapter, createApeiraRunner, toRunnerTools };
package/dist/index.mjs CHANGED
@@ -1,4 +1,30 @@
1
+ import { RetryableAgentError } from "@alint-js/core/agent";
1
2
  import { chat, rawTool, stepCountAtLeast, user } from "apeira";
3
+ //#region src/retry.ts
4
+ function isRetryableApeiraFailure(error) {
5
+ const seen = /* @__PURE__ */ new Set();
6
+ let candidate = error;
7
+ let retryable = false;
8
+ while (isObject(candidate) && !seen.has(candidate)) {
9
+ seen.add(candidate);
10
+ if (readProperty(candidate, "name") === "AbortError") return false;
11
+ const statusCode = readProperty(candidate, "statusCode");
12
+ if (typeof statusCode === "number" && (statusCode === 408 || statusCode === 429 || Number.isInteger(statusCode) && statusCode >= 500 && statusCode <= 599)) retryable = true;
13
+ candidate = readProperty(candidate, "cause");
14
+ }
15
+ return retryable;
16
+ }
17
+ function isObject(value) {
18
+ return typeof value === "object" && value !== null || typeof value === "function";
19
+ }
20
+ function readProperty(value, property) {
21
+ try {
22
+ return Reflect.get(value, property);
23
+ } catch {
24
+ return;
25
+ }
26
+ }
27
+ //#endregion
2
28
  //#region src/index.ts
3
29
  const defaultMaxSteps = 8;
4
30
  function createApeiraAdapter(options = {}) {
@@ -6,11 +32,20 @@ function createApeiraAdapter(options = {}) {
6
32
  const maxSteps = options.maxSteps ?? defaultMaxSteps;
7
33
  if (!Number.isInteger(maxSteps) || maxSteps < 1) throw new TypeError("Apeira adapter maxSteps must be a positive integer");
8
34
  return async (request) => {
9
- const result = await createRunner(request.model, maxSteps)(buildRunnerContext(request));
10
- return {
11
- answer: extractAnswer(result.output),
12
- usage: mapUsage(result.usage)
13
- };
35
+ let toolStarted = false;
36
+ const runner = createRunner(request.model, maxSteps);
37
+ try {
38
+ const result = await runner(buildRunnerContext(request, () => {
39
+ toolStarted = true;
40
+ }));
41
+ return {
42
+ answer: extractAnswer(result.output),
43
+ usage: mapUsage(result.usage)
44
+ };
45
+ } catch (error) {
46
+ if (!toolStarted && !request.signal?.aborted && isRetryableApeiraFailure(error)) throw new RetryableAgentError("Apeira agent invocation can be safely replayed", { cause: error });
47
+ throw error;
48
+ }
14
49
  };
15
50
  }
16
51
  function createApeiraRunner(model, maxSteps = defaultMaxSteps) {
@@ -22,22 +57,24 @@ function createApeiraRunner(model, maxSteps = defaultMaxSteps) {
22
57
  streamOptions: { includeUsage: true }
23
58
  });
24
59
  }
25
- function toRunnerTools(tools) {
60
+ function toRunnerTools(tools, onToolStart = () => {}) {
26
61
  return tools.map((agentTool) => rawTool({
27
62
  description: agentTool.description,
28
63
  execute: async (input) => {
64
+ onToolStart();
29
65
  return await agentTool.execute(input) ?? "";
30
66
  },
31
67
  name: agentTool.name,
32
68
  parameters: agentTool.parameters
33
69
  }));
34
70
  }
35
- function buildRunnerContext(request) {
71
+ function buildRunnerContext(request, onToolStart) {
36
72
  return {
73
+ abortSignal: request.signal,
37
74
  channel: noopChannel(),
38
75
  input: [user(request.prompt)],
39
76
  instructions: request.instructions,
40
- tools: toRunnerTools(request.tools),
77
+ tools: toRunnerTools(request.tools, onToolStart),
41
78
  turnId: "alint"
42
79
  };
43
80
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alint-js/agent-apeira",
3
3
  "type": "module",
4
- "version": "0.0.25",
4
+ "version": "0.0.28",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.mts",
@@ -13,13 +13,13 @@
13
13
  "dist"
14
14
  ],
15
15
  "peerDependencies": {
16
- "@alint-js/core": "0.0.25"
16
+ "@alint-js/core": "0.0.28"
17
17
  },
18
18
  "dependencies": {
19
19
  "apeira": "^0.0.7"
20
20
  },
21
21
  "devDependencies": {
22
- "@alint-js/core": "0.0.25"
22
+ "@alint-js/core": "0.0.28"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "tsdown",