@ank1015/providers 0.0.1 → 0.0.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.
Files changed (169) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -383
  3. package/dist/agent/conversation.d.ts +97 -0
  4. package/dist/agent/conversation.d.ts.map +1 -0
  5. package/dist/agent/conversation.js +328 -0
  6. package/dist/agent/conversation.js.map +1 -0
  7. package/dist/agent/runner.d.ts +37 -0
  8. package/dist/agent/runner.d.ts.map +1 -0
  9. package/dist/agent/runner.js +169 -0
  10. package/dist/agent/runner.js.map +1 -0
  11. package/dist/agent/tools/calculate.d.ts +15 -0
  12. package/dist/agent/tools/calculate.d.ts.map +1 -0
  13. package/dist/agent/tools/calculate.js +23 -0
  14. package/dist/agent/tools/calculate.js.map +1 -0
  15. package/dist/agent/tools/get-current-time.d.ts +15 -0
  16. package/dist/agent/tools/get-current-time.d.ts.map +1 -0
  17. package/dist/agent/tools/get-current-time.js +38 -0
  18. package/dist/agent/tools/get-current-time.js.map +1 -0
  19. package/dist/agent/tools/index.d.ts +3 -0
  20. package/dist/agent/tools/index.d.ts.map +1 -0
  21. package/dist/agent/tools/index.js +3 -0
  22. package/dist/agent/tools/index.js.map +1 -0
  23. package/dist/agent/types.d.ts +53 -31
  24. package/dist/agent/types.d.ts.map +1 -1
  25. package/dist/agent/types.js +1 -2
  26. package/dist/agent/utils.d.ts +14 -0
  27. package/dist/agent/utils.d.ts.map +1 -0
  28. package/dist/agent/utils.js +59 -0
  29. package/dist/agent/utils.js.map +1 -0
  30. package/dist/index.d.ts +16 -9
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +16 -28
  33. package/dist/index.js.map +1 -1
  34. package/dist/llm.d.ts +15 -0
  35. package/dist/llm.d.ts.map +1 -0
  36. package/dist/llm.js +92 -0
  37. package/dist/llm.js.map +1 -0
  38. package/dist/models.d.ts +8 -1
  39. package/dist/models.d.ts.map +1 -1
  40. package/dist/models.generated.d.ts +25 -112
  41. package/dist/models.generated.d.ts.map +1 -1
  42. package/dist/models.generated.js +72 -227
  43. package/dist/models.generated.js.map +1 -1
  44. package/dist/models.js +30 -32
  45. package/dist/models.js.map +1 -1
  46. package/dist/providers/google/complete.d.ts +3 -0
  47. package/dist/providers/google/complete.d.ts.map +1 -0
  48. package/dist/providers/google/complete.js +53 -0
  49. package/dist/providers/google/complete.js.map +1 -0
  50. package/dist/providers/google/index.d.ts +6 -0
  51. package/dist/providers/google/index.d.ts.map +1 -0
  52. package/dist/providers/google/index.js +6 -0
  53. package/dist/providers/google/index.js.map +1 -0
  54. package/dist/providers/google/stream.d.ts +3 -0
  55. package/dist/providers/google/stream.d.ts.map +1 -0
  56. package/dist/providers/{google.js → google/stream.js} +67 -231
  57. package/dist/providers/google/stream.js.map +1 -0
  58. package/dist/providers/google/types.d.ts +8 -0
  59. package/dist/providers/google/types.d.ts.map +1 -0
  60. package/dist/providers/google/types.js +2 -0
  61. package/dist/providers/google/types.js.map +1 -0
  62. package/dist/providers/google/utils.d.ts +30 -0
  63. package/dist/providers/google/utils.d.ts.map +1 -0
  64. package/dist/providers/google/utils.js +354 -0
  65. package/dist/providers/google/utils.js.map +1 -0
  66. package/dist/providers/openai/complete.d.ts +3 -0
  67. package/dist/providers/openai/complete.d.ts.map +1 -0
  68. package/dist/providers/openai/complete.js +57 -0
  69. package/dist/providers/openai/complete.js.map +1 -0
  70. package/dist/providers/openai/index.d.ts +4 -0
  71. package/dist/providers/openai/index.d.ts.map +1 -0
  72. package/dist/providers/openai/index.js +4 -0
  73. package/dist/providers/openai/index.js.map +1 -0
  74. package/dist/providers/openai/stream.d.ts +3 -0
  75. package/dist/providers/openai/stream.d.ts.map +1 -0
  76. package/dist/providers/{openai.js → openai/stream.js} +74 -152
  77. package/dist/providers/openai/stream.js.map +1 -0
  78. package/dist/providers/openai/types.d.ts +8 -0
  79. package/dist/providers/openai/types.d.ts.map +1 -0
  80. package/dist/providers/openai/types.js +2 -0
  81. package/dist/providers/openai/types.js.map +1 -0
  82. package/dist/providers/openai/utils.d.ts +13 -0
  83. package/dist/providers/openai/utils.d.ts.map +1 -0
  84. package/dist/providers/openai/utils.js +285 -0
  85. package/dist/providers/openai/utils.js.map +1 -0
  86. package/dist/types.d.ts +95 -87
  87. package/dist/types.d.ts.map +1 -1
  88. package/dist/types.js +1 -9
  89. package/dist/types.js.map +1 -1
  90. package/dist/utils/event-stream.d.ts +2 -2
  91. package/dist/utils/event-stream.d.ts.map +1 -1
  92. package/dist/utils/event-stream.js +2 -7
  93. package/dist/utils/event-stream.js.map +1 -1
  94. package/dist/utils/json-parse.js +3 -6
  95. package/dist/utils/json-parse.js.map +1 -1
  96. package/dist/utils/overflow.d.ts +51 -0
  97. package/dist/utils/overflow.d.ts.map +1 -0
  98. package/dist/utils/overflow.js +106 -0
  99. package/dist/utils/overflow.js.map +1 -0
  100. package/dist/utils/sanitize-unicode.js +1 -4
  101. package/dist/utils/sanitize-unicode.js.map +1 -1
  102. package/dist/utils/uuid.d.ts +6 -0
  103. package/dist/utils/uuid.d.ts.map +1 -0
  104. package/dist/utils/uuid.js +9 -0
  105. package/dist/utils/uuid.js.map +1 -0
  106. package/dist/utils/validation.d.ts +10 -3
  107. package/dist/utils/validation.d.ts.map +1 -1
  108. package/dist/utils/validation.js +20 -12
  109. package/dist/utils/validation.js.map +1 -1
  110. package/package.json +45 -8
  111. package/biome.json +0 -43
  112. package/dist/agent/agent-loop.d.ts +0 -5
  113. package/dist/agent/agent-loop.d.ts.map +0 -1
  114. package/dist/agent/agent-loop.js +0 -219
  115. package/dist/agent/agent-loop.js.map +0 -1
  116. package/dist/providers/convert.d.ts +0 -6
  117. package/dist/providers/convert.d.ts.map +0 -1
  118. package/dist/providers/convert.js +0 -207
  119. package/dist/providers/convert.js.map +0 -1
  120. package/dist/providers/google.d.ts +0 -26
  121. package/dist/providers/google.d.ts.map +0 -1
  122. package/dist/providers/google.js.map +0 -1
  123. package/dist/providers/openai.d.ts +0 -17
  124. package/dist/providers/openai.d.ts.map +0 -1
  125. package/dist/providers/openai.js.map +0 -1
  126. package/dist/stream.d.ts +0 -4
  127. package/dist/stream.d.ts.map +0 -1
  128. package/dist/stream.js +0 -40
  129. package/dist/stream.js.map +0 -1
  130. package/dist/test-google-agent-loop.d.ts +0 -2
  131. package/dist/test-google-agent-loop.d.ts.map +0 -1
  132. package/dist/test-google-agent-loop.js +0 -186
  133. package/dist/test-google-agent-loop.js.map +0 -1
  134. package/dist/test-google.d.ts +0 -2
  135. package/dist/test-google.d.ts.map +0 -1
  136. package/dist/test-google.js +0 -41
  137. package/dist/test-google.js.map +0 -1
  138. package/src/agent/agent-loop.ts +0 -275
  139. package/src/agent/types.ts +0 -80
  140. package/src/index.ts +0 -72
  141. package/src/models.generated.ts +0 -314
  142. package/src/models.ts +0 -45
  143. package/src/providers/convert.ts +0 -222
  144. package/src/providers/google.ts +0 -496
  145. package/src/providers/openai.ts +0 -437
  146. package/src/stream.ts +0 -60
  147. package/src/types.ts +0 -198
  148. package/src/utils/event-stream.ts +0 -60
  149. package/src/utils/json-parse.ts +0 -28
  150. package/src/utils/sanitize-unicode.ts +0 -25
  151. package/src/utils/validation.ts +0 -69
  152. package/test/core/agent-loop.test.ts +0 -958
  153. package/test/core/stream.test.ts +0 -409
  154. package/test/data/red-circle.png +0 -0
  155. package/test/data/superintelligentwill.pdf +0 -0
  156. package/test/edge-cases/general.test.ts +0 -565
  157. package/test/integration/e2e.test.ts +0 -530
  158. package/test/models/cost.test.ts +0 -499
  159. package/test/models/registry.test.ts +0 -298
  160. package/test/providers/convert.test.ts +0 -846
  161. package/test/providers/google-schema.test.ts +0 -666
  162. package/test/providers/google-stream.test.ts +0 -369
  163. package/test/providers/openai-stream.test.ts +0 -251
  164. package/test/utils/event-stream.test.ts +0 -289
  165. package/test/utils/json-parse.test.ts +0 -344
  166. package/test/utils/sanitize-unicode.test.ts +0 -329
  167. package/test/utils/validation.test.ts +0 -614
  168. package/tsconfig.json +0 -21
  169. package/vitest.config.ts +0 -9
