@eserstack/ai 0.0.0-bootstrap.0 → 5.0.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 (62) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +498 -0
  3. package/dist/adapters/ajan-bridge.d.ts +69 -0
  4. package/dist/adapters/ajan-bridge.js +661 -0
  5. package/dist/adapters/anthropic.d.ts +25 -0
  6. package/dist/adapters/anthropic.js +567 -0
  7. package/dist/adapters/claude-code.d.ts +28 -0
  8. package/dist/adapters/claude-code.js +462 -0
  9. package/dist/adapters/cli-shared.d.ts +36 -0
  10. package/dist/adapters/cli-shared.js +207 -0
  11. package/dist/adapters/gemini.d.ts +19 -0
  12. package/dist/adapters/gemini.js +91 -0
  13. package/dist/adapters/google-shared.d.ts +58 -0
  14. package/dist/adapters/google-shared.js +424 -0
  15. package/dist/adapters/kiro.d.ts +19 -0
  16. package/dist/adapters/kiro.js +195 -0
  17. package/dist/adapters/mod.d.ts +61 -0
  18. package/dist/adapters/mod.js +129 -0
  19. package/dist/adapters/ollama.d.ts +20 -0
  20. package/dist/adapters/ollama.js +226 -0
  21. package/dist/adapters/openai.d.ts +25 -0
  22. package/dist/adapters/openai.js +553 -0
  23. package/dist/adapters/opencode.d.ts +19 -0
  24. package/dist/adapters/opencode.js +158 -0
  25. package/dist/adapters/vertexai.d.ts +19 -0
  26. package/dist/adapters/vertexai.js +96 -0
  27. package/dist/batch.d.ts +35 -0
  28. package/dist/batch.js +2 -0
  29. package/dist/commands/ask.d.ts +30 -0
  30. package/dist/commands/ask.js +404 -0
  31. package/dist/commands/list.d.ts +2 -0
  32. package/dist/commands/list.js +35 -0
  33. package/dist/config.d.ts +21 -0
  34. package/dist/config.js +16 -0
  35. package/dist/content.d.ts +14 -0
  36. package/dist/content.js +103 -0
  37. package/dist/detect.d.ts +9 -0
  38. package/dist/detect.js +125 -0
  39. package/dist/errors.d.ts +62 -0
  40. package/dist/errors.js +135 -0
  41. package/dist/generation.d.ts +126 -0
  42. package/dist/generation.js +96 -0
  43. package/dist/main.d.ts +22 -0
  44. package/dist/main.js +42 -0
  45. package/dist/mod.d.ts +16 -0
  46. package/dist/mod.js +11 -0
  47. package/dist/model.d.ts +27 -0
  48. package/dist/model.js +12 -0
  49. package/dist/module.d.ts +10 -0
  50. package/dist/module.js +21 -0
  51. package/dist/package.json +74 -0
  52. package/dist/registry.d.ts +22 -0
  53. package/dist/registry.js +97 -0
  54. package/dist/streams/layers.d.ts +4 -0
  55. package/dist/streams/layers.js +33 -0
  56. package/dist/streams/mod.d.ts +2 -0
  57. package/dist/streams/mod.js +5 -0
  58. package/dist/streams/sources.d.ts +6 -0
  59. package/dist/streams/sources.js +76 -0
  60. package/dist/types.d.ts +79 -0
  61. package/dist/types.js +2 -0
  62. package/package.json +63 -1
