@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.
Files changed (183) hide show
  1. package/README.md +134 -271
  2. package/UPGRADING.md +158 -0
  3. package/dist/adapters/engines/anthropic.d.ts +7 -1
  4. package/dist/adapters/engines/anthropic.js +64 -37
  5. package/dist/adapters/engines/base.d.ts +29 -2
  6. package/dist/adapters/engines/base.js +52 -8
  7. package/dist/adapters/engines/google.d.ts +17 -1
  8. package/dist/adapters/engines/google.js +93 -29
  9. package/dist/adapters/engines/ollama.d.ts +2 -1
  10. package/dist/adapters/engines/ollama.js +51 -15
  11. package/dist/adapters/engines/openaiChat.d.ts +2 -1
  12. package/dist/adapters/engines/openaiChat.js +60 -30
  13. package/dist/adapters/profiles.d.ts +21 -1
  14. package/dist/adapters/profiles.js +12 -2
  15. package/dist/agent/loop.d.ts +43 -1
  16. package/dist/agent/loop.js +120 -13
  17. package/dist/cjs/adapters/engines/anthropic.js +308 -0
  18. package/dist/cjs/adapters/engines/base.js +147 -0
  19. package/dist/cjs/adapters/engines/google.js +284 -0
  20. package/dist/cjs/adapters/engines/ollama.js +199 -0
  21. package/dist/cjs/adapters/engines/openaiChat.js +261 -0
  22. package/dist/cjs/adapters/index.js +22 -0
  23. package/dist/cjs/adapters/profiles.js +125 -0
  24. package/dist/cjs/agent/index.js +18 -0
  25. package/dist/cjs/agent/loop.js +372 -0
  26. package/dist/cjs/agent/tools.js +51 -0
  27. package/dist/cjs/connect.js +27 -0
  28. package/dist/cjs/errors.js +110 -0
  29. package/dist/cjs/handler.js +561 -0
  30. package/dist/cjs/index.js +27 -0
  31. package/dist/cjs/json.js +121 -0
  32. package/dist/cjs/keys.js +81 -0
  33. package/dist/cjs/package.json +3 -0
  34. package/dist/cjs/policy.js +42 -0
  35. package/dist/cjs/redact.js +78 -0
  36. package/dist/cjs/tokens.js +22 -0
  37. package/dist/cjs/transport.js +172 -0
  38. package/dist/cjs/types.js +6 -0
  39. package/dist/cjs/util.js +112 -0
  40. package/dist/connect.d.ts +31 -0
  41. package/dist/connect.js +25 -0
  42. package/dist/errors.js +25 -1
  43. package/dist/handler.d.ts +61 -1
  44. package/dist/handler.js +247 -61
  45. package/dist/index.d.ts +1 -0
  46. package/dist/index.js +1 -0
  47. package/dist/keys.js +1 -4
  48. package/dist/redact.d.ts +18 -0
  49. package/dist/redact.js +29 -1
  50. package/dist/transport.d.ts +3 -1
  51. package/dist/transport.js +21 -1
  52. package/dist/types.d.ts +96 -2
  53. package/dist/util.d.ts +16 -0
  54. package/dist/util.js +50 -0
  55. package/nugget/VERSION.txt +2 -2
  56. package/nugget/dist/adapters/engines/anthropic.d.ts +14 -0
  57. package/nugget/dist/adapters/engines/anthropic.d.ts.map +1 -0
  58. package/nugget/dist/adapters/engines/anthropic.js +305 -0
  59. package/nugget/dist/adapters/engines/anthropic.js.map +1 -0
  60. package/nugget/dist/adapters/engines/base.d.ts +50 -0
  61. package/nugget/dist/adapters/engines/base.d.ts.map +1 -0
  62. package/nugget/dist/adapters/engines/base.js +137 -0
  63. package/nugget/dist/adapters/engines/base.js.map +1 -0
  64. package/nugget/dist/adapters/engines/google.d.ts +24 -0
  65. package/nugget/dist/adapters/engines/google.d.ts.map +1 -0
  66. package/nugget/dist/adapters/engines/google.js +281 -0
  67. package/nugget/dist/adapters/engines/google.js.map +1 -0
  68. package/nugget/dist/adapters/engines/ollama.d.ts +11 -0
  69. package/nugget/dist/adapters/engines/ollama.d.ts.map +1 -0
  70. package/nugget/dist/adapters/engines/ollama.js +196 -0
  71. package/nugget/dist/adapters/engines/ollama.js.map +1 -0
  72. package/nugget/dist/adapters/engines/openaiChat.d.ts +16 -0
  73. package/nugget/dist/adapters/engines/openaiChat.d.ts.map +1 -0
  74. package/nugget/dist/adapters/engines/openaiChat.js +258 -0
  75. package/nugget/dist/adapters/engines/openaiChat.js.map +1 -0
  76. package/nugget/dist/adapters/index.d.ts +5 -0
  77. package/nugget/dist/adapters/index.d.ts.map +1 -0
  78. package/nugget/dist/adapters/index.js +17 -0
  79. package/nugget/dist/adapters/index.js.map +1 -0
  80. package/nugget/dist/adapters/profiles.d.ts +62 -0
  81. package/nugget/dist/adapters/profiles.d.ts.map +1 -0
  82. package/nugget/dist/adapters/profiles.js +121 -0
  83. package/nugget/dist/adapters/profiles.js.map +1 -0
  84. package/nugget/dist/agent/index.d.ts +3 -0
  85. package/nugget/dist/agent/index.d.ts.map +1 -0
  86. package/nugget/dist/agent/index.js +3 -0
  87. package/nugget/dist/agent/index.js.map +1 -0
  88. package/nugget/dist/agent/loop.d.ts +112 -0
  89. package/nugget/dist/agent/loop.d.ts.map +1 -0
  90. package/nugget/dist/agent/loop.js +370 -0
  91. package/nugget/dist/agent/loop.js.map +1 -0
  92. package/nugget/dist/agent/tools.d.ts +29 -0
  93. package/nugget/dist/agent/tools.d.ts.map +1 -0
  94. package/nugget/dist/agent/tools.js +48 -0
  95. package/nugget/dist/agent/tools.js.map +1 -0
  96. package/nugget/dist/cjs/adapters/engines/anthropic.js +308 -0
  97. package/nugget/dist/cjs/adapters/engines/base.js +147 -0
  98. package/nugget/dist/cjs/adapters/engines/google.js +284 -0
  99. package/nugget/dist/cjs/adapters/engines/ollama.js +199 -0
  100. package/nugget/dist/cjs/adapters/engines/openaiChat.js +261 -0
  101. package/nugget/dist/cjs/adapters/index.js +22 -0
  102. package/nugget/dist/cjs/adapters/profiles.js +125 -0
  103. package/nugget/dist/cjs/agent/index.js +18 -0
  104. package/nugget/dist/cjs/agent/loop.js +372 -0
  105. package/nugget/dist/cjs/agent/tools.js +51 -0
  106. package/nugget/dist/cjs/connect.js +27 -0
  107. package/nugget/dist/cjs/errors.js +110 -0
  108. package/nugget/dist/cjs/handler.js +561 -0
  109. package/nugget/dist/cjs/index.js +27 -0
  110. package/nugget/dist/cjs/json.js +121 -0
  111. package/nugget/dist/cjs/keys.js +81 -0
  112. package/nugget/dist/cjs/package.json +3 -0
  113. package/nugget/dist/cjs/policy.js +42 -0
  114. package/nugget/dist/cjs/redact.js +78 -0
  115. package/nugget/dist/cjs/tokens.js +22 -0
  116. package/nugget/dist/cjs/transport.js +172 -0
  117. package/nugget/dist/cjs/types.js +6 -0
  118. package/nugget/dist/cjs/util.js +112 -0
  119. package/nugget/dist/connect.d.ts +31 -0
  120. package/nugget/dist/connect.d.ts.map +1 -0
  121. package/nugget/dist/connect.js +25 -0
  122. package/nugget/dist/connect.js.map +1 -0
  123. package/nugget/dist/errors.d.ts +22 -0
  124. package/nugget/dist/errors.d.ts.map +1 -0
  125. package/nugget/dist/errors.js +104 -0
  126. package/nugget/dist/errors.js.map +1 -0
  127. package/nugget/dist/handler.d.ts +108 -0
  128. package/nugget/dist/handler.d.ts.map +1 -0
  129. package/nugget/dist/handler.js +557 -0
  130. package/nugget/dist/handler.js.map +1 -0
  131. package/nugget/dist/index.d.ts +12 -0
  132. package/nugget/dist/index.d.ts.map +1 -0
  133. package/nugget/dist/index.js +12 -0
  134. package/nugget/dist/index.js.map +1 -0
  135. package/nugget/dist/json.d.ts +7 -0
  136. package/nugget/dist/json.d.ts.map +1 -0
  137. package/nugget/dist/json.js +114 -0
  138. package/nugget/dist/json.js.map +1 -0
  139. package/nugget/dist/keys.d.ts +7 -0
  140. package/nugget/dist/keys.d.ts.map +1 -0
  141. package/nugget/dist/keys.js +75 -0
  142. package/nugget/dist/keys.js.map +1 -0
  143. package/nugget/dist/policy.d.ts +6 -0
  144. package/nugget/dist/policy.d.ts.map +1 -0
  145. package/nugget/dist/policy.js +37 -0
  146. package/nugget/dist/policy.js.map +1 -0
  147. package/nugget/dist/redact.d.ts +26 -0
  148. package/nugget/dist/redact.d.ts.map +1 -0
  149. package/nugget/dist/redact.js +74 -0
  150. package/nugget/dist/redact.js.map +1 -0
  151. package/nugget/dist/tokens.d.ts +5 -0
  152. package/nugget/dist/tokens.d.ts.map +1 -0
  153. package/nugget/dist/tokens.js +18 -0
  154. package/nugget/dist/tokens.js.map +1 -0
  155. package/nugget/dist/transport.d.ts +30 -0
  156. package/nugget/dist/transport.d.ts.map +1 -0
  157. package/nugget/dist/transport.js +165 -0
  158. package/nugget/dist/transport.js.map +1 -0
  159. package/nugget/dist/types.d.ts +285 -0
  160. package/nugget/dist/types.d.ts.map +1 -0
  161. package/nugget/dist/types.js +4 -0
  162. package/nugget/dist/types.js.map +1 -0
  163. package/nugget/dist/util.d.ts +24 -0
  164. package/nugget/dist/util.d.ts.map +1 -0
  165. package/nugget/dist/util.js +101 -0
  166. package/nugget/dist/util.js.map +1 -0
  167. package/nugget/src/adapters/engines/anthropic.ts +62 -37
  168. package/nugget/src/adapters/engines/base.ts +50 -7
  169. package/nugget/src/adapters/engines/google.ts +92 -29
  170. package/nugget/src/adapters/engines/ollama.ts +51 -17
  171. package/nugget/src/adapters/engines/openaiChat.ts +61 -32
  172. package/nugget/src/adapters/profiles.ts +53 -3
  173. package/nugget/src/agent/loop.ts +158 -14
  174. package/nugget/src/connect.ts +48 -0
  175. package/nugget/src/errors.ts +25 -1
  176. package/nugget/src/handler.ts +286 -59
  177. package/nugget/src/index.ts +1 -0
  178. package/nugget/src/keys.ts +1 -5
  179. package/nugget/src/redact.ts +32 -1
  180. package/nugget/src/transport.ts +20 -1
  181. package/nugget/src/types.ts +96 -1
  182. package/nugget/src/util.ts +55 -0
  183. package/package.json +20 -6
