@jxburros/ai-nugget 0.3.1 → 0.6.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 +134 -271
- package/UPGRADING.md +158 -0
- package/dist/adapters/engines/anthropic.d.ts +7 -1
- package/dist/adapters/engines/anthropic.js +64 -37
- package/dist/adapters/engines/base.d.ts +29 -2
- package/dist/adapters/engines/base.js +52 -8
- package/dist/adapters/engines/google.d.ts +17 -1
- package/dist/adapters/engines/google.js +93 -29
- package/dist/adapters/engines/ollama.d.ts +2 -1
- package/dist/adapters/engines/ollama.js +51 -15
- package/dist/adapters/engines/openaiChat.d.ts +2 -1
- package/dist/adapters/engines/openaiChat.js +60 -30
- package/dist/adapters/profiles.d.ts +21 -1
- package/dist/adapters/profiles.js +12 -2
- package/dist/agent/loop.d.ts +43 -1
- package/dist/agent/loop.js +120 -13
- package/dist/cjs/adapters/engines/anthropic.js +308 -0
- package/dist/cjs/adapters/engines/base.js +147 -0
- package/dist/cjs/adapters/engines/google.js +284 -0
- package/dist/cjs/adapters/engines/ollama.js +199 -0
- package/dist/cjs/adapters/engines/openaiChat.js +261 -0
- package/dist/cjs/adapters/index.js +22 -0
- package/dist/cjs/adapters/profiles.js +125 -0
- package/dist/cjs/agent/index.js +18 -0
- package/dist/cjs/agent/loop.js +372 -0
- package/dist/cjs/agent/tools.js +51 -0
- package/dist/cjs/connect.js +27 -0
- package/dist/cjs/errors.js +110 -0
- package/dist/cjs/handler.js +561 -0
- package/dist/cjs/index.js +27 -0
- package/dist/cjs/json.js +121 -0
- package/dist/cjs/keys.js +81 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/policy.js +42 -0
- package/dist/cjs/redact.js +78 -0
- package/dist/cjs/tokens.js +22 -0
- package/dist/cjs/transport.js +172 -0
- package/dist/cjs/types.js +6 -0
- package/dist/cjs/util.js +112 -0
- package/dist/connect.d.ts +31 -0
- package/dist/connect.js +25 -0
- package/dist/errors.js +25 -1
- package/dist/handler.d.ts +61 -1
- package/dist/handler.js +247 -61
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/keys.js +1 -4
- package/dist/redact.d.ts +18 -0
- package/dist/redact.js +29 -1
- package/dist/transport.d.ts +3 -1
- package/dist/transport.js +21 -1
- package/dist/types.d.ts +96 -2
- package/dist/util.d.ts +16 -0
- package/dist/util.js +50 -0
- package/nugget/VERSION.txt +2 -2
- package/nugget/dist/adapters/engines/anthropic.d.ts +14 -0
- package/nugget/dist/adapters/engines/anthropic.d.ts.map +1 -0
- package/nugget/dist/adapters/engines/anthropic.js +305 -0
- package/nugget/dist/adapters/engines/anthropic.js.map +1 -0
- package/nugget/dist/adapters/engines/base.d.ts +50 -0
- package/nugget/dist/adapters/engines/base.d.ts.map +1 -0
- package/nugget/dist/adapters/engines/base.js +137 -0
- package/nugget/dist/adapters/engines/base.js.map +1 -0
- package/nugget/dist/adapters/engines/google.d.ts +24 -0
- package/nugget/dist/adapters/engines/google.d.ts.map +1 -0
- package/nugget/dist/adapters/engines/google.js +281 -0
- package/nugget/dist/adapters/engines/google.js.map +1 -0
- package/nugget/dist/adapters/engines/ollama.d.ts +11 -0
- package/nugget/dist/adapters/engines/ollama.d.ts.map +1 -0
- package/nugget/dist/adapters/engines/ollama.js +196 -0
- package/nugget/dist/adapters/engines/ollama.js.map +1 -0
- package/nugget/dist/adapters/engines/openaiChat.d.ts +16 -0
- package/nugget/dist/adapters/engines/openaiChat.d.ts.map +1 -0
- package/nugget/dist/adapters/engines/openaiChat.js +258 -0
- package/nugget/dist/adapters/engines/openaiChat.js.map +1 -0
- package/nugget/dist/adapters/index.d.ts +5 -0
- package/nugget/dist/adapters/index.d.ts.map +1 -0
- package/nugget/dist/adapters/index.js +17 -0
- package/nugget/dist/adapters/index.js.map +1 -0
- package/nugget/dist/adapters/profiles.d.ts +62 -0
- package/nugget/dist/adapters/profiles.d.ts.map +1 -0
- package/nugget/dist/adapters/profiles.js +121 -0
- package/nugget/dist/adapters/profiles.js.map +1 -0
- package/nugget/dist/agent/index.d.ts +3 -0
- package/nugget/dist/agent/index.d.ts.map +1 -0
- package/nugget/dist/agent/index.js +3 -0
- package/nugget/dist/agent/index.js.map +1 -0
- package/nugget/dist/agent/loop.d.ts +112 -0
- package/nugget/dist/agent/loop.d.ts.map +1 -0
- package/nugget/dist/agent/loop.js +370 -0
- package/nugget/dist/agent/loop.js.map +1 -0
- package/nugget/dist/agent/tools.d.ts +29 -0
- package/nugget/dist/agent/tools.d.ts.map +1 -0
- package/nugget/dist/agent/tools.js +48 -0
- package/nugget/dist/agent/tools.js.map +1 -0
- package/nugget/dist/cjs/adapters/engines/anthropic.js +308 -0
- package/nugget/dist/cjs/adapters/engines/base.js +147 -0
- package/nugget/dist/cjs/adapters/engines/google.js +284 -0
- package/nugget/dist/cjs/adapters/engines/ollama.js +199 -0
- package/nugget/dist/cjs/adapters/engines/openaiChat.js +261 -0
- package/nugget/dist/cjs/adapters/index.js +22 -0
- package/nugget/dist/cjs/adapters/profiles.js +125 -0
- package/nugget/dist/cjs/agent/index.js +18 -0
- package/nugget/dist/cjs/agent/loop.js +372 -0
- package/nugget/dist/cjs/agent/tools.js +51 -0
- package/nugget/dist/cjs/connect.js +27 -0
- package/nugget/dist/cjs/errors.js +110 -0
- package/nugget/dist/cjs/handler.js +561 -0
- package/nugget/dist/cjs/index.js +27 -0
- package/nugget/dist/cjs/json.js +121 -0
- package/nugget/dist/cjs/keys.js +81 -0
- package/nugget/dist/cjs/package.json +3 -0
- package/nugget/dist/cjs/policy.js +42 -0
- package/nugget/dist/cjs/redact.js +78 -0
- package/nugget/dist/cjs/tokens.js +22 -0
- package/nugget/dist/cjs/transport.js +172 -0
- package/nugget/dist/cjs/types.js +6 -0
- package/nugget/dist/cjs/util.js +112 -0
- package/nugget/dist/connect.d.ts +31 -0
- package/nugget/dist/connect.d.ts.map +1 -0
- package/nugget/dist/connect.js +25 -0
- package/nugget/dist/connect.js.map +1 -0
- package/nugget/dist/errors.d.ts +22 -0
- package/nugget/dist/errors.d.ts.map +1 -0
- package/nugget/dist/errors.js +104 -0
- package/nugget/dist/errors.js.map +1 -0
- package/nugget/dist/handler.d.ts +108 -0
- package/nugget/dist/handler.d.ts.map +1 -0
- package/nugget/dist/handler.js +557 -0
- package/nugget/dist/handler.js.map +1 -0
- package/nugget/dist/index.d.ts +12 -0
- package/nugget/dist/index.d.ts.map +1 -0
- package/nugget/dist/index.js +12 -0
- package/nugget/dist/index.js.map +1 -0
- package/nugget/dist/json.d.ts +7 -0
- package/nugget/dist/json.d.ts.map +1 -0
- package/nugget/dist/json.js +114 -0
- package/nugget/dist/json.js.map +1 -0
- package/nugget/dist/keys.d.ts +7 -0
- package/nugget/dist/keys.d.ts.map +1 -0
- package/nugget/dist/keys.js +75 -0
- package/nugget/dist/keys.js.map +1 -0
- package/nugget/dist/policy.d.ts +6 -0
- package/nugget/dist/policy.d.ts.map +1 -0
- package/nugget/dist/policy.js +37 -0
- package/nugget/dist/policy.js.map +1 -0
- package/nugget/dist/redact.d.ts +26 -0
- package/nugget/dist/redact.d.ts.map +1 -0
- package/nugget/dist/redact.js +74 -0
- package/nugget/dist/redact.js.map +1 -0
- package/nugget/dist/tokens.d.ts +5 -0
- package/nugget/dist/tokens.d.ts.map +1 -0
- package/nugget/dist/tokens.js +18 -0
- package/nugget/dist/tokens.js.map +1 -0
- package/nugget/dist/transport.d.ts +30 -0
- package/nugget/dist/transport.d.ts.map +1 -0
- package/nugget/dist/transport.js +165 -0
- package/nugget/dist/transport.js.map +1 -0
- package/nugget/dist/types.d.ts +285 -0
- package/nugget/dist/types.d.ts.map +1 -0
- package/nugget/dist/types.js +4 -0
- package/nugget/dist/types.js.map +1 -0
- package/nugget/dist/util.d.ts +24 -0
- package/nugget/dist/util.d.ts.map +1 -0
- package/nugget/dist/util.js +101 -0
- package/nugget/dist/util.js.map +1 -0
- package/nugget/src/adapters/engines/anthropic.ts +62 -37
- package/nugget/src/adapters/engines/base.ts +50 -7
- package/nugget/src/adapters/engines/google.ts +92 -29
- package/nugget/src/adapters/engines/ollama.ts +51 -17
- package/nugget/src/adapters/engines/openaiChat.ts +61 -32
- package/nugget/src/adapters/profiles.ts +53 -3
- package/nugget/src/agent/loop.ts +158 -14
- package/nugget/src/connect.ts +48 -0
- package/nugget/src/errors.ts +25 -1
- package/nugget/src/handler.ts +286 -59
- package/nugget/src/index.ts +1 -0
- package/nugget/src/keys.ts +1 -5
- package/nugget/src/redact.ts +32 -1
- package/nugget/src/transport.ts +20 -1
- package/nugget/src/types.ts +96 -1
- package/nugget/src/util.ts +55 -0
- package/package.json +20 -6
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
const HOSTED_CLOUD = { nativeTools: true, jsonMode: true, local: false, embeddable: false };
|
|
2
|
+
const LOCAL_RUNTIME = { nativeTools: false, jsonMode: false, local: true, embeddable: true };
|
|
3
|
+
export const PROVIDER_PROFILES = {
|
|
4
|
+
openai: {
|
|
5
|
+
engine: 'openaiChat',
|
|
6
|
+
defaultBaseUrl: 'https://api.openai.com/v1',
|
|
7
|
+
auth: 'bearer',
|
|
8
|
+
listModelsPath: '/models',
|
|
9
|
+
capabilities: HOSTED_CLOUD,
|
|
10
|
+
quirks: { supportsUsageInStream: true, maxTokensParam: 'max_completion_tokens', supportsJsonSchema: true, supportsIdempotencyKey: true },
|
|
11
|
+
},
|
|
12
|
+
'azure-openai': {
|
|
13
|
+
engine: 'openaiChat',
|
|
14
|
+
auth: 'api-key-header',
|
|
15
|
+
capabilities: HOSTED_CLOUD,
|
|
16
|
+
quirks: { urlTemplate: '{baseUrl}/openai/deployments/{model}/chat/completions?api-version={apiVersion}', apiVersion: '2024-10-21', maxTokensParam: 'max_completion_tokens', supportsJsonSchema: true },
|
|
17
|
+
},
|
|
18
|
+
openrouter: {
|
|
19
|
+
engine: 'openaiChat',
|
|
20
|
+
defaultBaseUrl: 'https://openrouter.ai/api/v1',
|
|
21
|
+
auth: 'bearer',
|
|
22
|
+
defaultHeaders: { 'HTTP-Referer': 'https://github.com/jxburros/AI-Nugget', 'X-Title': 'ai-nugget' },
|
|
23
|
+
listModelsPath: '/models',
|
|
24
|
+
capabilities: HOSTED_CLOUD,
|
|
25
|
+
quirks: { supportsUsageInStream: true },
|
|
26
|
+
},
|
|
27
|
+
groq: { engine: 'openaiChat', defaultBaseUrl: 'https://api.groq.com/openai/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
28
|
+
deepseek: { engine: 'openaiChat', defaultBaseUrl: 'https://api.deepseek.com/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
29
|
+
mistral: { engine: 'openaiChat', defaultBaseUrl: 'https://api.mistral.ai/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
30
|
+
together: { engine: 'openaiChat', defaultBaseUrl: 'https://api.together.xyz/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
31
|
+
fireworks: { engine: 'openaiChat', defaultBaseUrl: 'https://api.fireworks.ai/inference/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
32
|
+
cerebras: { engine: 'openaiChat', defaultBaseUrl: 'https://api.cerebras.ai/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
33
|
+
moonshot: { engine: 'openaiChat', defaultBaseUrl: 'https://api.moonshot.ai/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
34
|
+
// Cohere's OpenAI-compatible surface lives under /compatibility/v1.
|
|
35
|
+
cohere: { engine: 'openaiChat', defaultBaseUrl: 'https://api.cohere.ai/compatibility/v1', auth: 'bearer', listModelsPath: '/models', capabilities: HOSTED_CLOUD },
|
|
36
|
+
// Perplexity has no public model-listing endpoint, so listModels resolves empty.
|
|
37
|
+
perplexity: { engine: 'openaiChat', defaultBaseUrl: 'https://api.perplexity.ai', auth: 'bearer', capabilities: HOSTED_CLOUD },
|
|
38
|
+
lmstudio: {
|
|
39
|
+
engine: 'openaiChat',
|
|
40
|
+
defaultBaseUrl: 'http://localhost:1234/v1',
|
|
41
|
+
auth: 'none',
|
|
42
|
+
listModelsPath: '/models',
|
|
43
|
+
capabilities: LOCAL_RUNTIME,
|
|
44
|
+
quirks: { keyOptional: true },
|
|
45
|
+
},
|
|
46
|
+
llamacpp: {
|
|
47
|
+
engine: 'openaiChat',
|
|
48
|
+
defaultBaseUrl: 'http://localhost:8080/v1',
|
|
49
|
+
auth: 'none',
|
|
50
|
+
listModelsPath: '/models',
|
|
51
|
+
healthPath: '/health',
|
|
52
|
+
capabilities: LOCAL_RUNTIME,
|
|
53
|
+
quirks: { keyOptional: true, modelOptional: true },
|
|
54
|
+
},
|
|
55
|
+
vllm: {
|
|
56
|
+
engine: 'openaiChat',
|
|
57
|
+
defaultBaseUrl: 'http://localhost:8000/v1',
|
|
58
|
+
auth: 'none',
|
|
59
|
+
listModelsPath: '/models',
|
|
60
|
+
capabilities: LOCAL_RUNTIME,
|
|
61
|
+
quirks: { keyOptional: true },
|
|
62
|
+
},
|
|
63
|
+
ollama: {
|
|
64
|
+
engine: 'ollama',
|
|
65
|
+
defaultBaseUrl: 'http://localhost:11434',
|
|
66
|
+
auth: 'none',
|
|
67
|
+
listModelsPath: '/api/tags',
|
|
68
|
+
// Ollama's `format: 'json'` is engine-enforced (reliable regardless of
|
|
69
|
+
// model); its `tools` field is honored only by models trained for tool
|
|
70
|
+
// use, so nativeTools stays conservative like the other local runtimes.
|
|
71
|
+
capabilities: { ...LOCAL_RUNTIME, jsonMode: true },
|
|
72
|
+
quirks: { keyOptional: true },
|
|
73
|
+
},
|
|
74
|
+
anthropic: {
|
|
75
|
+
engine: 'anthropic',
|
|
76
|
+
defaultBaseUrl: 'https://api.anthropic.com',
|
|
77
|
+
auth: 'x-api-key',
|
|
78
|
+
defaultHeaders: { 'anthropic-version': '2023-06-01' },
|
|
79
|
+
capabilities: HOSTED_CLOUD,
|
|
80
|
+
quirks: { maxTokensRequired: true },
|
|
81
|
+
},
|
|
82
|
+
google: {
|
|
83
|
+
engine: 'google',
|
|
84
|
+
defaultBaseUrl: 'https://generativelanguage.googleapis.com',
|
|
85
|
+
auth: 'x-goog-api-key',
|
|
86
|
+
capabilities: HOSTED_CLOUD,
|
|
87
|
+
},
|
|
88
|
+
'openai-compat': {
|
|
89
|
+
engine: 'openaiChat',
|
|
90
|
+
auth: 'bearer',
|
|
91
|
+
listModelsPath: '/models',
|
|
92
|
+
// Unknown endpoint — usually pointed at a local/self-hosted server, so
|
|
93
|
+
// assume the conservative local-runtime defaults rather than the hosted-cloud ones.
|
|
94
|
+
capabilities: LOCAL_RUNTIME,
|
|
95
|
+
// Most servers reached through this escape hatch (JX Runtime and other
|
|
96
|
+
// local OpenAI-compatible runtimes) don't require a key at all; keep
|
|
97
|
+
// `auth: 'bearer'` for the ones that do (a key is only sent if provided).
|
|
98
|
+
quirks: { keyOptional: true },
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
export function profileFor(provider, baseUrl) {
|
|
102
|
+
return PROVIDER_PROFILES[provider] ?? {
|
|
103
|
+
...PROVIDER_PROFILES['openai-compat'],
|
|
104
|
+
defaultBaseUrl: baseUrl,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export function applyAuth(profile, apiKey, headers) {
|
|
108
|
+
const next = { ...profile.defaultHeaders, ...headers };
|
|
109
|
+
if (!apiKey)
|
|
110
|
+
return next;
|
|
111
|
+
if (profile.auth === 'bearer')
|
|
112
|
+
next.authorization = `Bearer ${apiKey}`;
|
|
113
|
+
else if (profile.auth === 'x-api-key')
|
|
114
|
+
next['x-api-key'] = apiKey;
|
|
115
|
+
else if (profile.auth === 'x-goog-api-key')
|
|
116
|
+
next['x-goog-api-key'] = apiKey;
|
|
117
|
+
else if (profile.auth === 'api-key-header')
|
|
118
|
+
next['api-key'] = apiKey;
|
|
119
|
+
return next;
|
|
120
|
+
}
|
|
121
|
+
//#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.d.ts","sourceRoot":"","sources":["../../src/agent/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/agent/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { AIHandler } from '../handler.js';
|
|
2
|
+
import type { AIErrorKind, ChatMessage, Connection, StreamEvent, ToolCall, Usage } from '../types.js';
|
|
3
|
+
import type { ToolSpec } from './tools.js';
|
|
4
|
+
export interface AgentOptions {
|
|
5
|
+
handler: AIHandler;
|
|
6
|
+
connection: Connection;
|
|
7
|
+
model: string;
|
|
8
|
+
tools: ToolSpec[];
|
|
9
|
+
messages: ChatMessage[];
|
|
10
|
+
/**
|
|
11
|
+
* `native` sends `tools` on the request; `promptJson` describes tools in a
|
|
12
|
+
* system message and parses a JSON directive back out of plain text.
|
|
13
|
+
* `auto` (the default) picks per-call from the connection's provider
|
|
14
|
+
* capability profile (`profileFor(provider).capabilities.nativeTools`) —
|
|
15
|
+
* hosted providers with reliable tool-calling get `native`, local runtimes
|
|
16
|
+
* (Ollama, llama.cpp, LM Studio, vLLM) and the `openai-compat` escape hatch
|
|
17
|
+
* default to `promptJson` since native tool support there is model-
|
|
18
|
+
* dependent, not protocol-guaranteed.
|
|
19
|
+
*/
|
|
20
|
+
toolMode?: 'native' | 'promptJson' | 'auto';
|
|
21
|
+
/**
|
|
22
|
+
* Discovered capabilities of the specific model (e.g. Ollama `/api/show`
|
|
23
|
+
* `["tools"]`). When `toolMode` is `auto`, a `tools` capability here upgrades a
|
|
24
|
+
* local model to `native` tool-calling instead of the profile's conservative
|
|
25
|
+
* `promptJson` default — so a capable local model isn't forced onto the fallback.
|
|
26
|
+
*/
|
|
27
|
+
modelCapabilities?: string[];
|
|
28
|
+
/** Sampler/decoding options forwarded to every model turn (parity with a plain chat call). */
|
|
29
|
+
temperature?: number;
|
|
30
|
+
maxTokens?: number;
|
|
31
|
+
topP?: number;
|
|
32
|
+
stopSequences?: string[];
|
|
33
|
+
/** Provider-native passthrough forwarded to every turn — see {@link ChatRequest.providerOptions}. */
|
|
34
|
+
providerOptions?: Record<string, unknown>;
|
|
35
|
+
budget?: {
|
|
36
|
+
maxSteps?: number;
|
|
37
|
+
maxTokens?: number;
|
|
38
|
+
deadlineMs?: number;
|
|
39
|
+
};
|
|
40
|
+
approval?: ApprovalGate;
|
|
41
|
+
/**
|
|
42
|
+
* Which tools require the approval gate. `sideEffectsOnly` (default) gates only
|
|
43
|
+
* tools declared `sideEffects: true`; `all` gates every tool — so `tool_denied`
|
|
44
|
+
* is reachable without marking each tool, and a consumer can require approval
|
|
45
|
+
* for a whole run in one place.
|
|
46
|
+
*/
|
|
47
|
+
approvalMode?: 'sideEffectsOnly' | 'all';
|
|
48
|
+
/**
|
|
49
|
+
* Bounds and framing applied to each tool result before it re-enters the
|
|
50
|
+
* model's context. `maxChars` truncates oversized results (with a notice) so a
|
|
51
|
+
* single large return can't blow the next turn's window; `wrapUntrusted` fences
|
|
52
|
+
* the result in an `<untrusted_tool_output>` envelope. Both are opt-in.
|
|
53
|
+
*/
|
|
54
|
+
toolResult?: {
|
|
55
|
+
maxChars?: number;
|
|
56
|
+
wrapUntrusted?: boolean;
|
|
57
|
+
};
|
|
58
|
+
onEvent?: (e: AgentEvent) => void;
|
|
59
|
+
signal?: AbortSignal;
|
|
60
|
+
metadata?: Record<string, unknown>;
|
|
61
|
+
}
|
|
62
|
+
export type ApprovalGate = (req: {
|
|
63
|
+
call: ToolCall;
|
|
64
|
+
tool: ToolSpec;
|
|
65
|
+
step: number;
|
|
66
|
+
}) => Promise<'allow' | 'deny' | {
|
|
67
|
+
modifiedArguments: unknown;
|
|
68
|
+
}>;
|
|
69
|
+
export type AgentEvent = StreamEvent | {
|
|
70
|
+
type: 'step_start';
|
|
71
|
+
step: number;
|
|
72
|
+
} | {
|
|
73
|
+
type: 'tool_mode';
|
|
74
|
+
mode: 'native' | 'promptJson';
|
|
75
|
+
} | {
|
|
76
|
+
type: 'tool_start';
|
|
77
|
+
step: number;
|
|
78
|
+
call: ToolCall;
|
|
79
|
+
} | {
|
|
80
|
+
type: 'tool_result';
|
|
81
|
+
step: number;
|
|
82
|
+
call: ToolCall;
|
|
83
|
+
result: unknown;
|
|
84
|
+
isError: boolean;
|
|
85
|
+
} | {
|
|
86
|
+
type: 'tool_denied';
|
|
87
|
+
step: number;
|
|
88
|
+
call: ToolCall;
|
|
89
|
+
reason: string;
|
|
90
|
+
} | {
|
|
91
|
+
type: 'agent_done';
|
|
92
|
+
result: AgentResult;
|
|
93
|
+
};
|
|
94
|
+
export interface AgentResult {
|
|
95
|
+
finalText: string;
|
|
96
|
+
messages: ChatMessage[];
|
|
97
|
+
usage: Usage;
|
|
98
|
+
steps: number;
|
|
99
|
+
stopReason: 'finished' | 'max_steps' | 'budget' | 'deadline' | 'canceled' | 'error';
|
|
100
|
+
/**
|
|
101
|
+
* Populated when `stopReason` is `error`: the failure that ended the run, so
|
|
102
|
+
* the caller can see what went wrong without replaying the event stream.
|
|
103
|
+
*/
|
|
104
|
+
error?: {
|
|
105
|
+
kind: AIErrorKind;
|
|
106
|
+
message: string;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export declare function runAgent(opts: AgentOptions): AsyncIterable<AgentEvent> & {
|
|
110
|
+
result: Promise<AgentResult>;
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=loop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop.d.ts","sourceRoot":"","sources":["../../src/agent/loop.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,QAA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MAAM,CAAA;KAAE,CAAC;CAChD;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,aAAa,CAAC,UAAU,CAAC,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC,CAAA;CAAE,CAOzG"}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { profileFor } from '../adapters/profiles.js';
|
|
2
|
+
import { AIError } from '../errors.js';
|
|
3
|
+
import { extractJson } from '../json.js';
|
|
4
|
+
import { mergeUsage } from '../tokens.js';
|
|
5
|
+
import { asRecord, sleep } from '../util.js';
|
|
6
|
+
import { validateToolArgs } from './tools.js';
|
|
7
|
+
export function runAgent(opts) {
|
|
8
|
+
let resolveResult;
|
|
9
|
+
const result = new Promise((resolve) => {
|
|
10
|
+
resolveResult = resolve;
|
|
11
|
+
});
|
|
12
|
+
const iterable = run(opts, resolveResult);
|
|
13
|
+
return Object.assign(iterable, { result });
|
|
14
|
+
}
|
|
15
|
+
async function* run(opts, resolveResult) {
|
|
16
|
+
const messages = [...opts.messages];
|
|
17
|
+
const toolMode = resolveToolMode(opts);
|
|
18
|
+
const maxSteps = opts.budget?.maxSteps ?? 8;
|
|
19
|
+
const agentSignal = createAgentSignal(opts.signal, opts.budget?.deadlineMs);
|
|
20
|
+
let usage;
|
|
21
|
+
let finalText = '';
|
|
22
|
+
let step = 0;
|
|
23
|
+
let settled = false;
|
|
24
|
+
// Disclose the resolved tool protocol once up front (a silent promptJson
|
|
25
|
+
// fallback was the friction behind FR‑2).
|
|
26
|
+
yield emit(opts, { type: 'tool_mode', mode: toolMode });
|
|
27
|
+
try {
|
|
28
|
+
while (step < maxSteps) {
|
|
29
|
+
step += 1;
|
|
30
|
+
if (agentSignal.signal?.aborted)
|
|
31
|
+
return yield* yieldDone(agentSignal.timedOut() ? 'deadline' : 'canceled');
|
|
32
|
+
yield emit(opts, { type: 'step_start', step });
|
|
33
|
+
const calls = [];
|
|
34
|
+
let stepText = '';
|
|
35
|
+
let streamFailure;
|
|
36
|
+
// In promptJson mode a tool directive must not leak into the visible
|
|
37
|
+
// stream, so directive-looking text is withheld from `delta` events (still
|
|
38
|
+
// accumulated in `stepText` for parsing). Prose streams normally.
|
|
39
|
+
const filter = toolMode === 'promptJson' ? new PromptJsonDeltaFilter() : undefined;
|
|
40
|
+
const requestMessages = toolMode === 'promptJson' ? withPromptJsonInstruction(messages, opts.tools) : messages;
|
|
41
|
+
for await (const event of opts.handler.stream(opts.connection, {
|
|
42
|
+
model: opts.model,
|
|
43
|
+
messages: requestMessages,
|
|
44
|
+
tools: toolMode === 'promptJson' ? undefined : opts.tools,
|
|
45
|
+
temperature: opts.temperature,
|
|
46
|
+
maxTokens: opts.maxTokens,
|
|
47
|
+
topP: opts.topP,
|
|
48
|
+
stopSequences: opts.stopSequences,
|
|
49
|
+
providerOptions: opts.providerOptions,
|
|
50
|
+
signal: agentSignal.signal,
|
|
51
|
+
metadata: { ...opts.metadata, agentStep: step },
|
|
52
|
+
})) {
|
|
53
|
+
if (event.type === 'delta') {
|
|
54
|
+
stepText += event.text;
|
|
55
|
+
if (filter) {
|
|
56
|
+
const visible = filter.push(event.text);
|
|
57
|
+
if (visible)
|
|
58
|
+
yield emit(opts, { type: 'delta', text: visible });
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (event.type === 'tool_call')
|
|
63
|
+
calls.push(event.call);
|
|
64
|
+
if (event.type === 'error')
|
|
65
|
+
streamFailure = event.error;
|
|
66
|
+
if (event.type === 'done') {
|
|
67
|
+
usage = usage ? mergeUsage(usage, event.result.usage) : event.result.usage;
|
|
68
|
+
if (toolMode === 'promptJson' && calls.length === 0)
|
|
69
|
+
calls.push(...callsFromPromptJson(stepText));
|
|
70
|
+
}
|
|
71
|
+
yield emit(opts, event);
|
|
72
|
+
}
|
|
73
|
+
// If withheld text turned out not to be a real directive, flush it so the
|
|
74
|
+
// user still sees the model's answer.
|
|
75
|
+
if (filter && calls.length === 0) {
|
|
76
|
+
const flushed = filter.flush();
|
|
77
|
+
if (flushed)
|
|
78
|
+
yield emit(opts, { type: 'delta', text: flushed });
|
|
79
|
+
}
|
|
80
|
+
// A handler-level failure (auth, policy, cancel, exhausted retries) must
|
|
81
|
+
// stop the loop honestly rather than looking like an empty completion.
|
|
82
|
+
if (streamFailure)
|
|
83
|
+
return yield* yieldDone(agentSignal.timedOut() ? 'deadline' : streamFailure.kind === 'canceled' ? 'canceled' : 'error', errorInfo(streamFailure));
|
|
84
|
+
finalText = stepText;
|
|
85
|
+
messages.push(toolMode === 'promptJson'
|
|
86
|
+
? { role: 'assistant', content: stepText }
|
|
87
|
+
: { role: 'assistant', content: stepText, toolCalls: calls.length ? calls : undefined });
|
|
88
|
+
if (calls.length === 0)
|
|
89
|
+
return yield* yieldDone('finished');
|
|
90
|
+
for (const call of calls) {
|
|
91
|
+
if (agentSignal.signal?.aborted)
|
|
92
|
+
return yield* yieldDone(agentSignal.timedOut() ? 'deadline' : 'canceled');
|
|
93
|
+
const tool = opts.tools.find((candidate) => candidate.name === call.name);
|
|
94
|
+
if (!tool) {
|
|
95
|
+
yield* appendToolError(opts, messages, step, call, `Unknown tool: ${call.name}`);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const validation = validateToolArgs(tool, call);
|
|
99
|
+
if (!validation.ok) {
|
|
100
|
+
yield* appendToolError(opts, messages, step, call, validation.message);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
let args = validation.args;
|
|
104
|
+
const requiresApproval = tool.sideEffects || opts.approvalMode === 'all';
|
|
105
|
+
if (requiresApproval) {
|
|
106
|
+
if (!opts.approval) {
|
|
107
|
+
yield* appendToolDenied(opts, messages, step, call, 'No approval gate configured');
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const approval = await opts.approval({ call, tool, step });
|
|
111
|
+
if (agentSignal.signal?.aborted)
|
|
112
|
+
return yield* yieldDone(agentSignal.timedOut() ? 'deadline' : 'canceled');
|
|
113
|
+
if (approval === 'deny') {
|
|
114
|
+
yield* appendToolDenied(opts, messages, step, call, 'Denied by approval gate');
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (approval !== 'allow') {
|
|
118
|
+
const modified = validateToolArgs(tool, { ...call, arguments: approval.modifiedArguments });
|
|
119
|
+
if (!modified.ok) {
|
|
120
|
+
yield* appendToolError(opts, messages, step, call, modified.message);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
args = modified.args;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
yield emit(opts, { type: 'tool_start', step, call });
|
|
127
|
+
let executed;
|
|
128
|
+
try {
|
|
129
|
+
const result = await tool.execute(args, {
|
|
130
|
+
signal: agentSignal.signal ?? new AbortController().signal,
|
|
131
|
+
callId: call.id,
|
|
132
|
+
step,
|
|
133
|
+
metadata: opts.metadata,
|
|
134
|
+
});
|
|
135
|
+
executed = { ok: true, result };
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
executed = { ok: false, message: error instanceof Error ? error.message : 'Tool failed' };
|
|
139
|
+
}
|
|
140
|
+
if (!executed.ok) {
|
|
141
|
+
yield* appendToolError(opts, messages, step, call, executed.message);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
// Guard against a tool returning `undefined` (JSON.stringify(undefined)
|
|
146
|
+
// is the value `undefined`, not a string) — an unguarded value here
|
|
147
|
+
// used to crash the next step's message mapper and kill the whole run.
|
|
148
|
+
const serialized = JSON.stringify(executed.result) ?? 'null';
|
|
149
|
+
messages.push({ role: 'tool', toolCallId: call.id, name: call.name, content: formatToolResult(serialized, opts.toolResult) });
|
|
150
|
+
// A tool that returns a structured `{ ok: false, ... }` is a recoverable
|
|
151
|
+
// error the model should react to — surface it as an error result.
|
|
152
|
+
const record = asRecord(executed.result);
|
|
153
|
+
const isError = record?.ok === false;
|
|
154
|
+
yield emit(opts, { type: 'tool_result', step, call, result: executed.result, isError });
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
const message = error instanceof Error ? error.message : 'Tool result was not serializable';
|
|
158
|
+
yield* appendToolError(opts, messages, step, call, message);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const totalTokens = (usage?.inputTokens ?? 0) + (usage?.outputTokens ?? 0);
|
|
162
|
+
if (opts.budget?.maxTokens && totalTokens > opts.budget.maxTokens)
|
|
163
|
+
return yield* yieldDone('budget');
|
|
164
|
+
await sleep(0, agentSignal.signal);
|
|
165
|
+
}
|
|
166
|
+
return yield* yieldDone('max_steps');
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
const stopReason = agentSignal.timedOut() ? 'deadline' : (agentSignal.signal?.aborted || (error instanceof AIError && error.kind === 'canceled')) ? 'canceled' : 'error';
|
|
170
|
+
const result = makeResult(finalText, messages, usage ?? { estimated: true }, step, stopReason, stopReason === 'error' ? errorInfo(error) : undefined);
|
|
171
|
+
settled = true;
|
|
172
|
+
resolveResult(result);
|
|
173
|
+
yield emit(opts, { type: 'agent_done', result });
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
agentSignal.dispose();
|
|
177
|
+
if (!settled) {
|
|
178
|
+
const result = makeResult(finalText, messages, usage ?? { estimated: true }, step, 'canceled');
|
|
179
|
+
resolveResult(result);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function* yieldDone(stopReason, error) {
|
|
183
|
+
const result = makeResult(finalText, messages, usage ?? { estimated: true }, step, stopReason, error);
|
|
184
|
+
settled = true;
|
|
185
|
+
resolveResult(result);
|
|
186
|
+
yield emit(opts, { type: 'agent_done', result });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function emit(opts, event) {
|
|
190
|
+
opts.onEvent?.(event);
|
|
191
|
+
return event;
|
|
192
|
+
}
|
|
193
|
+
function makeResult(finalText, messages, usage, steps, stopReason, error) {
|
|
194
|
+
return error ? { finalText, messages, usage, steps, stopReason, error } : { finalText, messages, usage, steps, stopReason };
|
|
195
|
+
}
|
|
196
|
+
function errorInfo(error) {
|
|
197
|
+
if (error instanceof AIError)
|
|
198
|
+
return { kind: error.kind, message: error.message };
|
|
199
|
+
return { kind: 'tool_error', message: error instanceof Error ? error.message : 'Agent run failed' };
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Applies the optional size cap and untrusted-content framing to a serialized
|
|
203
|
+
* tool result before it re-enters the model's context.
|
|
204
|
+
*/
|
|
205
|
+
function formatToolResult(serialized, opts) {
|
|
206
|
+
let out = serialized;
|
|
207
|
+
const max = opts?.maxChars;
|
|
208
|
+
if (max !== undefined && out.length > max) {
|
|
209
|
+
out = `${out.slice(0, max)}\n…[truncated ${out.length - max} chars]`;
|
|
210
|
+
}
|
|
211
|
+
if (opts?.wrapUntrusted) {
|
|
212
|
+
out = `<untrusted_tool_output>\n${out}\n</untrusted_tool_output>`;
|
|
213
|
+
}
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Withholds a promptJson tool directive from the visible `delta` stream. Once
|
|
218
|
+
* the leading non-whitespace character is known: a `{`, `[`, or fence marks a
|
|
219
|
+
* directive candidate whose text is buffered (never streamed); anything else is
|
|
220
|
+
* prose that streams through untouched. `flush()` returns any buffered text when
|
|
221
|
+
* the stream ended without yielding a parsed directive.
|
|
222
|
+
*/
|
|
223
|
+
class PromptJsonDeltaFilter {
|
|
224
|
+
decided = false;
|
|
225
|
+
withhold = false;
|
|
226
|
+
buffer = '';
|
|
227
|
+
push(text) {
|
|
228
|
+
if (!this.decided) {
|
|
229
|
+
this.buffer += text;
|
|
230
|
+
const lead = this.buffer.replace(/^\s+/, '');
|
|
231
|
+
if (lead.length === 0)
|
|
232
|
+
return '';
|
|
233
|
+
this.decided = true;
|
|
234
|
+
this.withhold = /^[[{`]/.test(lead);
|
|
235
|
+
if (this.withhold)
|
|
236
|
+
return '';
|
|
237
|
+
const flushed = this.buffer;
|
|
238
|
+
this.buffer = '';
|
|
239
|
+
return flushed;
|
|
240
|
+
}
|
|
241
|
+
if (this.withhold) {
|
|
242
|
+
this.buffer += text;
|
|
243
|
+
return '';
|
|
244
|
+
}
|
|
245
|
+
return text;
|
|
246
|
+
}
|
|
247
|
+
flush() {
|
|
248
|
+
if (!this.withhold)
|
|
249
|
+
return '';
|
|
250
|
+
const out = this.buffer;
|
|
251
|
+
this.buffer = '';
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function* appendToolError(opts, messages, step, call, message) {
|
|
256
|
+
const result = { error: message };
|
|
257
|
+
messages.push({ role: 'tool', toolCallId: call.id, name: call.name, content: JSON.stringify(result) });
|
|
258
|
+
yield emit(opts, { type: 'tool_result', step, call, result, isError: true });
|
|
259
|
+
}
|
|
260
|
+
function* appendToolDenied(opts, messages, step, call, reason) {
|
|
261
|
+
messages.push({ role: 'tool', toolCallId: call.id, name: call.name, content: JSON.stringify({ denied: true, reason }) });
|
|
262
|
+
yield emit(opts, { type: 'tool_denied', step, call, reason });
|
|
263
|
+
}
|
|
264
|
+
function resolveToolMode(opts) {
|
|
265
|
+
const mode = opts.toolMode ?? 'auto';
|
|
266
|
+
if (mode !== 'auto')
|
|
267
|
+
return mode;
|
|
268
|
+
// A model that actually advertises tool support gets native tool-calling even
|
|
269
|
+
// on a local runtime whose profile is conservatively `nativeTools: false`.
|
|
270
|
+
if (opts.modelCapabilities?.includes('tools'))
|
|
271
|
+
return 'native';
|
|
272
|
+
return profileFor(opts.connection.provider, opts.connection.baseUrl).capabilities.nativeTools ? 'native' : 'promptJson';
|
|
273
|
+
}
|
|
274
|
+
function withPromptJsonInstruction(messages, tools) {
|
|
275
|
+
// Include each tool's parameter schema, not just its description, so the model
|
|
276
|
+
// knows the argument shape instead of guessing it (FR‑3).
|
|
277
|
+
const catalog = tools
|
|
278
|
+
.map((tool) => `- ${tool.name}: ${tool.description}\n parameters: ${JSON.stringify(tool.parameters)}`)
|
|
279
|
+
.join('\n');
|
|
280
|
+
return [
|
|
281
|
+
{
|
|
282
|
+
role: 'system',
|
|
283
|
+
content: `When you need tools, respond only with JSON. For one tool: {"tool":"name","input":{...}}. For several in one turn: {"tools":[{"tool":"name","input":{...}}]}. The "input" object must match the tool's parameters schema. Available tools:\n${catalog}`,
|
|
284
|
+
},
|
|
285
|
+
...messages.map(toPromptJsonMessage),
|
|
286
|
+
];
|
|
287
|
+
}
|
|
288
|
+
function toPromptJsonMessage(message) {
|
|
289
|
+
if (message.role === 'tool') {
|
|
290
|
+
return {
|
|
291
|
+
role: 'user',
|
|
292
|
+
content: `Tool ${message.name ?? message.toolCallId ?? 'unknown'} returned: ${textContent(message.content)}`,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
role: message.role,
|
|
297
|
+
content: textContent(message.content),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Parses a promptJson tool directive into zero or more calls. Accepts the single
|
|
302
|
+
* form ({"tool","input"}), the batched form ({"tools":[...]}), and a bare array
|
|
303
|
+
* of directives, so a promptJson-mode model can request several tools per turn
|
|
304
|
+
* just like native tool-calling can. Malformed entries are skipped, not thrown.
|
|
305
|
+
*/
|
|
306
|
+
function callsFromPromptJson(text) {
|
|
307
|
+
const value = extractJson(text);
|
|
308
|
+
if (!value || typeof value !== 'object')
|
|
309
|
+
return [];
|
|
310
|
+
const entries = Array.isArray(value)
|
|
311
|
+
? value
|
|
312
|
+
: Array.isArray(value.tools)
|
|
313
|
+
? value.tools
|
|
314
|
+
: [value];
|
|
315
|
+
const calls = [];
|
|
316
|
+
for (const entry of entries) {
|
|
317
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry))
|
|
318
|
+
continue;
|
|
319
|
+
const record = entry;
|
|
320
|
+
if (typeof record.tool !== 'string')
|
|
321
|
+
continue;
|
|
322
|
+
calls.push({
|
|
323
|
+
id: randomId(),
|
|
324
|
+
name: record.tool,
|
|
325
|
+
arguments: record.input && typeof record.input === 'object' ? record.input : {},
|
|
326
|
+
raw: JSON.stringify(record.input ?? {}),
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
return calls;
|
|
330
|
+
}
|
|
331
|
+
function randomId() {
|
|
332
|
+
return globalThis.crypto?.randomUUID?.() ?? `tool_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
333
|
+
}
|
|
334
|
+
function textContent(content) {
|
|
335
|
+
if (typeof content === 'string')
|
|
336
|
+
return content;
|
|
337
|
+
if (!content)
|
|
338
|
+
return '';
|
|
339
|
+
return content.filter((part) => part.type === 'text').map((part) => part.text ?? '').join('\n');
|
|
340
|
+
}
|
|
341
|
+
function createAgentSignal(signal, deadlineMs) {
|
|
342
|
+
if (!signal && deadlineMs === undefined)
|
|
343
|
+
return { signal: undefined, timedOut: () => false, dispose: () => undefined };
|
|
344
|
+
const controller = new AbortController();
|
|
345
|
+
let didTimeOut = false;
|
|
346
|
+
let timer;
|
|
347
|
+
const onAbort = () => controller.abort(signal?.reason);
|
|
348
|
+
if (signal) {
|
|
349
|
+
if (signal.aborted)
|
|
350
|
+
onAbort();
|
|
351
|
+
else
|
|
352
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
353
|
+
}
|
|
354
|
+
if (deadlineMs !== undefined) {
|
|
355
|
+
timer = setTimeout(() => {
|
|
356
|
+
didTimeOut = true;
|
|
357
|
+
controller.abort(new AIError(`Agent deadline exceeded after ${deadlineMs}ms`, { kind: 'budget_exceeded', retryable: false }));
|
|
358
|
+
}, deadlineMs);
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
signal: controller.signal,
|
|
362
|
+
timedOut: () => didTimeOut,
|
|
363
|
+
dispose() {
|
|
364
|
+
if (timer)
|
|
365
|
+
clearTimeout(timer);
|
|
366
|
+
signal?.removeEventListener('abort', onAbort);
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
//# sourceMappingURL=loop.js.map
|