package/dist/model.js ADDED
@@ -0,0 +1,12 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ // =============================================================================
3
+ // Type Guard
4
+ // =============================================================================
5
+ export const isBatchCapable = (model) => {
6
+ const candidate = model;
7
+ return (typeof candidate.submitBatch === "function" &&
8
+ typeof candidate.getBatchJob === "function" &&
9
+ typeof candidate.listBatchJobs === "function" &&
10
+ typeof candidate.downloadBatchResults === "function" &&
11
+ typeof candidate.cancelBatchJob === "function");
12
+ };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * AI module definition for @eserstack/shell integration.
3
+ *
4
+ * @module
5
+ */
6
+ import * as module from "@eserstack/shell/module";
7
+ /** The command module type, `Module` from `@eserstack/shell/module`. */
8
+ export type Module = module.Module;
9
+ /** The `ai` command group: `ask` and `list`. */
10
+ export declare const moduleDef: Module;
package/dist/module.js ADDED
@@ -0,0 +1,21 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ /**
3
+ * AI module definition for @eserstack/shell integration.
4
+ *
5
+ * @module
6
+ */
7
+ import * as module from "@eserstack/shell/module";
8
+ /** The `ai` command group: `ask` and `list`. */
9
+ export const moduleDef = new module.Module({
10
+ description: "AI provider interface — ask questions, generate content",
11
+ modules: {
12
+ ask: {
13
+ description: "Send a prompt to an AI provider",
14
+ load: () => import("./commands/ask.js"),
15
+ },
16
+ list: {
17
+ description: "List available AI providers",
18
+ load: () => import("./commands/list.js"),
19
+ },
20
+ },
21
+ });
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@eserstack/ai",
3
+ "version": "5.0.0",
4
+ "type": "module",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/eser/stack",
8
+ "directory": "pkg/@eserstack/ai"
9
+ },
10
+ "files": [
11
+ "dist/**/*.js",
12
+ "dist/**/*.d.ts",
13
+ "dist/**/*.json"
14
+ ],
15
+ "exports": {
16
+ ".": "./main.ts",
17
+ "./mod": "./mod.ts",
18
+ "./module": "./module.ts",
19
+ "./adapters": "./adapters/mod.ts",
20
+ "./streams": "./streams/mod.ts",
21
+ "./commands/ask": "./commands/ask.ts",
22
+ "./commands/list": "./commands/list.ts"
23
+ },
24
+ "publishConfig": {
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/main.d.ts",
28
+ "import": "./dist/main.js"
29
+ },
30
+ "./mod": {
31
+ "types": "./dist/mod.d.ts",
32
+ "import": "./dist/mod.js"
33
+ },
34
+ "./module": {
35
+ "types": "./dist/module.d.ts",
36
+ "import": "./dist/module.js"
37
+ },
38
+ "./adapters": {
39
+ "types": "./dist/adapters/mod.d.ts",
40
+ "import": "./dist/adapters/mod.js"
41
+ },
42
+ "./streams": {
43
+ "types": "./dist/streams/mod.d.ts",
44
+ "import": "./dist/streams/mod.js"
45
+ },
46
+ "./commands/ask": {
47
+ "types": "./dist/commands/ask.d.ts",
48
+ "import": "./dist/commands/ask.js"
49
+ },
50
+ "./commands/list": {
51
+ "types": "./dist/commands/list.d.ts",
52
+ "import": "./dist/commands/list.js"
53
+ }
54
+ }
55
+ },
56
+ "scripts": {
57
+ "build": "node ../../../etc/scripts/build-library.ts"
58
+ },
59
+ "dependencies": {
60
+ "@anthropic-ai/sdk": "^1.7.0",
61
+ "@eserstack/ajan": "workspace:*",
62
+ "@eserstack/httpclient": "workspace:*",
63
+ "@eserstack/logging": "workspace:*",
64
+ "@eserstack/primitives": "workspace:*",
65
+ "@eserstack/shell": "workspace:*",
66
+ "@eserstack/standards": "workspace:*",
67
+ "@eserstack/streams": "workspace:*",
68
+ "@google/genai": "^3.0.0",
69
+ "openai": "^7.54.0"
70
+ },
71
+ "devDependencies": {
72
+ "@eserstack/test-kit": "workspace:*"
73
+ }
74
+ }
@@ -0,0 +1,22 @@
1
+ import type * as types from "./types.ts";
2
+ import type * as config from "./config.ts";
3
+ import type * as model from "./model.ts";
4
+ export type RegistryOptions = {
5
+ readonly factories?: readonly model.ProviderFactory[];
6
+ };
7
+ export declare class Registry {
8
+ private readonly models;
9
+ private readonly factories;
10
+ constructor(options?: RegistryOptions);
11
+ registerFactory(factory: model.ProviderFactory): void;
12
+ addModel(name: string, target: config.ConfigTarget): Promise<model.LanguageModel>;
13
+ removeModel(name: string): Promise<void>;
14
+ getDefault(): model.LanguageModel | null;
15
+ getNamed(name: string): model.LanguageModel | null;
16
+ getByProvider(provider: string): readonly model.LanguageModel[];
17
+ getByCapability(capability: types.ProviderCapability): readonly model.LanguageModel[];
18
+ listModels(): readonly string[];
19
+ listRegisteredProviders(): readonly string[];
20
+ loadFromConfig(cfg: config.Config): Promise<void>;
21
+ close(): Promise<void>;
22
+ }
@@ -0,0 +1,97 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ import * as configModule from "./config.js";
3
+ import * as errors from "./errors.js";
4
+ // =============================================================================
5
+ // Registry
6
+ // =============================================================================
7
+ export class Registry {
8
+ models = new Map();
9
+ factories = new Map();
10
+ constructor(options) {
11
+ if (options?.factories !== undefined) {
12
+ for (const factory of options.factories) {
13
+ this.factories.set(factory.provider, factory);
14
+ }
15
+ }
16
+ }
17
+ registerFactory(factory) {
18
+ this.factories.set(factory.provider, factory);
19
+ }
20
+ async addModel(name, target) {
21
+ if (this.models.has(name)) {
22
+ throw new errors.ModelAlreadyExistsError(name);
23
+ }
24
+ const factory = this.factories.get(target.provider);
25
+ if (factory === undefined) {
26
+ throw new errors.UnsupportedProviderError(target.provider);
27
+ }
28
+ const resolved = configModule.withDefaults(target);
29
+ const languageModel = await factory.createModel(resolved);
30
+ this.models.set(name, languageModel);
31
+ return languageModel;
32
+ }
33
+ async removeModel(name) {
34
+ const languageModel = this.models.get(name);
35
+ if (languageModel === undefined) {
36
+ throw new errors.ModelNotFoundError(name);
37
+ }
38
+ await languageModel.close();
39
+ this.models.delete(name);
40
+ }
41
+ getDefault() {
42
+ const defaultModel = this.models.get("default");
43
+ if (defaultModel !== undefined) {
44
+ return defaultModel;
45
+ }
46
+ // Fall back to first registered model
47
+ const first = this.models.values().next();
48
+ if (first.done === true) {
49
+ return null;
50
+ }
51
+ return first.value;
52
+ }
53
+ getNamed(name) {
54
+ return this.models.get(name) ?? null;
55
+ }
56
+ getByProvider(provider) {
57
+ const result = [];
58
+ for (const languageModel of this.models.values()) {
59
+ if (languageModel.provider === provider) {
60
+ result.push(languageModel);
61
+ }
62
+ }
63
+ return result;
64
+ }
65
+ getByCapability(capability) {
66
+ const result = [];
67
+ for (const languageModel of this.models.values()) {
68
+ if (languageModel.capabilities.includes(capability)) {
69
+ result.push(languageModel);
70
+ }
71
+ }
72
+ return result;
73
+ }
74
+ listModels() {
75
+ return [...this.models.keys()];
76
+ }
77
+ listRegisteredProviders() {
78
+ return [...this.factories.keys()];
79
+ }
80
+ async loadFromConfig(cfg) {
81
+ const entries = Object.entries(cfg.targets);
82
+ for (const [name, target] of entries) {
83
+ // One target at a time: models register in config order, which decides
84
+ // the getDefault fallback, and the first failure stops further clients.
85
+ // oxlint-disable-next-line no-await-in-loop
86
+ await this.addModel(name, target);
87
+ }
88
+ }
89
+ async close() {
90
+ const closePromises = [];
91
+ for (const languageModel of this.models.values()) {
92
+ closePromises.push(languageModel.close());
93
+ }
94
+ await Promise.all(closePromises);
95
+ this.models.clear();
96
+ }
97
+ }
@@ -0,0 +1,4 @@
1
+ import * as streams from "@eserstack/streams";
2
+ import type * as generation from "../generation.ts";
3
+ export declare const extractText: () => streams.Layer<generation.StreamEvent, string>;
4
+ export declare const tokenCounter: (onUsage: (usage: generation.Usage) => void) => streams.Layer<generation.StreamEvent, generation.StreamEvent>;
@@ -0,0 +1,33 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ import * as streams from "@eserstack/streams";
3
+ // =============================================================================
4
+ // Extract Text Layer
5
+ // =============================================================================
6
+ export const extractText = () => {
7
+ return streams.defineLayer({
8
+ name: "ai/extract-text",
9
+ create: () => ({
10
+ transform: (chunk, controller) => {
11
+ if (chunk.data.kind === "content_delta") {
12
+ controller.enqueue(streams.createChunk(chunk.data.textDelta));
13
+ }
14
+ },
15
+ }),
16
+ });
17
+ };
18
+ // =============================================================================
19
+ // Token Counter Layer
20
+ // =============================================================================
21
+ export const tokenCounter = (onUsage) => {
22
+ return streams.defineLayer({
23
+ name: "ai/token-counter",
24
+ create: () => ({
25
+ transform: (chunk, controller) => {
26
+ if (chunk.data.kind === "message_done") {
27
+ onUsage(chunk.data.usage);
28
+ }
29
+ controller.enqueue(streams.createChunk(chunk.data));
30
+ },
31
+ }),
32
+ });
33
+ };
@@ -0,0 +1,2 @@
1
+ export { aiSource, aiTextSource, streamEventSource } from "./sources.ts";
2
+ export { extractText, tokenCounter } from "./layers.ts";
@@ -0,0 +1,5 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ // Sources
3
+ export { aiSource, aiTextSource, streamEventSource } from "./sources.js";
4
+ // Layers
5
+ export { extractText, tokenCounter } from "./layers.js";
@@ -0,0 +1,6 @@
1
+ import * as streams from "@eserstack/streams";
2
+ import type * as model from "../model.ts";
3
+ import type * as generation from "../generation.ts";
4
+ export declare const aiSource: (languageModel: model.LanguageModel, options: generation.GenerateTextOptions, signal?: AbortSignal) => streams.Source<generation.StreamEvent>;
5
+ export declare const aiTextSource: (languageModel: model.LanguageModel, options: generation.GenerateTextOptions, signal?: AbortSignal) => streams.Source<string>;
6
+ export declare const streamEventSource: (stream: AsyncIterable<generation.StreamEvent>, name?: string) => streams.Source<generation.StreamEvent>;
@@ -0,0 +1,76 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ import * as streams from "@eserstack/streams";
3
+ // =============================================================================
4
+ // AI Source — StreamEvent chunks
5
+ // =============================================================================
6
+ export const aiSource = (languageModel, options, signal) => {
7
+ const iterable = languageModel.streamText(options, signal);
8
+ return {
9
+ name: `ai/${languageModel.provider}/${languageModel.modelId}`,
10
+ readable: new ReadableStream({
11
+ async start(controller) {
12
+ try {
13
+ for await (const event of iterable) {
14
+ controller.enqueue(streams.createChunk(event));
15
+ }
16
+ controller.close();
17
+ }
18
+ catch (err) {
19
+ controller.error(err);
20
+ }
21
+ },
22
+ }),
23
+ };
24
+ };
25
+ // =============================================================================
26
+ // AI Text Source — string chunks (text deltas only)
27
+ // =============================================================================
28
+ export const aiTextSource = (languageModel, options, signal) => {
29
+ const iterable = languageModel.streamText(options, signal);
30
+ return {
31
+ name: `ai-text/${languageModel.provider}/${languageModel.modelId}`,
32
+ readable: new ReadableStream({
33
+ async start(controller) {
34
+ try {
35
+ for await (const event of iterable) {
36
+ if (event.kind === "content_delta") {
37
+ controller.enqueue(streams.createChunk(event.textDelta));
38
+ }
39
+ else if (event.kind === "error") {
40
+ // streamText reports a failure as an event. A text stream has no
41
+ // chunk for it, so the stream fails with the error instead of
42
+ // ending as if the reply were empty.
43
+ controller.error(event.error);
44
+ return;
45
+ }
46
+ }
47
+ controller.close();
48
+ }
49
+ catch (err) {
50
+ controller.error(err);
51
+ }
52
+ },
53
+ }),
54
+ };
55
+ };
56
+ // =============================================================================
57
+ // Wrap existing AsyncIterable as Source
58
+ // =============================================================================
59
+ export const streamEventSource = (stream, name) => {
60
+ return {
61
+ name: name ?? "ai-stream",
62
+ readable: new ReadableStream({
63
+ async start(controller) {
64
+ try {
65
+ for await (const event of stream) {
66
+ controller.enqueue(streams.createChunk(event));
67
+ }
68
+ controller.close();
69
+ }
70
+ catch (err) {
71
+ controller.error(err);
72
+ }
73
+ },
74
+ }),
75
+ };
76
+ };
@@ -0,0 +1,79 @@
1
+ export type Role = "user" | "assistant" | "system" | "tool";
2
+ export type ImageDetail = "low" | "high" | "auto";
3
+ export type ImagePart = {
4
+ readonly url?: string;
5
+ readonly data?: Uint8Array;
6
+ readonly mimeType?: string;
7
+ readonly detail?: ImageDetail;
8
+ };
9
+ export type AudioPart = {
10
+ readonly url?: string;
11
+ readonly data?: Uint8Array;
12
+ readonly mimeType?: string;
13
+ };
14
+ export type FilePart = {
15
+ readonly uri: string;
16
+ readonly mimeType?: string;
17
+ };
18
+ export type ToolCall = {
19
+ readonly id: string;
20
+ readonly name: string;
21
+ readonly arguments: Record<string, unknown>;
22
+ };
23
+ export type ToolResult = {
24
+ readonly toolCallId: string;
25
+ readonly content: string;
26
+ readonly isError: boolean;
27
+ };
28
+ /**
29
+ * A block of the model's reasoning, kept apart from the answer text. A
30
+ * provider that signs its reasoning (Anthropic) checks the block when it comes
31
+ * back in the next request of a multi-turn call, so send it back unchanged.
32
+ */
33
+ export type ThinkingPart = {
34
+ /** The reasoning; empty when the provider omits it. */
35
+ readonly text: string;
36
+ /** The provider's opaque check value for `text`. */
37
+ readonly signature?: string;
38
+ /** Opaque, encrypted reasoning, set instead of `text` when the provider redacted it. */
39
+ readonly redactedData?: string;
40
+ };
41
+ export type TextBlock = {
42
+ readonly kind: "text";
43
+ readonly text: string;
44
+ };
45
+ export type ImageBlock = {
46
+ readonly kind: "image";
47
+ readonly image: ImagePart;
48
+ };
49
+ export type AudioBlock = {
50
+ readonly kind: "audio";
51
+ readonly audio: AudioPart;
52
+ };
53
+ export type FileBlock = {
54
+ readonly kind: "file";
55
+ readonly file: FilePart;
56
+ };
57
+ export type ToolCallBlock = {
58
+ readonly kind: "tool_call";
59
+ readonly toolCall: ToolCall;
60
+ };
61
+ export type ToolResultBlock = {
62
+ readonly kind: "tool_result";
63
+ readonly toolResult: ToolResult;
64
+ };
65
+ export type ThinkingBlock = {
66
+ readonly kind: "thinking";
67
+ readonly thinking: ThinkingPart;
68
+ };
69
+ export type ContentBlock = TextBlock | ImageBlock | AudioBlock | FileBlock | ToolCallBlock | ToolResultBlock | ThinkingBlock;
70
+ export type Message = {
71
+ readonly role: Role;
72
+ readonly content: readonly ContentBlock[];
73
+ };
74
+ export type ToolDefinition = {
75
+ readonly name: string;
76
+ readonly description: string;
77
+ readonly parameters?: Record<string, unknown>;
78
+ };
79
+ export type ProviderCapability = "text_generation" | "streaming" | "tool_calling" | "vision" | "audio" | "batch_processing" | "structured_output" | "reasoning";
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ // Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
2
+ export {};
package/package.json CHANGED
@@ -1 +1,63 @@
1
- {"name":"@eserstack/ai","version":"0.0.0-bootstrap.0","license":"Apache-2.0","repository":{"type":"git","url":"git+https://github.com/eser/stack.git"}}
1
+ {
2
+ "name": "@eserstack/ai",
3
+ "version": "5.0.0",
4
+ "type": "module",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/eser/stack",
8
+ "directory": "pkg/@eserstack/ai"
9
+ },
10
+ "files": [
11
+ "dist/**/*.js",
12
+ "dist/**/*.d.ts",
13
+ "dist/**/*.json"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/main.d.ts",
18
+ "import": "./dist/main.js"
19
+ },
20
+ "./mod": {
21
+ "types": "./dist/mod.d.ts",
22
+ "import": "./dist/mod.js"
23
+ },
24
+ "./module": {
25
+ "types": "./dist/module.d.ts",
26
+ "import": "./dist/module.js"
27
+ },
28
+ "./adapters": {
29
+ "types": "./dist/adapters/mod.d.ts",
30
+ "import": "./dist/adapters/mod.js"
31
+ },
32
+ "./streams": {
33
+ "types": "./dist/streams/mod.d.ts",
34
+ "import": "./dist/streams/mod.js"
35
+ },
36
+ "./commands/ask": {
37
+ "types": "./dist/commands/ask.d.ts",
38
+ "import": "./dist/commands/ask.js"
39
+ },
40
+ "./commands/list": {
41
+ "types": "./dist/commands/list.d.ts",
42
+ "import": "./dist/commands/list.js"
43
+ }
44
+ },
45
+ "dependencies": {
46
+ "@anthropic-ai/sdk": "^1.7.0",
47
+ "@google/genai": "^3.0.0",
48
+ "openai": "^7.54.0",
49
+ "@eserstack/ajan": "5.0.0",
50
+ "@eserstack/httpclient": "5.0.0",
51
+ "@eserstack/logging": "5.0.0",
52
+ "@eserstack/primitives": "5.0.0",
53
+ "@eserstack/shell": "5.0.0",
54
+ "@eserstack/standards": "5.0.0",
55
+ "@eserstack/streams": "5.0.0"
56
+ },
57
+ "devDependencies": {
58
+ "@eserstack/test-kit": "5.0.0"
59
+ },
60
+ "scripts": {
61
+ "build": "node ../../../etc/scripts/build-library.ts"
62
+ }
63
+ }