package/dist/redact.js CHANGED
@@ -1,4 +1,8 @@
1
- const SECRET_PATTERNS = [
1
+ /**
2
+ * Prefixed secret formats. Each pattern is anchored on a vendor prefix that is
3
+ * distinctive enough that a match is a secret, not a coincidence.
4
+ */
5
+ export const SECRET_PATTERNS = [
2
6
  /sk-[A-Za-z0-9_-]{20,}/g,
3
7
  /sk-proj-[A-Za-z0-9_-]{20,}/g,
4
8
  /sk-ant-[A-Za-z0-9_-]{20,}/g,
@@ -20,6 +24,27 @@ const SECRET_PATTERNS = [
20
24
  /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g,
21
25
  /eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,
22
26
  ];
27
+ /**
28
+ * Unprefixed/opaque secrets — Azure OpenAI `api-key` values, AWS *secret*
29
+ * access keys, generic bearer/session tokens — carry no distinctive prefix, so
30
+ * matching them on shape alone would redact commit SHAs, content hashes, and
31
+ * base64 payloads. These patterns instead anchor on the *label* that precedes
32
+ * the value (`api-key: …`, `"aws_secret_access_key": "…"`, `password=…`) and
33
+ * redact only the value, keeping the label readable in logs.
34
+ *
35
+ * Exact-match registration (`SessionRedactor.addSecret`, which the handler does
36
+ * for every resolved key) remains the only guaranteed catch for a bare,
37
+ * unlabeled, unprefixed secret.
38
+ */
39
+ export const LABELED_SECRET_PATTERNS = [
40
+ new RegExp(
41
+ // Lookbehind rather than `\b` so an underscore-joined prefix still matches
42
+ // the label (`aws_secret_access_key`, `azure_api_key`).
43
+ String.raw `(?<![A-Za-z0-9])(api[_-]?key|apikey|secret[_-]?access[_-]?key|access[_-]?token|auth[_-]?token|session[_-]?token|client[_-]?secret|refresh[_-]?token|private[_-]?key|password|passwd)` +
44
+ // Optional closing quote handles JSON keys (`"api_key": "…"`).
45
+ String.raw `(["']?\s*[:=]\s*|\s+)` +
46
+ String.raw `(["']?)([A-Za-z0-9/+_.~-]{16,}={0,2})\3`, 'gi'),
47
+ ];
23
48
  export function createDefaultRedactor(extraSecrets = []) {
24
49
  const secrets = new Set([...extraSecrets].filter((value) => value.length >= 6));
25
50
  return {
@@ -27,6 +52,9 @@ export function createDefaultRedactor(extraSecrets = []) {
27
52
  let output = text;
28
53
  for (const pattern of SECRET_PATTERNS)
29
54
  output = output.replace(pattern, '[REDACTED]');
55
+ for (const pattern of LABELED_SECRET_PATTERNS) {
56
+ output = output.replace(pattern, (_match, label, separator, quote) => `${label}${separator}${quote}[REDACTED]${quote}`);
57
+ }
30
58
  for (const secret of secrets)
31
59
  output = output.split(secret).join('[REDACTED]');
32
60
  return output;
@@ -1,7 +1,9 @@
1
- export declare function withTimeout(ms: number, external?: AbortSignal): {
1
+ export declare function withTimeout(ms: number, external?: AbortSignal, idleMs?: number): {
2
2
  signal: AbortSignal;
3
3
  done(): void;
4
4
  timedOut(): boolean;
5
+ /** Reset the idle timer. No-op unless `idleMs` was supplied. Call on each chunk. */
6
+ bump(): void;
5
7
  };
6
8
  export declare function fetchJson(url: string, init: RequestInit & {
7
9
  timeoutMs: number;
package/dist/transport.js CHANGED
@@ -1,11 +1,25 @@
1
1
  import { AIError, classify, fromUnknown } from './errors.js';
2
- export function withTimeout(ms, external) {
2
+ export function withTimeout(ms, external, idleMs) {
3
3
  const controller = new AbortController();
4
4
  let didTimeOut = false;
5
5
  const timer = setTimeout(() => {
6
6
  didTimeOut = true;
7
7
  controller.abort(new AIError(`Request timed out after ${ms}ms`, { kind: 'timeout' }));
8
8
  }, ms);
9
+ // Optional idle timeout: aborts only when no chunk has arrived for `idleMs`,
10
+ // rearmed on every `bump()`. Keeps a long but healthy stream alive while the
11
+ // total `ms` ceiling still bounds a truly stuck call.
12
+ let idleTimer;
13
+ const armIdle = () => {
14
+ if (idleMs === undefined)
15
+ return;
16
+ if (idleTimer)
17
+ clearTimeout(idleTimer);
18
+ idleTimer = setTimeout(() => {
19
+ didTimeOut = true;
20
+ controller.abort(new AIError(`Request idle for ${idleMs}ms`, { kind: 'timeout' }));
21
+ }, idleMs);
22
+ };
9
23
  const onAbort = () => controller.abort(external?.reason);
10
24
  if (external) {
11
25
  if (external.aborted)
@@ -13,15 +27,21 @@ export function withTimeout(ms, external) {
13
27
  else
14
28
  external.addEventListener('abort', onAbort, { once: true });
15
29
  }
30
+ armIdle();
16
31
  return {
17
32
  signal: controller.signal,
18
33
  done() {
19
34
  clearTimeout(timer);
35
+ if (idleTimer)
36
+ clearTimeout(idleTimer);
20
37
  external?.removeEventListener('abort', onAbort);
21
38
  },
22
39
  timedOut() {
23
40
  return didTimeOut;
24
41
  },
42
+ bump() {
43
+ armIdle();
44
+ },
25
45
  };
26
46
  }
27
47
  export async function fetchJson(url, init) {
package/dist/types.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { KnownProvider } from './adapters/profiles.js';
2
+ export type { KnownProvider };
1
3
  export type Role = 'system' | 'user' | 'assistant' | 'tool';
2
4
  export interface ContentPart {
3
5
  type: 'text' | 'image';
@@ -42,14 +44,39 @@ export interface ChatRequest {
42
44
  stopSequences?: string[];
43
45
  signal?: AbortSignal;
44
46
  metadata?: Record<string, unknown>;
47
+ /**
48
+ * Provider-native request fields the nugget does not model as first-class
49
+ * options — an escape hatch so a caller can reach a capability without waiting
50
+ * for a library release. Merged into the outgoing provider request body:
51
+ * shallow at the top level, and one level deep into the provider's own nested
52
+ * option container (Ollama `options`, Google `generationConfig`). Examples:
53
+ * `{ options: { num_ctx: 8192 }, keep_alive: '30m' }` (Ollama),
54
+ * `{ reasoning_effort: 'high' }` (OpenAI), `{ thinking: { type: 'enabled' } }`
55
+ * (Anthropic), `{ safetySettings: [...] }` (Google). Values here win over the
56
+ * nugget's own fields on a key collision, so use with care.
57
+ */
58
+ providerOptions?: Record<string, unknown>;
45
59
  }
46
60
  export interface Connection {
47
61
  id: string;
48
- provider: string;
62
+ /**
63
+ * A provider with a profile in the table, or any other string — an unknown
64
+ * key with a `baseUrl` resolves through the `openai-compat` escape hatch.
65
+ * The union is there for autocomplete and typo-catching; the `string & {}`
66
+ * arm keeps the escape hatch open.
67
+ */
68
+ provider: KnownProvider | (string & {});
49
69
  baseUrl?: string;
50
70
  keyRef?: KeyRef;
51
71
  timeoutMs?: number;
52
72
  headers?: Record<string, string>;
73
+ /**
74
+ * Idle timeout for streaming calls: aborts the stream only if no bytes arrive
75
+ * for this many milliseconds, resetting on every chunk. Distinct from
76
+ * `timeoutMs`, which bounds the total call lifetime. Set this (and a generous
77
+ * `timeoutMs`) for long local generations that stream slowly but healthily.
78
+ */
79
+ idleTimeoutMs?: number;
53
80
  }
54
81
  export type KeyRef = {
55
82
  kind: 'none';
@@ -103,8 +130,27 @@ export interface ModelSource {
103
130
  connectionId: string;
104
131
  baseUrl?: string;
105
132
  }
133
+ export interface EmbedRequest {
134
+ model: string;
135
+ /** One string, or a batch of strings embedded in a single call. */
136
+ input: string | string[];
137
+ signal?: AbortSignal;
138
+ metadata?: Record<string, unknown>;
139
+ /** Provider-native passthrough, same contract as {@link ChatRequest.providerOptions}. */
140
+ providerOptions?: Record<string, unknown>;
141
+ }
142
+ export interface EmbedResult {
143
+ /** One vector per input, in input order. */
144
+ embeddings: number[][];
145
+ model: string;
146
+ usage: Usage;
147
+ source: ModelSource;
148
+ raw?: unknown;
149
+ }
106
150
  export declare function modelRef(source: ModelSource, model: string): string;
107
- export type AIErrorKind = 'auth' | 'rate_limit' | 'timeout' | 'network' | 'server' | 'invalid_request' | 'invalid_response' | 'context_length' | 'canceled' | 'policy_blocked' | 'key_unavailable' | 'tool_error' | 'budget_exceeded';
151
+ export type AIErrorKind = 'auth' | 'rate_limit' | 'timeout' | 'network' | 'server' | 'invalid_request' | 'invalid_response'
152
+ /** 404/410 — endpoint, deployment, or model not found (wrong baseUrl/model, or a misrouted edge). */
153
+ | 'not_found' | 'context_length' | 'canceled' | 'policy_blocked' | 'key_unavailable' | 'tool_error' | 'budget_exceeded';
108
154
  export type StreamEvent = {
109
155
  type: 'start';
110
156
  callId: string;
@@ -113,6 +159,9 @@ export type StreamEvent = {
113
159
  } | {
114
160
  type: 'delta';
115
161
  text: string;
162
+ } | {
163
+ type: 'reasoning';
164
+ text: string;
116
165
  } | {
117
166
  type: 'tool_call';
118
167
  call: ToolCall;
@@ -141,6 +190,7 @@ export interface ProviderAdapter {
141
190
  ok: boolean;
142
191
  detail?: string;
143
192
  }>;
193
+ embed?(conn: ResolvedConnection, req: EmbedRequest): Promise<EmbedResult>;
144
194
  }
145
195
  export interface ModelInfo {
146
196
  id: string;
@@ -168,6 +218,15 @@ export interface CallInfo {
168
218
  provider: string;
169
219
  model: string;
170
220
  metadata?: Record<string, unknown>;
221
+ /**
222
+ * The fully resolved connection for this call — base URL defaulted from the
223
+ * provider profile and auth headers applied. Present whenever `beforeCall`
224
+ * runs after resolution (chat/stream and probes), letting a hook validate the
225
+ * *effective* endpoint (e.g. a request-time SSRF/DNS-rebinding re-check on the
226
+ * resolved `baseUrl`) without duplicating profile logic. The resolved
227
+ * `apiKey` is intentionally not exposed here.
228
+ */
229
+ resolved?: Omit<ResolvedConnection, 'apiKey'>;
171
230
  }
172
231
  export interface CallRecord {
173
232
  callId: string;
@@ -187,5 +246,40 @@ export interface CallRecord {
187
246
  metadata?: Record<string, unknown>;
188
247
  promptChars: number;
189
248
  responseChars: number;
249
+ /**
250
+ * Estimated cost of this call in USD, computed by the optional `pricing` hook
251
+ * passed to the handler. Undefined when no pricing hook is configured or the
252
+ * hook returns nothing for this provider/model.
253
+ */
254
+ costUsd?: number;
255
+ }
256
+ /**
257
+ * Minimal [Standard Schema](https://standardschema.dev) validator surface. Any
258
+ * Zod / Valibot / ArkType schema (v1+) satisfies this via its `~standard`
259
+ * property, so `chatParsed` can validate model output against a schema the app
260
+ * already owns — with zero dependency added to this library.
261
+ */
262
+ export interface StandardSchemaV1<Output = unknown> {
263
+ readonly '~standard': {
264
+ readonly version: 1;
265
+ readonly vendor: string;
266
+ readonly validate: (value: unknown) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
267
+ };
268
+ }
269
+ export type StandardSchemaResult<Output> = {
270
+ readonly value: Output;
271
+ readonly issues?: undefined;
272
+ } | {
273
+ readonly issues: ReadonlyArray<{
274
+ readonly message: string;
275
+ readonly path?: ReadonlyArray<PropertyKey | {
276
+ readonly key: PropertyKey;
277
+ }>;
278
+ }>;
279
+ };
280
+ /** Output of {@link AIHandler.chatParsed}: the validated value plus the raw chat result. */
281
+ export interface ParsedResult<T> {
282
+ data: T;
283
+ result: ChatResult;
190
284
  }
191
285
  //# sourceMappingURL=types.d.ts.map
package/dist/util.d.ts CHANGED
@@ -4,5 +4,21 @@ export declare function asString(value: unknown): string | undefined;
4
4
  export declare function asNumber(value: unknown): number | undefined;
5
5
  export declare function promptChars(messages: ChatMessage[]): number;
6
6
  export declare function textFromMessages(messages: ChatMessage[]): string;
7
+ export declare function globalEnv(): Record<string, string | undefined>;
8
+ /**
9
+ * Merges app-supplied {@link ChatRequest.providerOptions} into a provider
10
+ * request body: shallow at the top level, and one level deep for each key in
11
+ * `nestedKeys` where both sides are plain objects (Ollama `options`, Google
12
+ * `generationConfig`). Values in `providerOptions` win on collision.
13
+ */
14
+ export declare function applyProviderOptions(body: Record<string, unknown>, providerOptions: Record<string, unknown> | undefined, nestedKeys?: readonly string[]): Record<string, unknown>;
15
+ /** Joins a base URL and path without producing a double slash, even if `base` ends with `/`. */
16
+ export declare function joinUrl(base: string, path: string): string;
17
+ /**
18
+ * Runs `fn` over `items` with at most `limit` in flight at once, preserving
19
+ * input order in the returned array. Used to probe many local models
20
+ * concurrently without opening an unbounded number of sockets.
21
+ */
22
+ export declare function mapWithConcurrency<T, R>(items: readonly T[], limit: number, fn: (item: T, index: number) => Promise<R>): Promise<R[]>;
7
23
  export declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
8
24
  //# sourceMappingURL=util.d.ts.map
package/dist/util.js CHANGED
@@ -21,6 +21,56 @@ export function textFromMessages(messages) {
21
21
  return message.content.map((part) => part.text ?? '').join('');
22
22
  }).join('\n');
23
23
  }
24
+ export function globalEnv() {
25
+ const maybeProcess = globalThis;
26
+ return maybeProcess.process?.env ?? {};
27
+ }
28
+ /**
29
+ * Merges app-supplied {@link ChatRequest.providerOptions} into a provider
30
+ * request body: shallow at the top level, and one level deep for each key in
31
+ * `nestedKeys` where both sides are plain objects (Ollama `options`, Google
32
+ * `generationConfig`). Values in `providerOptions` win on collision.
33
+ */
34
+ export function applyProviderOptions(body, providerOptions, nestedKeys = []) {
35
+ if (!providerOptions)
36
+ return body;
37
+ const merged = { ...body };
38
+ for (const [key, value] of Object.entries(providerOptions)) {
39
+ const base = asRecord(merged[key]);
40
+ if (nestedKeys.includes(key) && base && asRecord(value)) {
41
+ merged[key] = { ...base, ...value };
42
+ }
43
+ else {
44
+ merged[key] = value;
45
+ }
46
+ }
47
+ return merged;
48
+ }
49
+ /** Joins a base URL and path without producing a double slash, even if `base` ends with `/`. */
50
+ export function joinUrl(base, path) {
51
+ return `${base.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`;
52
+ }
53
+ /**
54
+ * Runs `fn` over `items` with at most `limit` in flight at once, preserving
55
+ * input order in the returned array. Used to probe many local models
56
+ * concurrently without opening an unbounded number of sockets.
57
+ */
58
+ export async function mapWithConcurrency(items, limit, fn) {
59
+ const results = new Array(items.length);
60
+ let next = 0;
61
+ const workerCount = Math.max(1, Math.min(limit, items.length));
62
+ const worker = async () => {
63
+ for (;;) {
64
+ const index = next;
65
+ next += 1;
66
+ if (index >= items.length)
67
+ return;
68
+ results[index] = await fn(items[index], index);
69
+ }
70
+ };
71
+ await Promise.all(Array.from({ length: workerCount }, worker));
72
+ return results;
73
+ }
24
74
  export function sleep(ms, signal) {
25
75
  if (ms <= 0)
26
76
  return Promise.resolve();
@@ -1,2 +1,2 @@
1
- 0.3.1
2
- a15d2e448ed391c96856ccb9b4f423d229a7bfd684bf394a5c3d93a5fcd876f6
1
+ 0.6.0
2
+ 762595605eef73dde3751687e21190d8281be3ade9d699d02620920f6c021de2
@@ -0,0 +1,14 @@
1
+ import type { ChatRequest, ChatResult, ModelInfo, ProviderAdapter, ResolvedConnection, StreamEvent } from '../../types.js';
2
+ export declare class AnthropicAdapter implements ProviderAdapter {
3
+ readonly provider: string;
4
+ constructor(provider: string);
5
+ chat(conn: ResolvedConnection, req: ChatRequest): Promise<ChatResult>;
6
+ stream(conn: ResolvedConnection, req: ChatRequest): AsyncIterable<StreamEvent>;
7
+ /**
8
+ * Lists models from Anthropic's `/v1/models` endpoint (auth + version headers
9
+ * already applied to `conn.headers`). The endpoint does not report a context
10
+ * window, so `contextWindow` is left undefined rather than guessed.
11
+ */
12
+ listModels(conn: ResolvedConnection): Promise<ModelInfo[]>;
13
+ }
14
+ //# sourceMappingURL=anthropic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../../src/adapters/engines/anthropic.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAe,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,kBAAkB,EAAE,WAAW,EAAY,MAAM,gBAAgB,CAAC;AAMlJ,qBAAa,gBAAiB,YAAW,eAAe;IACtD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;gBACd,QAAQ,EAAE,MAAM;IAItB,IAAI,CAAC,IAAI,EAAE,kBAAkB,EAAE,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAUpE,MAAM,CAAC,IAAI,EAAE,kBAAkB,EAAE,GAAG,EAAE,WAAW,GAAG,aAAa,CAAC,WAAW,CAAC;IA2HrF;;;;OAIG;IACG,UAAU,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;CAajE"}
@@ -0,0 +1,305 @@
1
+ import { AIError } from '../../errors.js';
2
+ import { estimatedUsage } from '../../tokens.js';
3
+ import { fetchJson, postResponse, sseLines } from '../../transport.js';
4
+ import { applyProviderOptions, asNumber, asRecord, asString, joinUrl, textFromMessages } from '../../util.js';
5
+ import { DEFAULT_TIMEOUT_MS, parseArgs, randomId, safeParse, streamAnomaly, streamError, streamTimeout } from './base.js';
6
+ const JSON_MODE_TOOL = 'json_output';
7
+ export class AnthropicAdapter {
8
+ provider;
9
+ constructor(provider) {
10
+ this.provider = provider;
11
+ }
12
+ async chat(conn, req) {
13
+ let result;
14
+ for await (const event of this.stream(conn, req)) {
15
+ if (event.type === 'done')
16
+ result = event.result;
17
+ if (event.type === 'error')
18
+ throw event.error;
19
+ }
20
+ if (!result)
21
+ throw new AIError('Anthropic stream ended without a result', { kind: 'invalid_response', provider: conn.provider });
22
+ return result;
23
+ }
24
+ async *stream(conn, req) {
25
+ const started = Date.now();
26
+ const jsonMode = req.responseFormat?.type === 'json' && !req.tools?.length;
27
+ let firstTokenMs = null;
28
+ let text = '';
29
+ let inputTokens;
30
+ let outputTokens;
31
+ let stopReason;
32
+ let sawTerminal = false;
33
+ const emittedTools = [];
34
+ // Partial tool_use blocks keyed by content-block index (input_json_delta accumulation).
35
+ const blocks = new Map();
36
+ const timeout = streamTimeout(conn, req.signal);
37
+ yield { type: 'start', callId: '', provider: conn.provider, model: req.model };
38
+ // Anthropic JSON mode is implemented as a forced `json_output` tool, which
39
+ // cannot coexist with the caller's own tools — same silent-downgrade shape
40
+ // as Gemini, so it gets the same signal rather than quietly dropping.
41
+ if (req.responseFormat?.type === 'json' && req.tools?.length) {
42
+ yield {
43
+ type: 'context',
44
+ kind: 'json_mode_downgraded',
45
+ data: { reason: 'Anthropic JSON mode uses a forced tool and cannot be combined with caller tools; the request was sent without JSON mode', provider: conn.provider },
46
+ };
47
+ }
48
+ try {
49
+ const res = await postResponse(`${conn.baseUrl}/v1/messages`, body(req, jsonMode), conn.headers, timeout.signal, conn.provider);
50
+ const contentType = res.headers.get('content-type') ?? '';
51
+ if (!contentType.includes('text/event-stream')) {
52
+ const data = await res.json();
53
+ const parsed = parseResponse(data);
54
+ inputTokens = parsed.inputTokens;
55
+ outputTokens = parsed.outputTokens;
56
+ stopReason = parsed.stopReason;
57
+ sawTerminal = true;
58
+ if (jsonMode) {
59
+ text = jsonTextFrom(parsed.toolCalls, parsed.text);
60
+ if (text)
61
+ yield { type: 'delta', text };
62
+ }
63
+ else {
64
+ text = parsed.text;
65
+ if (text)
66
+ yield { type: 'delta', text };
67
+ for (const call of parsed.toolCalls) {
68
+ emittedTools.push(call);
69
+ yield { type: 'tool_call', call };
70
+ }
71
+ }
72
+ }
73
+ else {
74
+ for await (const line of sseLines(res)) {
75
+ timeout.bump();
76
+ const record = asRecord(safeParse(line));
77
+ if (!record)
78
+ continue;
79
+ const type = asString(record.type);
80
+ if (type === 'message_start') {
81
+ const usage = asRecord(asRecord(record.message)?.usage);
82
+ inputTokens = asNumber(usage?.input_tokens) ?? inputTokens;
83
+ outputTokens = asNumber(usage?.output_tokens) ?? outputTokens;
84
+ }
85
+ else if (type === 'content_block_start') {
86
+ const index = asNumber(record.index) ?? 0;
87
+ const block = asRecord(record.content_block);
88
+ if (block?.type === 'tool_use') {
89
+ blocks.set(index, { id: asString(block.id) ?? randomId(), name: asString(block.name) ?? 'unknown', raw: '' });
90
+ }
91
+ }
92
+ else if (type === 'content_block_delta') {
93
+ const index = asNumber(record.index) ?? 0;
94
+ const delta = asRecord(record.delta);
95
+ if (delta?.type === 'text_delta') {
96
+ const piece = asString(delta.text) ?? '';
97
+ if (piece) {
98
+ if (firstTokenMs === null)
99
+ firstTokenMs = Date.now() - started;
100
+ text += piece;
101
+ yield { type: 'delta', text: piece };
102
+ }
103
+ }
104
+ else if (delta?.type === 'thinking_delta') {
105
+ // Extended-thinking tokens ride the reasoning channel, kept out of `text`.
106
+ const piece = asString(delta.thinking) ?? '';
107
+ if (piece)
108
+ yield { type: 'reasoning', text: piece };
109
+ }
110
+ else if (delta?.type === 'input_json_delta') {
111
+ const partial = blocks.get(index);
112
+ if (partial)
113
+ partial.raw += asString(delta.partial_json) ?? '';
114
+ }
115
+ }
116
+ else if (type === 'content_block_stop') {
117
+ const index = asNumber(record.index) ?? 0;
118
+ const partial = blocks.get(index);
119
+ if (partial) {
120
+ const call = { id: partial.id, name: partial.name, raw: partial.raw, arguments: parseArgs(partial.raw) };
121
+ blocks.delete(index);
122
+ if (jsonMode && call.name === JSON_MODE_TOOL) {
123
+ text = jsonTextFrom([call], text);
124
+ if (firstTokenMs === null)
125
+ firstTokenMs = Date.now() - started;
126
+ yield { type: 'delta', text };
127
+ }
128
+ else {
129
+ emittedTools.push(call);
130
+ yield { type: 'tool_call', call };
131
+ }
132
+ }
133
+ }
134
+ else if (type === 'message_stop') {
135
+ sawTerminal = true;
136
+ }
137
+ else if (type === 'message_delta') {
138
+ const delta = asRecord(record.delta);
139
+ if (asString(delta?.stop_reason))
140
+ sawTerminal = true;
141
+ stopReason = asString(delta?.stop_reason) ?? stopReason;
142
+ const usage = asRecord(record.usage);
143
+ outputTokens = asNumber(usage?.output_tokens) ?? outputTokens;
144
+ }
145
+ }
146
+ }
147
+ if (!sawTerminal)
148
+ yield streamAnomaly('stream ended without a stop_reason or message_stop');
149
+ const hasTools = emittedTools.length > 0;
150
+ yield { type: 'done', result: {
151
+ text,
152
+ toolCalls: hasTools ? emittedTools : undefined,
153
+ finishReason: mapStop(stopReason, hasTools),
154
+ usage: inputTokens !== undefined || outputTokens !== undefined ? { inputTokens, outputTokens, estimated: false } : estimatedUsage(textFromMessages(req.messages), text),
155
+ timing: { firstTokenMs, totalMs: Date.now() - started },
156
+ model: req.model,
157
+ source: { provider: conn.provider, connectionId: conn.id, baseUrl: conn.baseUrl },
158
+ } };
159
+ }
160
+ catch (error) {
161
+ throw streamError(error, timeout, conn.provider);
162
+ }
163
+ finally {
164
+ timeout.done();
165
+ }
166
+ }
167
+ /**
168
+ * Lists models from Anthropic's `/v1/models` endpoint (auth + version headers
169
+ * already applied to `conn.headers`). The endpoint does not report a context
170
+ * window, so `contextWindow` is left undefined rather than guessed.
171
+ */
172
+ async listModels(conn) {
173
+ const { data } = await fetchJson(joinUrl(conn.baseUrl, '/v1/models'), {
174
+ method: 'GET',
175
+ headers: conn.headers,
176
+ timeoutMs: Math.min(conn.timeoutMs ?? DEFAULT_TIMEOUT_MS, 15_000),
177
+ provider: conn.provider,
178
+ });
179
+ const rows = Array.isArray(asRecord(data)?.data) ? asRecord(data).data : [];
180
+ return rows
181
+ .map((row) => asString(asRecord(row)?.id))
182
+ .filter((id) => Boolean(id))
183
+ .map((id) => ({ id, source: { provider: conn.provider, connectionId: conn.id, baseUrl: conn.baseUrl } }));
184
+ }
185
+ }
186
+ function body(req, jsonMode) {
187
+ const system = req.messages.filter((m) => m.role === 'system').map((m) => textContent(m.content)).join('\n\n') || undefined;
188
+ const base = {
189
+ model: req.model,
190
+ max_tokens: req.maxTokens ?? 4096,
191
+ temperature: req.temperature,
192
+ top_p: req.topP,
193
+ stop_sequences: req.stopSequences,
194
+ system,
195
+ messages: toAnthropicMessages(req.messages.filter((m) => m.role !== 'system')),
196
+ stream: true,
197
+ };
198
+ if (jsonMode) {
199
+ // Forced-tool JSON mode: Anthropic has no response_format, so we make the
200
+ // model answer through a single required tool whose input is the JSON we want.
201
+ base.tools = [{ name: JSON_MODE_TOOL, description: 'Return the answer as a JSON object.', input_schema: schemaFor(req) }];
202
+ base.tool_choice = { type: 'tool', name: JSON_MODE_TOOL };
203
+ }
204
+ else if (req.tools?.length) {
205
+ base.tools = req.tools.map((tool) => ({ name: tool.name, description: tool.description, input_schema: tool.parameters }));
206
+ if (typeof req.toolChoice === 'object')
207
+ base.tool_choice = { type: 'tool', name: req.toolChoice.name };
208
+ else if (req.toolChoice === 'auto')
209
+ base.tool_choice = { type: 'auto' };
210
+ else if (req.toolChoice === 'none')
211
+ base.tool_choice = { type: 'none' };
212
+ }
213
+ // providerOptions carries Anthropic-native fields (`thinking`, `metadata`,
214
+ // top-level `cache_control` extras, `service_tier`, …) without a release.
215
+ return applyProviderOptions(base, req.providerOptions);
216
+ }
217
+ function toAnthropicMessages(messages) {
218
+ const out = [];
219
+ for (let index = 0; index < messages.length; index += 1) {
220
+ const message = messages[index];
221
+ if (message.role !== 'tool') {
222
+ out.push(toAnthropicMessage(message));
223
+ continue;
224
+ }
225
+ const content = [];
226
+ while (index < messages.length && messages[index]?.role === 'tool') {
227
+ const toolMessage = messages[index];
228
+ content.push({ type: 'tool_result', tool_use_id: toolMessage.toolCallId ?? '', content: textContent(toolMessage.content) });
229
+ index += 1;
230
+ }
231
+ index -= 1;
232
+ out.push({ role: 'user', content });
233
+ }
234
+ return out;
235
+ }
236
+ function toAnthropicMessage(m) {
237
+ // Tool-role messages are handled by toAnthropicMessages' batching loop and
238
+ // never reach here, so no tool branch is needed.
239
+ // assistant turns that carried tool calls replay them as tool_use blocks,
240
+ // preserving any text/image content parts that accompanied the calls.
241
+ if (m.role === 'assistant' && m.toolCalls?.length) {
242
+ const content = [...contentBlocks(m.content)];
243
+ for (const call of m.toolCalls)
244
+ content.push({ type: 'tool_use', id: call.id, name: call.name, input: call.arguments ?? {} });
245
+ return { role: 'assistant', content };
246
+ }
247
+ return {
248
+ role: m.role === 'assistant' ? 'assistant' : 'user',
249
+ content: typeof m.content === 'string' ? m.content : contentBlocks(m.content),
250
+ };
251
+ }
252
+ /** Maps text/image content — a string or a ContentPart[] — onto Anthropic content blocks. */
253
+ function contentBlocks(content) {
254
+ if (typeof content === 'string')
255
+ return content ? [{ type: 'text', text: content }] : [];
256
+ return content.map((part) => part.type === 'image'
257
+ ? { type: 'image', source: { type: 'base64', media_type: part.mimeType ?? 'image/png', data: part.imageBase64 ?? '' } }
258
+ : { type: 'text', text: part.text ?? '' });
259
+ }
260
+ function textContent(content) {
261
+ if (typeof content === 'string')
262
+ return content;
263
+ if (!content)
264
+ return '';
265
+ return content.filter((part) => part.type === 'text').map((part) => part.text ?? '').join('\n');
266
+ }
267
+ function parseResponse(data) {
268
+ const record = asRecord(data);
269
+ const content = Array.isArray(record?.content) ? record.content : [];
270
+ let text = '';
271
+ const toolCalls = [];
272
+ for (const blockValue of content) {
273
+ const block = asRecord(blockValue);
274
+ if (block?.type === 'text')
275
+ text += asString(block.text) ?? '';
276
+ if (block?.type === 'tool_use') {
277
+ toolCalls.push({
278
+ id: asString(block.id) ?? randomId(),
279
+ name: asString(block.name) ?? 'unknown',
280
+ arguments: block.input ?? {},
281
+ raw: JSON.stringify(block.input ?? {}),
282
+ });
283
+ }
284
+ }
285
+ const usage = asRecord(record?.usage);
286
+ return { text, inputTokens: asNumber(usage?.input_tokens), outputTokens: asNumber(usage?.output_tokens), stopReason: asString(record?.stop_reason), toolCalls };
287
+ }
288
+ function schemaFor(req) {
289
+ const schema = req.responseFormat?.type === 'json' ? req.responseFormat.schema : undefined;
290
+ return schema ?? { type: 'object' };
291
+ }
292
+ function jsonTextFrom(toolCalls, fallback) {
293
+ const json = toolCalls.find((call) => call.name === JSON_MODE_TOOL) ?? toolCalls[0];
294
+ if (json)
295
+ return json.raw && json.raw.trim() ? json.raw : JSON.stringify(json.arguments ?? {});
296
+ return fallback;
297
+ }
298
+ function mapStop(stop, hasTools) {
299
+ if (stop === 'tool_use' && hasTools)
300
+ return 'tool';
301
+ if (stop === 'max_tokens')
302
+ return 'length';
303
+ return 'stop';
304
+ }
305
+ //# sourceMappingURL=anthropic.js.map