@@ -1,60 +0,0 @@
1
- import { NativeAssistantMessage, AssistantMessageEvent } from "../types";
2
-
3
- // Generic event stream class for async iteration
4
- export class EventStream<T, R = T> implements AsyncIterable<T> {
5
- private queue: T[] = [];
6
- private waiting: ((value: IteratorResult<T>) => void)[] = [];
7
- private done = false;
8
- private finalResultPromise: Promise<R>;
9
- private resolveFinalResult!: (result: R) => void;
10
-
11
- constructor() {
12
- this.finalResultPromise = new Promise((resolve) => {
13
- this.resolveFinalResult = resolve;
14
- });
15
- }
16
-
17
- push(event: T): void {
18
- if (this.done) return;
19
-
20
- // Deliver to waiting consumer or queue it
21
- const waiter = this.waiting.shift();
22
- if (waiter) {
23
- waiter({ value: event, done: false });
24
- } else {
25
- this.queue.push(event);
26
- }
27
- }
28
-
29
- end(result: R): void {
30
- this.done = true;
31
- // Always resolve the final result to prevent deadlock
32
- this.resolveFinalResult(result);
33
- // Notify all waiting consumers that we're done
34
- while (this.waiting.length > 0) {
35
- const waiter = this.waiting.shift()!;
36
- // When done is true, value type is the return type (void for async generators)
37
- waiter({ done: true, value: undefined! });
38
- }
39
- }
40
-
41
- async *[Symbol.asyncIterator](): AsyncIterator<T> {
42
- while (true) {
43
- if (this.queue.length > 0) {
44
- yield this.queue.shift()!;
45
- } else if (this.done) {
46
- return;
47
- } else {
48
- const result = await new Promise<IteratorResult<T>>((resolve) => this.waiting.push(resolve));
49
- if (result.done) return;
50
- yield result.value;
51
- }
52
- }
53
- }
54
-
55
- result(): Promise<R> {
56
- return this.finalResultPromise;
57
- }
58
- }
59
-
60
- export class AssistantMessageEventStream extends EventStream<AssistantMessageEvent, NativeAssistantMessage> {}
@@ -1,28 +0,0 @@
1
- import { parse as partialParse } from "partial-json";
2
-
3
- /**
4
- * Attempts to parse potentially incomplete JSON during streaming.
5
- * Always returns a valid object, even if the JSON is incomplete.
6
- *
7
- * @param partialJson The partial JSON string from streaming
8
- * @returns Parsed object or empty object if parsing fails
9
- */
10
- export function parseStreamingJson<T = any>(partialJson: string | undefined): T {
11
- if (!partialJson || partialJson.trim() === "") {
12
- return {} as T;
13
- }
14
-
15
- // Try standard parsing first (fastest for complete JSON)
16
- try {
17
- return JSON.parse(partialJson) as T;
18
- } catch {
19
- // Try partial-json for incomplete JSON
20
- try {
21
- const result = partialParse(partialJson);
22
- return (result ?? {}) as T;
23
- } catch {
24
- // If all parsing fails, return empty object
25
- return {} as T;
26
- }
27
- }
28
- }
@@ -1,25 +0,0 @@
1
- /**
2
- * Removes unpaired Unicode surrogate characters from a string.
3
- *
4
- * Unpaired surrogates (high surrogates 0xD800-0xDBFF without matching low surrogates 0xDC00-0xDFFF,
5
- * or vice versa) cause JSON serialization errors in many API providers.
6
- *
7
- * Valid emoji and other characters outside the Basic Multilingual Plane use properly paired
8
- * surrogates and will NOT be affected by this function.
9
- *
10
- * @param text - The text to sanitize
11
- * @returns The sanitized text with unpaired surrogates removed
12
- *
13
- * @example
14
- * // Valid emoji (properly paired surrogates) are preserved
15
- * sanitizeSurrogates("Hello 🙈 World") // => "Hello 🙈 World"
16
- *
17
- * // Unpaired high surrogate is removed
18
- * const unpaired = String.fromCharCode(0xD83D); // high surrogate without low
19
- * sanitizeSurrogates(`Text ${unpaired} here`) // => "Text here"
20
- */
21
- export function sanitizeSurrogates(text: string): string {
22
- // Replace unpaired high surrogates (0xD800-0xDBFF not followed by low surrogate)
23
- // Replace unpaired low surrogates (0xDC00-0xDFFF not preceded by high surrogate)
24
- return text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "");
25
- }
@@ -1,69 +0,0 @@
1
- import AjvModule from "ajv";
2
- import addFormatsModule from "ajv-formats";
3
-
4
- // Handle both default and named exports
5
- const Ajv = (AjvModule as any).default || AjvModule;
6
- const addFormats = (addFormatsModule as any).default || addFormatsModule;
7
-
8
- import type { Tool, AssistantToolCall } from "../types.js";
9
- import type { TSchema, Static } from "@sinclair/typebox";
10
-
11
- // Detect if we're in a browser extension environment with strict CSP
12
- // Chrome extensions with Manifest V3 don't allow eval/Function constructor
13
- const isBrowserExtension = typeof globalThis !== "undefined" && (globalThis as any).chrome?.runtime?.id !== undefined;
14
-
15
- // Create a singleton AJV instance with formats (only if not in browser extension)
16
- // AJV requires 'unsafe-eval' CSP which is not allowed in Manifest V3
17
- let ajv: any = null;
18
- if (!isBrowserExtension) {
19
- try {
20
- ajv = new Ajv({
21
- allErrors: true,
22
- strict: false,
23
- });
24
- addFormats(ajv);
25
- } catch (e) {
26
- // AJV initialization failed (likely CSP restriction)
27
- console.warn("AJV validation disabled due to CSP restrictions");
28
- }
29
- }
30
-
31
- /**
32
- * Validates tool call arguments against the tool's TypeBox schema
33
- * @param tool The tool definition with TypeBox schema
34
- * @param toolCall The tool call from the LLM
35
- * @returns The validated arguments with proper typing
36
- * @throws Error with formatted message if validation fails
37
- */
38
- export function validateToolArguments<TParameters extends TSchema>(
39
- tool: Tool<TParameters>,
40
- toolCall: AssistantToolCall
41
- ): Static<TParameters> {
42
- // Skip validation in browser extension environment (CSP restrictions prevent AJV from working)
43
- if (!ajv || isBrowserExtension) {
44
- // Trust the LLM's output without validation
45
- // Browser extensions can't use AJV due to Manifest V3 CSP restrictions
46
- return toolCall.arguments as Static<TParameters>;
47
- }
48
-
49
- // Compile the schema
50
- const validate = ajv.compile(tool.parameters);
51
-
52
- // Validate the arguments
53
- if (validate(toolCall.arguments)) {
54
- return toolCall.arguments as Static<TParameters>;
55
- }
56
-
57
- // Format validation errors nicely
58
- const errors =
59
- validate.errors
60
- ?.map((err: any) => {
61
- const path = err.instancePath ? err.instancePath.substring(1) : err.params.missingProperty || "root";
62
- return ` - ${path}: ${err.message}`;
63
- })
64
- .join("\n") || "Unknown validation error";
65
-
66
- const errorMessage = `Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`;
67
-
68
- throw new Error(errorMessage);
69
- }