@eserstack/ai 0.0.0-stage → 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.
- package/LICENSE +13 -0
- package/README.md +497 -2
- package/dist/adapters/ajan-bridge.d.ts +69 -0
- package/dist/adapters/ajan-bridge.js +661 -0
- package/dist/adapters/anthropic.d.ts +25 -0
- package/dist/adapters/anthropic.js +567 -0
- package/dist/adapters/claude-code.d.ts +28 -0
- package/dist/adapters/claude-code.js +462 -0
- package/dist/adapters/cli-shared.d.ts +36 -0
- package/dist/adapters/cli-shared.js +207 -0
- package/dist/adapters/gemini.d.ts +19 -0
- package/dist/adapters/gemini.js +91 -0
- package/dist/adapters/google-shared.d.ts +58 -0
- package/dist/adapters/google-shared.js +424 -0
- package/dist/adapters/kiro.d.ts +19 -0
- package/dist/adapters/kiro.js +195 -0
- package/dist/adapters/mod.d.ts +61 -0
- package/dist/adapters/mod.js +129 -0
- package/dist/adapters/ollama.d.ts +20 -0
- package/dist/adapters/ollama.js +226 -0
- package/dist/adapters/openai.d.ts +25 -0
- package/dist/adapters/openai.js +553 -0
- package/dist/adapters/opencode.d.ts +19 -0
- package/dist/adapters/opencode.js +158 -0
- package/dist/adapters/vertexai.d.ts +19 -0
- package/dist/adapters/vertexai.js +96 -0
- package/dist/batch.d.ts +35 -0
- package/dist/batch.js +2 -0
- package/dist/commands/ask.d.ts +30 -0
- package/dist/commands/ask.js +404 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +35 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +16 -0
- package/dist/content.d.ts +14 -0
- package/dist/content.js +103 -0
- package/dist/detect.d.ts +9 -0
- package/dist/detect.js +125 -0
- package/dist/errors.d.ts +62 -0
- package/dist/errors.js +135 -0
- package/dist/generation.d.ts +126 -0
- package/dist/generation.js +96 -0
- package/dist/main.d.ts +22 -0
- package/dist/main.js +42 -0
- package/dist/mod.d.ts +16 -0
- package/dist/mod.js +11 -0
- package/dist/model.d.ts +27 -0
- package/dist/model.js +12 -0
- package/dist/module.d.ts +10 -0
- package/dist/module.js +21 -0
- package/dist/package.json +74 -0
- package/dist/registry.d.ts +22 -0
- package/dist/registry.js +97 -0
- package/dist/streams/layers.d.ts +4 -0
- package/dist/streams/layers.js +33 -0
- package/dist/streams/mod.d.ts +2 -0
- package/dist/streams/mod.js +5 -0
- package/dist/streams/sources.d.ts +6 -0
- package/dist/streams/sources.js +76 -0
- package/dist/types.d.ts +79 -0
- package/dist/types.js +2 -0
- package/package.json +60 -3
package/dist/detect.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* AI provider detection — checks which providers are available.
|
|
4
|
+
*
|
|
5
|
+
* Used by `ai list` and `@eserstack/noskills` init.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
10
|
+
import * as shellExec from "@eserstack/shell/exec";
|
|
11
|
+
// =============================================================================
|
|
12
|
+
// Checks
|
|
13
|
+
// =============================================================================
|
|
14
|
+
const checkBinaryExists = async (name) => {
|
|
15
|
+
try {
|
|
16
|
+
const code = await shellExec.exec `which ${name}`.noThrow().code();
|
|
17
|
+
return code === 0;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const checkOllamaReachable = async () => {
|
|
24
|
+
try {
|
|
25
|
+
const response = await fetch("http://localhost:11434/api/version", {
|
|
26
|
+
signal: AbortSignal.timeout(1000),
|
|
27
|
+
});
|
|
28
|
+
return response.ok;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const checkEnvVar = (name) => {
|
|
35
|
+
return runtime.env.has(name);
|
|
36
|
+
};
|
|
37
|
+
// =============================================================================
|
|
38
|
+
// Detection
|
|
39
|
+
// =============================================================================
|
|
40
|
+
export const detectAllProviders = async () => {
|
|
41
|
+
const [hasClaude, hasOllama, hasOpencode, hasKiro] = await Promise.all([
|
|
42
|
+
checkBinaryExists("claude"),
|
|
43
|
+
checkOllamaReachable(),
|
|
44
|
+
checkBinaryExists("opencode"),
|
|
45
|
+
checkBinaryExists("kiro"),
|
|
46
|
+
]);
|
|
47
|
+
return [
|
|
48
|
+
{
|
|
49
|
+
name: "claude-code",
|
|
50
|
+
alias: "cc",
|
|
51
|
+
type: "CLI",
|
|
52
|
+
available: hasClaude,
|
|
53
|
+
detail: hasClaude ? "claude binary found" : "claude not on PATH",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: "ollama",
|
|
57
|
+
alias: "ol",
|
|
58
|
+
type: "HTTP",
|
|
59
|
+
available: hasOllama,
|
|
60
|
+
detail: hasOllama
|
|
61
|
+
? "localhost:11434 reachable"
|
|
62
|
+
: "localhost:11434 not reachable",
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "opencode",
|
|
66
|
+
alias: "oc",
|
|
67
|
+
type: "CLI",
|
|
68
|
+
available: hasOpencode,
|
|
69
|
+
detail: hasOpencode ? "opencode binary found" : "opencode not on PATH",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: "kiro",
|
|
73
|
+
alias: "kr",
|
|
74
|
+
type: "CLI",
|
|
75
|
+
available: hasKiro,
|
|
76
|
+
detail: hasKiro ? "kiro binary found" : "kiro not on PATH",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "anthropic",
|
|
80
|
+
alias: "ant",
|
|
81
|
+
type: "API",
|
|
82
|
+
// Both, matching the SDK's credential chain and pkg/ajan/aifx's
|
|
83
|
+
// credentialEnvVars. Checking only the key would report anthropic
|
|
84
|
+
// unavailable on a token-only machine that the providers themselves
|
|
85
|
+
// authenticate fine — telling the user to set something they do not need.
|
|
86
|
+
available: checkEnvVar("ANTHROPIC_API_KEY") || checkEnvVar("ANTHROPIC_AUTH_TOKEN"),
|
|
87
|
+
detail: checkEnvVar("ANTHROPIC_API_KEY")
|
|
88
|
+
? "ANTHROPIC_API_KEY set"
|
|
89
|
+
: checkEnvVar("ANTHROPIC_AUTH_TOKEN")
|
|
90
|
+
? "ANTHROPIC_AUTH_TOKEN set"
|
|
91
|
+
: "ANTHROPIC_API_KEY not set",
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: "openai",
|
|
95
|
+
alias: "oai",
|
|
96
|
+
type: "API",
|
|
97
|
+
available: checkEnvVar("OPENAI_API_KEY"),
|
|
98
|
+
detail: checkEnvVar("OPENAI_API_KEY")
|
|
99
|
+
? "OPENAI_API_KEY set"
|
|
100
|
+
: "OPENAI_API_KEY not set",
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "gemini",
|
|
104
|
+
alias: "gem",
|
|
105
|
+
type: "API",
|
|
106
|
+
available: checkEnvVar("GEMINI_API_KEY") || checkEnvVar("GOOGLE_API_KEY"),
|
|
107
|
+
detail: checkEnvVar("GEMINI_API_KEY") || checkEnvVar("GOOGLE_API_KEY")
|
|
108
|
+
? "API key set"
|
|
109
|
+
: "GEMINI_API_KEY not set",
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: "vertexai",
|
|
113
|
+
alias: "vtx",
|
|
114
|
+
type: "API",
|
|
115
|
+
available: checkEnvVar("GOOGLE_CLOUD_PROJECT"),
|
|
116
|
+
detail: checkEnvVar("GOOGLE_CLOUD_PROJECT")
|
|
117
|
+
? "GOOGLE_CLOUD_PROJECT set"
|
|
118
|
+
: "GOOGLE_CLOUD_PROJECT not set",
|
|
119
|
+
},
|
|
120
|
+
];
|
|
121
|
+
};
|
|
122
|
+
export const getAvailableProviderNames = async () => {
|
|
123
|
+
const providers = await detectAllProviders();
|
|
124
|
+
return providers.filter((p) => p.available).map((p) => p.name);
|
|
125
|
+
};
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export type AiErrorOptions = {
|
|
2
|
+
readonly provider?: string;
|
|
3
|
+
readonly statusCode?: number;
|
|
4
|
+
readonly cause?: Error;
|
|
5
|
+
};
|
|
6
|
+
export declare class AiError extends Error {
|
|
7
|
+
readonly provider: string | null;
|
|
8
|
+
readonly statusCode: number | null;
|
|
9
|
+
constructor(message: string, options?: AiErrorOptions);
|
|
10
|
+
}
|
|
11
|
+
export declare class RateLimitedError extends AiError {
|
|
12
|
+
constructor(message: string, options?: AiErrorOptions);
|
|
13
|
+
}
|
|
14
|
+
export declare class AuthFailedError extends AiError {
|
|
15
|
+
constructor(message: string, options?: AiErrorOptions);
|
|
16
|
+
}
|
|
17
|
+
export declare class InsufficientCreditsError extends AiError {
|
|
18
|
+
constructor(message: string, options?: AiErrorOptions);
|
|
19
|
+
}
|
|
20
|
+
export declare class BadRequestError extends AiError {
|
|
21
|
+
constructor(message: string, options?: AiErrorOptions);
|
|
22
|
+
}
|
|
23
|
+
export declare class ServiceUnavailableError extends AiError {
|
|
24
|
+
constructor(message: string, options?: AiErrorOptions);
|
|
25
|
+
}
|
|
26
|
+
export declare class ModelNotFoundError extends AiError {
|
|
27
|
+
constructor(modelName: string);
|
|
28
|
+
}
|
|
29
|
+
export declare class ModelAlreadyExistsError extends AiError {
|
|
30
|
+
constructor(modelName: string);
|
|
31
|
+
}
|
|
32
|
+
export declare class UnsupportedProviderError extends AiError {
|
|
33
|
+
constructor(providerName: string);
|
|
34
|
+
}
|
|
35
|
+
export declare const classifyStatusCode: (statusCode: number) => typeof AiError | null;
|
|
36
|
+
export declare const classifyAndWrap: (provider: string, statusCode: number, original: Error) => AiError;
|
|
37
|
+
/**
|
|
38
|
+
* Classification slugs emitted by `pkg/ajan/aifx`'s `ErrorKind`.
|
|
39
|
+
*
|
|
40
|
+
* These are wire values shared with Go. Renaming one here without changing
|
|
41
|
+
* `ErrorKind` in lockstep silently drops every error of that class back to a
|
|
42
|
+
* bare {@link AiError} — the exact failure this classification exists to fix,
|
|
43
|
+
* and one nothing would report.
|
|
44
|
+
*/
|
|
45
|
+
export type AiErrorKind = "rate_limited" | "auth_failed" | "insufficient_credits" | "bad_request" | "service_unavailable" | "cancelled";
|
|
46
|
+
/**
|
|
47
|
+
* Rebuild the typed error described by a bridge response.
|
|
48
|
+
*
|
|
49
|
+
* `kind` is preferred over `statusCode`: Go classifies once, and a status with
|
|
50
|
+
* no sentinel (404, 408, 413, 422) must not be re-derived into the wrong class
|
|
51
|
+
* here. When there is no kind, the status is still carried on a bare
|
|
52
|
+
* {@link AiError} so a caller can see it.
|
|
53
|
+
*
|
|
54
|
+
* `cancelled` is deliberately NOT mapped to a subclass. Go wraps a cancelled
|
|
55
|
+
* context with its service-unavailable sentinel, which is a *retryable* class —
|
|
56
|
+
* telling a caller to retry the request they just deliberately cancelled.
|
|
57
|
+
*
|
|
58
|
+
* A zero or absent status yields `undefined`, never `0`: `0 ?? 429` is `0`, so
|
|
59
|
+
* forwarding a defaulted zero would make every typed error report statusCode 0
|
|
60
|
+
* instead of its class default.
|
|
61
|
+
*/
|
|
62
|
+
export declare const classifyWireError: (provider: string, message: string, kind?: string, statusCode?: number) => AiError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
export class AiError extends Error {
|
|
3
|
+
provider;
|
|
4
|
+
statusCode;
|
|
5
|
+
constructor(message, options) {
|
|
6
|
+
super(message, { cause: options?.cause });
|
|
7
|
+
this.name = "AiError";
|
|
8
|
+
this.provider = options?.provider ?? null;
|
|
9
|
+
this.statusCode = options?.statusCode ?? null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
// =============================================================================
|
|
13
|
+
// Classified Errors (Provider-Agnostic)
|
|
14
|
+
// =============================================================================
|
|
15
|
+
export class RateLimitedError extends AiError {
|
|
16
|
+
constructor(message, options) {
|
|
17
|
+
super(message, { ...options, statusCode: options?.statusCode ?? 429 });
|
|
18
|
+
this.name = "RateLimitedError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class AuthFailedError extends AiError {
|
|
22
|
+
constructor(message, options) {
|
|
23
|
+
super(message, { ...options, statusCode: options?.statusCode ?? 401 });
|
|
24
|
+
this.name = "AuthFailedError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class InsufficientCreditsError extends AiError {
|
|
28
|
+
constructor(message, options) {
|
|
29
|
+
super(message, { ...options, statusCode: options?.statusCode ?? 402 });
|
|
30
|
+
this.name = "InsufficientCreditsError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class BadRequestError extends AiError {
|
|
34
|
+
constructor(message, options) {
|
|
35
|
+
super(message, { ...options, statusCode: options?.statusCode ?? 400 });
|
|
36
|
+
this.name = "BadRequestError";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class ServiceUnavailableError extends AiError {
|
|
40
|
+
constructor(message, options) {
|
|
41
|
+
super(message, { ...options, statusCode: options?.statusCode ?? 503 });
|
|
42
|
+
this.name = "ServiceUnavailableError";
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// =============================================================================
|
|
46
|
+
// Registry Errors
|
|
47
|
+
// =============================================================================
|
|
48
|
+
export class ModelNotFoundError extends AiError {
|
|
49
|
+
constructor(modelName) {
|
|
50
|
+
super(`Model "${modelName}" not found in registry`);
|
|
51
|
+
this.name = "ModelNotFoundError";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export class ModelAlreadyExistsError extends AiError {
|
|
55
|
+
constructor(modelName) {
|
|
56
|
+
super(`Model "${modelName}" already exists in registry`);
|
|
57
|
+
this.name = "ModelAlreadyExistsError";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export class UnsupportedProviderError extends AiError {
|
|
61
|
+
constructor(providerName) {
|
|
62
|
+
super(`No factory registered for provider "${providerName}"`);
|
|
63
|
+
this.name = "UnsupportedProviderError";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// =============================================================================
|
|
67
|
+
// Classification Utilities
|
|
68
|
+
// =============================================================================
|
|
69
|
+
export const classifyStatusCode = (statusCode) => {
|
|
70
|
+
if (statusCode === 429) {
|
|
71
|
+
return RateLimitedError;
|
|
72
|
+
}
|
|
73
|
+
if (statusCode === 401) {
|
|
74
|
+
return AuthFailedError;
|
|
75
|
+
}
|
|
76
|
+
if (statusCode === 402) {
|
|
77
|
+
return InsufficientCreditsError;
|
|
78
|
+
}
|
|
79
|
+
if (statusCode === 400) {
|
|
80
|
+
return BadRequestError;
|
|
81
|
+
}
|
|
82
|
+
if (statusCode === 500 || statusCode === 503 || statusCode === 529) {
|
|
83
|
+
return ServiceUnavailableError;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
};
|
|
87
|
+
export const classifyAndWrap = (provider, statusCode, original) => {
|
|
88
|
+
const ErrorClass = classifyStatusCode(statusCode);
|
|
89
|
+
if (ErrorClass !== null) {
|
|
90
|
+
return new ErrorClass(original.message, {
|
|
91
|
+
provider,
|
|
92
|
+
statusCode,
|
|
93
|
+
cause: original,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return new AiError(original.message, {
|
|
97
|
+
provider,
|
|
98
|
+
statusCode,
|
|
99
|
+
cause: original,
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
// Keys are the aifx ErrorKind wire slugs listed in AiErrorKind above. A Map, so
|
|
103
|
+
// a kind from the wire such as "constructor" or "toString" finds no entry
|
|
104
|
+
// instead of an Object.prototype member.
|
|
105
|
+
const WIRE_KINDS = new Map([
|
|
106
|
+
["rate_limited", RateLimitedError],
|
|
107
|
+
["auth_failed", AuthFailedError],
|
|
108
|
+
["insufficient_credits", InsufficientCreditsError],
|
|
109
|
+
["bad_request", BadRequestError],
|
|
110
|
+
["service_unavailable", ServiceUnavailableError],
|
|
111
|
+
]);
|
|
112
|
+
/**
|
|
113
|
+
* Rebuild the typed error described by a bridge response.
|
|
114
|
+
*
|
|
115
|
+
* `kind` is preferred over `statusCode`: Go classifies once, and a status with
|
|
116
|
+
* no sentinel (404, 408, 413, 422) must not be re-derived into the wrong class
|
|
117
|
+
* here. When there is no kind, the status is still carried on a bare
|
|
118
|
+
* {@link AiError} so a caller can see it.
|
|
119
|
+
*
|
|
120
|
+
* `cancelled` is deliberately NOT mapped to a subclass. Go wraps a cancelled
|
|
121
|
+
* context with its service-unavailable sentinel, which is a *retryable* class —
|
|
122
|
+
* telling a caller to retry the request they just deliberately cancelled.
|
|
123
|
+
*
|
|
124
|
+
* A zero or absent status yields `undefined`, never `0`: `0 ?? 429` is `0`, so
|
|
125
|
+
* forwarding a defaulted zero would make every typed error report statusCode 0
|
|
126
|
+
* instead of its class default.
|
|
127
|
+
*/
|
|
128
|
+
export const classifyWireError = (provider, message, kind, statusCode) => {
|
|
129
|
+
const status = statusCode !== undefined && statusCode > 0 ? statusCode : undefined;
|
|
130
|
+
const ErrorClass = kind === undefined ? undefined : WIRE_KINDS.get(kind);
|
|
131
|
+
if (ErrorClass === undefined) {
|
|
132
|
+
return new AiError(message, { provider, statusCode: status });
|
|
133
|
+
}
|
|
134
|
+
return new ErrorClass(message, { provider, statusCode: status });
|
|
135
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import * as results from "@eserstack/primitives/results";
|
|
2
|
+
import type * as types from "./types.ts";
|
|
3
|
+
import * as errors from "./errors.ts";
|
|
4
|
+
export type ToolChoice = "auto" | "none" | "required";
|
|
5
|
+
/**
|
|
6
|
+
* Why generation stopped.
|
|
7
|
+
*
|
|
8
|
+
* - `"refusal"`: the model declined the request, or a provider's content filter
|
|
9
|
+
* withheld the answer. The content, if any, explains the refusal or was cut
|
|
10
|
+
* off by the filter; it is not an answer.
|
|
11
|
+
* - `"max_tokens"`: the reply was cut off, by the output token limit or, on
|
|
12
|
+
* `anthropic`, by a full context window.
|
|
13
|
+
* - `"stop"`: a stop sequence, or a provider reason with no closer match.
|
|
14
|
+
*/
|
|
15
|
+
export type StopReason = "end_turn" | "max_tokens" | "tool_use" | "stop" | "refusal";
|
|
16
|
+
/**
|
|
17
|
+
* Asks for a JSON reply. `"json_schema"` constrains it to `jsonSchema`,
|
|
18
|
+
* `"json_object"` asks for any JSON object, and `"text"` for plain text.
|
|
19
|
+
*
|
|
20
|
+
* On `anthropic`, `"json_schema"` is sent as `output_config.format`, and on
|
|
21
|
+
* `claude-code` as the CLI's `--json-schema` flag; both need a `jsonSchema`.
|
|
22
|
+
* Claude has no JSON mode without a schema, so both ignore `"json_object"` and
|
|
23
|
+
* `"text"`, and neither has a field for `name`.
|
|
24
|
+
*/
|
|
25
|
+
export type ResponseFormat = {
|
|
26
|
+
readonly type: "json_schema" | "json_object" | "text";
|
|
27
|
+
readonly name?: string;
|
|
28
|
+
readonly jsonSchema?: Record<string, unknown>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Returns the schema of a `json_schema` response format, for an adapter that
|
|
32
|
+
* sends it. A missing or non-object schema fails before any request is sent,
|
|
33
|
+
* since the provider would reject it with a less specific message or answer
|
|
34
|
+
* without it. Not part of the package API.
|
|
35
|
+
*
|
|
36
|
+
* @throws {errors.BadRequestError} when `jsonSchema` is missing or is not an
|
|
37
|
+
* object.
|
|
38
|
+
*/
|
|
39
|
+
export declare const requireJsonSchema: (responseFormat: ResponseFormat, provider: string) => Record<string, unknown>;
|
|
40
|
+
/**
|
|
41
|
+
* How much the model thinks and how many tokens it spends on a reply. Lower
|
|
42
|
+
* effort answers faster and cheaper. Leave it unset for the provider default.
|
|
43
|
+
*
|
|
44
|
+
* - `anthropic`: sent as `output_config.effort`.
|
|
45
|
+
* - `openai`: sent as `reasoning_effort`; a model that lacks the level rejects
|
|
46
|
+
* the request.
|
|
47
|
+
* - `claude-code`: sent as the CLI's `--effort` flag.
|
|
48
|
+
* - `gemini`, `vertexai`: sent as `thinkingConfig.thinkingLevel`, unchecked
|
|
49
|
+
* against the levels the model takes; `"xhigh"` and `"max"` send `HIGH`,
|
|
50
|
+
* Gemini's highest level.
|
|
51
|
+
* - `ollama`, `opencode`, `kiro`: ignored.
|
|
52
|
+
*/
|
|
53
|
+
export type Effort = "low" | "medium" | "high" | "xhigh" | "max";
|
|
54
|
+
export type SafetySetting = {
|
|
55
|
+
readonly category: string;
|
|
56
|
+
readonly threshold: string;
|
|
57
|
+
};
|
|
58
|
+
export type GenerateTextOptions = {
|
|
59
|
+
readonly messages: readonly types.Message[];
|
|
60
|
+
readonly tools?: readonly types.ToolDefinition[];
|
|
61
|
+
readonly system?: string;
|
|
62
|
+
readonly maxTokens?: number;
|
|
63
|
+
readonly temperature?: number;
|
|
64
|
+
readonly topP?: number;
|
|
65
|
+
readonly stopWords?: readonly string[];
|
|
66
|
+
readonly toolChoice?: ToolChoice;
|
|
67
|
+
readonly responseFormat?: ResponseFormat;
|
|
68
|
+
readonly thinkingBudget?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Overrides the effort level that `thinkingBudget` maps to. On `gemini` and
|
|
71
|
+
* `vertexai`, the budget is then left out of the request.
|
|
72
|
+
*/
|
|
73
|
+
readonly effort?: Effort;
|
|
74
|
+
readonly safetySettings?: readonly SafetySetting[];
|
|
75
|
+
readonly extensions?: Record<string, unknown>;
|
|
76
|
+
};
|
|
77
|
+
export type Usage = {
|
|
78
|
+
readonly inputTokens: number;
|
|
79
|
+
readonly outputTokens: number;
|
|
80
|
+
readonly totalTokens: number;
|
|
81
|
+
readonly thinkingTokens?: number;
|
|
82
|
+
};
|
|
83
|
+
export type GenerateTextResult = {
|
|
84
|
+
readonly content: readonly types.ContentBlock[];
|
|
85
|
+
readonly stopReason: StopReason;
|
|
86
|
+
readonly usage: Usage;
|
|
87
|
+
readonly modelId: string;
|
|
88
|
+
readonly rawRequest?: unknown;
|
|
89
|
+
readonly rawResponse?: unknown;
|
|
90
|
+
};
|
|
91
|
+
export declare const text: (result: GenerateTextResult) => string;
|
|
92
|
+
export declare const textResult: (result: results.Result<GenerateTextResult, errors.AiError>) => results.Result<string, errors.AiError>;
|
|
93
|
+
export declare const toolCalls: (result: GenerateTextResult) => readonly types.ToolCall[];
|
|
94
|
+
export type ContentDeltaEvent = {
|
|
95
|
+
readonly kind: "content_delta";
|
|
96
|
+
readonly textDelta: string;
|
|
97
|
+
};
|
|
98
|
+
export type ToolCallDeltaEvent = {
|
|
99
|
+
readonly kind: "tool_call_delta";
|
|
100
|
+
readonly textDelta?: string;
|
|
101
|
+
readonly toolCall?: types.ToolCall;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* One finished thinking block. A streamed answer replays it in the next
|
|
105
|
+
* request like a generated one; it is never part of the answer text.
|
|
106
|
+
*/
|
|
107
|
+
export type ThinkingEvent = {
|
|
108
|
+
readonly kind: "thinking";
|
|
109
|
+
readonly thinking: types.ThinkingPart;
|
|
110
|
+
};
|
|
111
|
+
export type MessageDoneEvent = {
|
|
112
|
+
readonly kind: "message_done";
|
|
113
|
+
readonly stopReason: StopReason;
|
|
114
|
+
readonly usage: Usage;
|
|
115
|
+
};
|
|
116
|
+
export type StreamErrorEvent = {
|
|
117
|
+
readonly kind: "error";
|
|
118
|
+
readonly error: Error;
|
|
119
|
+
};
|
|
120
|
+
export type StreamEvent = ContentDeltaEvent | ToolCallDeltaEvent | ThinkingEvent | MessageDoneEvent | StreamErrorEvent;
|
|
121
|
+
/**
|
|
122
|
+
* Collects a stream into one result. Blocks come out in the order the provider
|
|
123
|
+
* produced them: thinking, then text, then tool calls. Anthropic rejects a
|
|
124
|
+
* replayed assistant turn whose tool calls are not preceded by their thinking.
|
|
125
|
+
*/
|
|
126
|
+
export declare const collectStream: (stream: AsyncIterable<StreamEvent>, modelId?: string) => Promise<GenerateTextResult>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
import * as results from "@eserstack/primitives/results";
|
|
3
|
+
import * as errors from "./errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* Returns the schema of a `json_schema` response format, for an adapter that
|
|
6
|
+
* sends it. A missing or non-object schema fails before any request is sent,
|
|
7
|
+
* since the provider would reject it with a less specific message or answer
|
|
8
|
+
* without it. Not part of the package API.
|
|
9
|
+
*
|
|
10
|
+
* @throws {errors.BadRequestError} when `jsonSchema` is missing or is not an
|
|
11
|
+
* object.
|
|
12
|
+
*/
|
|
13
|
+
export const requireJsonSchema = (responseFormat, provider) => {
|
|
14
|
+
const schema = responseFormat.jsonSchema;
|
|
15
|
+
if (schema === undefined) {
|
|
16
|
+
throw new errors.BadRequestError('response format "json_schema" has no jsonSchema; set jsonSchema to a JSON Schema object', { provider });
|
|
17
|
+
}
|
|
18
|
+
if (schema === null || typeof schema !== "object" || Array.isArray(schema)) {
|
|
19
|
+
throw new errors.BadRequestError('response format "json_schema" has a jsonSchema that is not an object; set jsonSchema to a JSON Schema object', { provider });
|
|
20
|
+
}
|
|
21
|
+
return schema;
|
|
22
|
+
};
|
|
23
|
+
// =============================================================================
|
|
24
|
+
// Result Helpers
|
|
25
|
+
// =============================================================================
|
|
26
|
+
export const text = (result) => {
|
|
27
|
+
const textParts = [];
|
|
28
|
+
for (const block of result.content) {
|
|
29
|
+
if (block.kind === "text") {
|
|
30
|
+
textParts.push(block.text);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return textParts.join("");
|
|
34
|
+
};
|
|
35
|
+
export const textResult = (result) => {
|
|
36
|
+
return results.map(result, text);
|
|
37
|
+
};
|
|
38
|
+
export const toolCalls = (result) => {
|
|
39
|
+
const calls = [];
|
|
40
|
+
for (const block of result.content) {
|
|
41
|
+
if (block.kind === "tool_call") {
|
|
42
|
+
calls.push(block.toolCall);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return calls;
|
|
46
|
+
};
|
|
47
|
+
// =============================================================================
|
|
48
|
+
// Stream Collector
|
|
49
|
+
// =============================================================================
|
|
50
|
+
/**
|
|
51
|
+
* Collects a stream into one result. Blocks come out in the order the provider
|
|
52
|
+
* produced them: thinking, then text, then tool calls. Anthropic rejects a
|
|
53
|
+
* replayed assistant turn whose tool calls are not preceded by their thinking.
|
|
54
|
+
*/
|
|
55
|
+
export const collectStream = async (stream, modelId = "unknown") => {
|
|
56
|
+
const thinkingBlocks = [];
|
|
57
|
+
const contentBlocks = [];
|
|
58
|
+
let accumulatedText = "";
|
|
59
|
+
let stopReason = "end_turn";
|
|
60
|
+
let usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
61
|
+
for await (const event of stream) {
|
|
62
|
+
switch (event.kind) {
|
|
63
|
+
case "content_delta": {
|
|
64
|
+
accumulatedText += event.textDelta;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case "tool_call_delta": {
|
|
68
|
+
if (event.toolCall !== undefined) {
|
|
69
|
+
contentBlocks.push({ kind: "tool_call", toolCall: event.toolCall });
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case "thinking": {
|
|
74
|
+
thinkingBlocks.push({ kind: "thinking", thinking: event.thinking });
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
case "message_done": {
|
|
78
|
+
stopReason = event.stopReason;
|
|
79
|
+
usage = event.usage;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case "error": {
|
|
83
|
+
throw event.error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (accumulatedText.length > 0) {
|
|
88
|
+
contentBlocks.unshift({ kind: "text", text: accumulatedText });
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
content: [...thinkingBlocks, ...contentBlocks],
|
|
92
|
+
stopReason,
|
|
93
|
+
usage,
|
|
94
|
+
modelId,
|
|
95
|
+
};
|
|
96
|
+
};
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The standalone CLI of @eserstack/ai. The package's `.` export runs this CLI,
|
|
3
|
+
* and the library is published at `/mod`:
|
|
4
|
+
*
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import * as ai from "@eserstack/ai/mod";
|
|
7
|
+
*
|
|
8
|
+
* const providers = await ai.getAvailableProviderNames();
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* node ./main.ts ask "your prompt" [-p provider]
|
|
13
|
+
* node ./main.ts list
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
import * as results from "@eserstack/primitives/results";
|
|
18
|
+
/** Parse the process arguments and run the matching ai command. */
|
|
19
|
+
export declare const main: () => Promise<results.Result<void, {
|
|
20
|
+
message?: string;
|
|
21
|
+
exitCode: number;
|
|
22
|
+
}>>;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* The standalone CLI of @eserstack/ai. The package's `.` export runs this CLI,
|
|
4
|
+
* and the library is published at `/mod`:
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import * as ai from "@eserstack/ai/mod";
|
|
8
|
+
*
|
|
9
|
+
* const providers = await ai.getAvailableProviderNames();
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* node ./main.ts ask "your prompt" [-p provider]
|
|
14
|
+
* node ./main.ts list
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
import * as results from "@eserstack/primitives/results";
|
|
19
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
20
|
+
import * as module from "./module.js";
|
|
21
|
+
import config from "./package.json" with { type: "json" };
|
|
22
|
+
const app = module.moduleDef.toCommand("ai", config.version);
|
|
23
|
+
/** Parse the process arguments and run the matching ai command. */
|
|
24
|
+
export const main = async () => {
|
|
25
|
+
return await app.parse();
|
|
26
|
+
};
|
|
27
|
+
if (import.meta.main) {
|
|
28
|
+
// CLI entry point: the await runs only when this file is executed directly.
|
|
29
|
+
// oxlint-disable-next-line node/no-top-level-await
|
|
30
|
+
const result = await main();
|
|
31
|
+
results.match(result, {
|
|
32
|
+
ok: () => { },
|
|
33
|
+
fail: (error) => {
|
|
34
|
+
if (error.message !== undefined) {
|
|
35
|
+
// The CLI reports a failed command on stderr before it sets the code.
|
|
36
|
+
// oxlint-disable-next-line no-console
|
|
37
|
+
console.error(error.message);
|
|
38
|
+
}
|
|
39
|
+
runtime.process.setExitCode(error.exitCode);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type { AudioBlock, AudioPart, ContentBlock, FileBlock, FilePart, ImageBlock, ImageDetail, ImagePart, Message, ProviderCapability, Role, TextBlock, ThinkingBlock, ThinkingPart, ToolCall, ToolCallBlock, ToolDefinition, ToolResult, ToolResultBlock, } from "./types.ts";
|
|
2
|
+
export { audioMessage, decodeDataUrl, detectMimeFromUrl, imageMessage, isDataUrl, textBlock, textMessage, toolCallBlock, toolResultBlock, toolResultMessage, } from "./content.ts";
|
|
3
|
+
export type { Config, ConfigTarget, ResolvedConfigTarget } from "./config.ts";
|
|
4
|
+
export { withDefaults } from "./config.ts";
|
|
5
|
+
export type { AiErrorOptions } from "./errors.ts";
|
|
6
|
+
export { AiError, AuthFailedError, BadRequestError, classifyAndWrap, classifyStatusCode, InsufficientCreditsError, ModelAlreadyExistsError, ModelNotFoundError, RateLimitedError, ServiceUnavailableError, UnsupportedProviderError, } from "./errors.ts";
|
|
7
|
+
export type { ContentDeltaEvent, Effort, GenerateTextOptions, GenerateTextResult, MessageDoneEvent, ResponseFormat, SafetySetting, StopReason, StreamErrorEvent, StreamEvent, ThinkingEvent, ToolCallDeltaEvent, ToolChoice, Usage, } from "./generation.ts";
|
|
8
|
+
export { collectStream, text, textResult, toolCalls } from "./generation.ts";
|
|
9
|
+
export type { BatchJob, BatchRequest, BatchRequestItem, BatchResult, BatchStatus, BatchStorage, ListBatchOptions, } from "./batch.ts";
|
|
10
|
+
export type { BatchCapableModel, LanguageModel, ProviderFactory, } from "./model.ts";
|
|
11
|
+
export { isBatchCapable } from "./model.ts";
|
|
12
|
+
export type { RegistryOptions } from "./registry.ts";
|
|
13
|
+
export { Registry } from "./registry.ts";
|
|
14
|
+
export type { ProviderStatus } from "./detect.ts";
|
|
15
|
+
export { detectAllProviders, getAvailableProviderNames } from "./detect.ts";
|
|
16
|
+
export { moduleDef } from "./module.ts";
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
// Content helpers
|
|
3
|
+
export { audioMessage, decodeDataUrl, detectMimeFromUrl, imageMessage, isDataUrl, textBlock, textMessage, toolCallBlock, toolResultBlock, toolResultMessage, } from "./content.js";
|
|
4
|
+
export { withDefaults } from "./config.js";
|
|
5
|
+
export { AiError, AuthFailedError, BadRequestError, classifyAndWrap, classifyStatusCode, InsufficientCreditsError, ModelAlreadyExistsError, ModelNotFoundError, RateLimitedError, ServiceUnavailableError, UnsupportedProviderError, } from "./errors.js";
|
|
6
|
+
export { collectStream, text, textResult, toolCalls } from "./generation.js";
|
|
7
|
+
export { isBatchCapable } from "./model.js";
|
|
8
|
+
export { Registry } from "./registry.js";
|
|
9
|
+
export { detectAllProviders, getAvailableProviderNames } from "./detect.js";
|
|
10
|
+
// Module (CLI integration)
|
|
11
|
+
export { moduleDef } from "./module.js";
|
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type * as results from "@eserstack/primitives/results";
|
|
2
|
+
import type * as types from "./types.ts";
|
|
3
|
+
import type * as config from "./config.ts";
|
|
4
|
+
import type * as generation from "./generation.ts";
|
|
5
|
+
import type * as batch from "./batch.ts";
|
|
6
|
+
import type * as errors from "./errors.ts";
|
|
7
|
+
export type LanguageModel = {
|
|
8
|
+
readonly capabilities: readonly types.ProviderCapability[];
|
|
9
|
+
readonly provider: string;
|
|
10
|
+
readonly modelId: string;
|
|
11
|
+
generateText(options: generation.GenerateTextOptions, signal?: AbortSignal): Promise<results.Result<generation.GenerateTextResult, errors.AiError>>;
|
|
12
|
+
streamText(options: generation.GenerateTextOptions, signal?: AbortSignal): AsyncIterable<generation.StreamEvent>;
|
|
13
|
+
close(): Promise<void>;
|
|
14
|
+
getRawClient(): unknown;
|
|
15
|
+
};
|
|
16
|
+
export type BatchCapableModel = LanguageModel & {
|
|
17
|
+
submitBatch(request: batch.BatchRequest, signal?: AbortSignal): Promise<batch.BatchJob>;
|
|
18
|
+
getBatchJob(jobId: string, signal?: AbortSignal): Promise<batch.BatchJob>;
|
|
19
|
+
listBatchJobs(options?: batch.ListBatchOptions, signal?: AbortSignal): Promise<readonly batch.BatchJob[]>;
|
|
20
|
+
downloadBatchResults(job: batch.BatchJob, signal?: AbortSignal): Promise<readonly batch.BatchResult[]>;
|
|
21
|
+
cancelBatchJob(jobId: string, signal?: AbortSignal): Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
export declare const isBatchCapable: (model: LanguageModel) => model is BatchCapableModel;
|
|
24
|
+
export type ProviderFactory = {
|
|
25
|
+
readonly provider: string;
|
|
26
|
+
createModel(config: config.ResolvedConfigTarget): Promise<LanguageModel>;
|
|
27
|
+
};
|