@inkdropapp/ai 0.1.3 → 0.1.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 (33) hide show
  1. package/README.md +110 -6
  2. package/package.json +21 -10
  3. package/src/events.ts +105 -0
  4. package/src/index.ts +22 -5
  5. package/src/provider-constants.ts +31 -0
  6. package/src/provider.ts +13 -1
  7. package/src/providers/anthropic/constants.ts +4 -0
  8. package/src/providers/{anthropic.ts → anthropic/provider.ts} +34 -18
  9. package/src/providers/openai-compatible/constants.ts +32 -0
  10. package/src/providers/{openai-compatible.ts → openai-compatible/provider.ts} +27 -29
  11. package/src/providers/openai-compatible/validator-compiled.d.ts +7 -0
  12. package/src/providers/openai-compatible/validator-compiled.js +4 -0
  13. package/src/providers/openai-compatible/validator.ts +33 -0
  14. package/src/registry.ts +30 -6
  15. package/src/utils/logging-fetch.ts +122 -0
  16. package/src/{internal → utils}/map-ai-sdk-stream.ts +1 -1
  17. package/types/events.d.ts +99 -0
  18. package/types/index.d.ts +5 -4
  19. package/types/provider-constants.d.ts +21 -0
  20. package/types/provider.d.ts +12 -1
  21. package/types/providers/anthropic/constants.d.ts +4 -0
  22. package/types/providers/anthropic/provider.d.ts +42 -0
  23. package/types/providers/openai-compatible/constants.d.ts +25 -0
  24. package/types/providers/openai-compatible/provider.d.ts +47 -0
  25. package/types/providers/openai-compatible/validator-compiled.d.ts +9 -0
  26. package/types/providers/openai-compatible/validator.d.ts +14 -0
  27. package/types/providers/openai-compatible.d.ts +0 -13
  28. package/types/registry.d.ts +12 -1
  29. package/types/utils/logging-fetch.d.ts +13 -0
  30. package/types/utils/map-ai-sdk-error.d.ts +21 -0
  31. package/types/utils/map-ai-sdk-stream.d.ts +25 -0
  32. package/src/stream-events.ts +0 -53
  33. /package/src/{internal → utils}/map-ai-sdk-error.ts +0 -0
@@ -0,0 +1,21 @@
1
+ import { AIError } from '../errors.js';
2
+ /**
3
+ * Reduces an arbitrary unknown error coming out of the AI SDK to one of our
4
+ * {@link AIError} variants.
5
+ *
6
+ * Mapping rules:
7
+ * - Existing `AIError` → returned unchanged.
8
+ * - `LoadAPIKeyError` → `NoApiKey`.
9
+ * - `APICallError` with status:
10
+ * - 401 / 403 → `AuthenticationError`
11
+ * - 429 → `RateLimitExceeded` (with `retryAfter` parsed from header)
12
+ * - 503 / 529 → `ServerOverloaded` (with `retryAfter`)
13
+ * - 413, or any message that "looks like" a context-length overflow → `PromptTooLarge`
14
+ * - anything else → `UpstreamError` carrying status + cause
15
+ * - Other `AISDKError` / `Error` instances → `UpstreamError` (or `PromptTooLarge`
16
+ * if the message matches).
17
+ *
18
+ * @param providerId - Used to attribute the error in messages and downstream
19
+ * routing decisions.
20
+ */
21
+ export declare const mapAiSdkError: (error: unknown, providerId: string) => AIError;
@@ -0,0 +1,25 @@
1
+ import type { StreamTextResult, ToolSet } from 'ai';
2
+ import type { StreamEvent } from '../events.js';
3
+ /**
4
+ * Adapts the AI SDK's `streamText().fullStream` into the library's
5
+ * provider-agnostic {@link StreamEvent} stream.
6
+ *
7
+ * Three contracts that downstream code relies on:
8
+ *
9
+ * 1. **No throwing.** Errors thrown by the iterator (e.g. mid-stream HTTP
10
+ * failure) are caught and re-emitted as `{ kind: 'error', error }`.
11
+ * The iterator always terminates cleanly, so consumers that pump events
12
+ * over IPC don't need to wrap iteration in try/catch.
13
+ * 2. **Errors are terminal.** When an `error` event from the SDK is observed
14
+ * the iterator returns immediately — any subsequent SDK chunks are dropped.
15
+ * 3. **Unknown SDK part types are silently ignored.** Forward-compat with
16
+ * new AI SDK chunk variants without a library bump.
17
+ *
18
+ * Emits in this order over the lifetime of one completion:
19
+ * `text-delta*` → `usage-update?` → `finish | error`.
20
+ *
21
+ * @param result - The object returned by `streamText()`.
22
+ * Only its `fullStream` is consumed.
23
+ * @param providerId - Provider id used to attribute any wrapped errors.
24
+ */
25
+ export declare function mapAiSdkStream(result: Pick<StreamTextResult<ToolSet, never, never>, 'fullStream'>, providerId: string): AsyncIterable<StreamEvent>;
@@ -1,53 +0,0 @@
1
- import type { AIError } from './errors.js'
2
-
3
- /**
4
- * Why a completion stream stopped producing output.
5
- *
6
- * Mirrors the AI SDK's `FinishReason`. Unknown / provider-specific values are
7
- * normalised to `'other'` rather than silently passed through.
8
- */
9
- export type FinishReason =
10
- | 'stop'
11
- | 'length'
12
- | 'content-filter'
13
- | 'tool-calls'
14
- | 'error'
15
- | 'other'
16
-
17
- /**
18
- * Token usage for a single completion. Every field is optional because
19
- * not every provider reports every counter.
20
- */
21
- export type Usage = {
22
- /** Tokens consumed from the prompt. */
23
- inputTokens?: number
24
- /** Tokens generated in the response. */
25
- outputTokens?: number
26
- /** `inputTokens + outputTokens` as reported by the provider. */
27
- totalTokens?: number
28
- /** Of `inputTokens`, how many were a cache hit (Anthropic prompt-cache reads). */
29
- cacheReadInputTokens?: number
30
- /** Of `inputTokens`, how many were written to the prompt cache for future reads. */
31
- cacheCreationInputTokens?: number
32
- /** Subset of `outputTokens` spent on reasoning / thinking content. */
33
- reasoningTokens?: number
34
- }
35
-
36
- /**
37
- * Provider-agnostic streaming events. Every concrete provider's stream is
38
- * reduced to this discriminated union so consumers never branch on provider id.
39
- *
40
- * Errors arrive as `{ kind: 'error', ... }` events, not as thrown exceptions —
41
- * this lets the host pump events across an Electron IPC boundary one at a time
42
- * without needing try/catch around the iterator.
43
- *
44
- * `tool-call` and `tool-result` are typed but never produced in the current
45
- * version (one-shot text completions only). They're reserved for forward compat.
46
- */
47
- export type StreamEvent =
48
- | { kind: 'text-delta'; delta: string }
49
- | { kind: 'tool-call'; id: string; name: string; input: unknown }
50
- | { kind: 'tool-result'; id: string; name: string; result: unknown }
51
- | { kind: 'usage-update'; usage: Usage }
52
- | { kind: 'finish'; finishReason: FinishReason; usage: Usage }
53
- | { kind: 'error'; error: AIError }
File without changes