@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
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* `ai ask` — Send a prompt to an AI provider and stream the response.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import * as results from "@eserstack/primitives/results";
|
|
8
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
9
|
+
import * as streams from "@eserstack/streams";
|
|
10
|
+
import * as logging from "@eserstack/logging";
|
|
11
|
+
import * as shellEnv from "@eserstack/shell/env";
|
|
12
|
+
import * as registry from "../registry.js";
|
|
13
|
+
import * as aiStreams from "../streams/mod.js";
|
|
14
|
+
import * as content from "../content.js";
|
|
15
|
+
import * as generationHelpers from "../generation.js";
|
|
16
|
+
// =============================================================================
|
|
17
|
+
// Provider Aliases
|
|
18
|
+
// =============================================================================
|
|
19
|
+
// Maps, so a name from the command line such as "constructor" finds no entry
|
|
20
|
+
// instead of an Object.prototype member.
|
|
21
|
+
const PROVIDER_ALIASES = new Map([
|
|
22
|
+
["cc", "claude-code"],
|
|
23
|
+
["ol", "ollama"],
|
|
24
|
+
["oc", "opencode"],
|
|
25
|
+
["oai", "openai"],
|
|
26
|
+
["ant", "anthropic"],
|
|
27
|
+
["gem", "gemini"],
|
|
28
|
+
["vtx", "vertexai"],
|
|
29
|
+
// detect.ts:94 registers this alias and `ai list` prints it; without the entry
|
|
30
|
+
// here `ai ask -p kr` rejected an alias the CLI had just advertised.
|
|
31
|
+
["kr", "kiro"],
|
|
32
|
+
]);
|
|
33
|
+
const PROVIDER_DISPLAY_NAMES = new Map([
|
|
34
|
+
["claude-code", "Claude Code"],
|
|
35
|
+
["ollama", "Ollama"],
|
|
36
|
+
["opencode", "OpenCode"],
|
|
37
|
+
["kiro", "Kiro"],
|
|
38
|
+
["anthropic", "Anthropic API"],
|
|
39
|
+
["openai", "OpenAI API"],
|
|
40
|
+
["gemini", "Gemini API"],
|
|
41
|
+
["vertexai", "Vertex AI"],
|
|
42
|
+
]);
|
|
43
|
+
const resolveAlias = (name) => {
|
|
44
|
+
return PROVIDER_ALIASES.get(name) ?? name;
|
|
45
|
+
};
|
|
46
|
+
// =============================================================================
|
|
47
|
+
// Logging Setup
|
|
48
|
+
// =============================================================================
|
|
49
|
+
const setupLogging = async (verbose) => {
|
|
50
|
+
if (verbose) {
|
|
51
|
+
const stderrWriter = runtime.process.stderr.getWriter();
|
|
52
|
+
const errOut = streams.output({
|
|
53
|
+
renderer: streams.renderers.ansi(),
|
|
54
|
+
sink: streams.sinks.writable(new WritableStream({
|
|
55
|
+
async write(chunk) {
|
|
56
|
+
await stderrWriter.write(new TextEncoder().encode(String(chunk.data)));
|
|
57
|
+
},
|
|
58
|
+
close() {
|
|
59
|
+
stderrWriter.releaseLock();
|
|
60
|
+
},
|
|
61
|
+
})),
|
|
62
|
+
});
|
|
63
|
+
await logging.config.configure({
|
|
64
|
+
sinks: {
|
|
65
|
+
stderr: logging.sinks.getOutputSink(errOut),
|
|
66
|
+
},
|
|
67
|
+
loggers: [
|
|
68
|
+
{
|
|
69
|
+
category: ["ai"],
|
|
70
|
+
lowestLevel: logging.Severities.Debug,
|
|
71
|
+
sinks: ["stderr"],
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return logging.logger.getLogger(["ai", "ask"]);
|
|
77
|
+
};
|
|
78
|
+
// =============================================================================
|
|
79
|
+
// Auto-detect Provider
|
|
80
|
+
// =============================================================================
|
|
81
|
+
const detectProvider = async (log) => {
|
|
82
|
+
await log.info("Auto-detecting AI provider...");
|
|
83
|
+
// 1. Check for claude binary.
|
|
84
|
+
//
|
|
85
|
+
// hasExecutable walks PATH in-process. `which` cost a subprocess per probe,
|
|
86
|
+
// does not exist on Windows, and -- because the shared exec path goes through
|
|
87
|
+
// the Go native library, which caches the environment it loaded with -- could
|
|
88
|
+
// not see a PATH exported afterwards.
|
|
89
|
+
if (await shellEnv.hasExecutable("claude")) {
|
|
90
|
+
await log.info("Claude Code detected.");
|
|
91
|
+
return "claude-code";
|
|
92
|
+
}
|
|
93
|
+
// 2. Check for ollama at localhost
|
|
94
|
+
try {
|
|
95
|
+
const response = await fetch("http://localhost:11434/api/version", {
|
|
96
|
+
signal: AbortSignal.timeout(1000),
|
|
97
|
+
});
|
|
98
|
+
if (response.ok) {
|
|
99
|
+
await log.info("Ollama detected at localhost:11434.");
|
|
100
|
+
return "ollama";
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// Not reachable
|
|
105
|
+
}
|
|
106
|
+
// 3. Check for opencode binary
|
|
107
|
+
if (await shellEnv.hasExecutable("opencode")) {
|
|
108
|
+
await log.info("OpenCode detected.");
|
|
109
|
+
return "opencode";
|
|
110
|
+
}
|
|
111
|
+
// 4. Check env vars for API providers
|
|
112
|
+
if (runtime.capabilities.env) {
|
|
113
|
+
if (runtime.env.has("ANTHROPIC_API_KEY") ||
|
|
114
|
+
runtime.env.has("ANTHROPIC_AUTH_TOKEN")) {
|
|
115
|
+
await log.info("Anthropic API key detected.");
|
|
116
|
+
return "anthropic";
|
|
117
|
+
}
|
|
118
|
+
if (runtime.env.has("OPENAI_API_KEY")) {
|
|
119
|
+
await log.info("OpenAI API key detected.");
|
|
120
|
+
return "openai";
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
throw new Error("No AI provider detected. Install claude, ollama, or set ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN / OPENAI_API_KEY.");
|
|
124
|
+
};
|
|
125
|
+
// =============================================================================
|
|
126
|
+
// Factory Loader
|
|
127
|
+
// =============================================================================
|
|
128
|
+
// Delegates to the adapters barrel rather than switching over the TS adapters
|
|
129
|
+
// directly, which is what this used to do -- and which meant `ai ask` silently
|
|
130
|
+
// never used the Go bridge for any provider, however the rest of the package
|
|
131
|
+
// was configured. factoryFor applies the same bridge-first preference as
|
|
132
|
+
// defaultFactories while still importing only the one adapter it needs.
|
|
133
|
+
const loadFactory = async (providerName) => {
|
|
134
|
+
const adapters = await import("../adapters/mod.js");
|
|
135
|
+
const factory = await adapters.factoryFor(providerName);
|
|
136
|
+
if (factory === undefined) {
|
|
137
|
+
throw new Error(`Unknown provider: ${providerName}`);
|
|
138
|
+
}
|
|
139
|
+
return factory;
|
|
140
|
+
};
|
|
141
|
+
// =============================================================================
|
|
142
|
+
// Arguments
|
|
143
|
+
// =============================================================================
|
|
144
|
+
const USAGE = 'Usage: ai ask "your prompt" [-p provider] [-m model] [--max-tokens n] [--effort level] [--json] [--verbose]';
|
|
145
|
+
/** The levels `--effort` accepts, the same as `GenerateTextOptions.effort`. */
|
|
146
|
+
const EFFORT_LEVELS = new Set([
|
|
147
|
+
"low",
|
|
148
|
+
"medium",
|
|
149
|
+
"high",
|
|
150
|
+
"xhigh",
|
|
151
|
+
"max",
|
|
152
|
+
]);
|
|
153
|
+
const isEffort = (value) => EFFORT_LEVELS.has(value);
|
|
154
|
+
/** The flags that take the next argument as their value. */
|
|
155
|
+
const VALUE_FLAGS = new Set([
|
|
156
|
+
"-p",
|
|
157
|
+
"--provider",
|
|
158
|
+
"-m",
|
|
159
|
+
"--model",
|
|
160
|
+
"--max-tokens",
|
|
161
|
+
"--effort",
|
|
162
|
+
]);
|
|
163
|
+
/**
|
|
164
|
+
* Parses the arguments of `ai ask`. The failure is the message to print: a
|
|
165
|
+
* flag without its value, an invalid value, or no prompt.
|
|
166
|
+
*/
|
|
167
|
+
export const parseAskArgs = (args) => {
|
|
168
|
+
let provider = null;
|
|
169
|
+
let modelId = null;
|
|
170
|
+
let maxTokens = null;
|
|
171
|
+
let effort = null;
|
|
172
|
+
let json = false;
|
|
173
|
+
let verbose = false;
|
|
174
|
+
const promptParts = [];
|
|
175
|
+
let i = 0;
|
|
176
|
+
while (i < args.length) {
|
|
177
|
+
const arg = args[i];
|
|
178
|
+
if (VALUE_FLAGS.has(arg)) {
|
|
179
|
+
const value = args[i + 1];
|
|
180
|
+
if (value === undefined) {
|
|
181
|
+
return results.fail(`${arg} needs a value. ${USAGE}`);
|
|
182
|
+
}
|
|
183
|
+
switch (arg) {
|
|
184
|
+
case "-p":
|
|
185
|
+
case "--provider":
|
|
186
|
+
provider = value;
|
|
187
|
+
break;
|
|
188
|
+
case "-m":
|
|
189
|
+
case "--model":
|
|
190
|
+
modelId = value;
|
|
191
|
+
break;
|
|
192
|
+
case "--max-tokens": {
|
|
193
|
+
const limit = Number(value);
|
|
194
|
+
if (!Number.isSafeInteger(limit) || limit <= 0) {
|
|
195
|
+
return results.fail(`--max-tokens ${JSON.stringify(value)} is not a positive whole number; pass the output token limit, such as --max-tokens 1024`);
|
|
196
|
+
}
|
|
197
|
+
maxTokens = limit;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case "--effort":
|
|
201
|
+
if (!isEffort(value)) {
|
|
202
|
+
return results.fail(`--effort ${JSON.stringify(value)} is not an effort level; use low, medium, high, xhigh or max`);
|
|
203
|
+
}
|
|
204
|
+
effort = value;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
i += 2;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (arg === "--json") {
|
|
211
|
+
json = true;
|
|
212
|
+
i += 1;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (arg === "-v" || arg === "--verbose") {
|
|
216
|
+
verbose = true;
|
|
217
|
+
i += 1;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (!arg.startsWith("-")) {
|
|
221
|
+
promptParts.push(arg);
|
|
222
|
+
}
|
|
223
|
+
i += 1;
|
|
224
|
+
}
|
|
225
|
+
const prompt = promptParts.join(" ");
|
|
226
|
+
if (prompt.length === 0) {
|
|
227
|
+
return results.fail(USAGE);
|
|
228
|
+
}
|
|
229
|
+
return results.ok({
|
|
230
|
+
provider,
|
|
231
|
+
model: modelId,
|
|
232
|
+
maxTokens,
|
|
233
|
+
effort,
|
|
234
|
+
json,
|
|
235
|
+
verbose,
|
|
236
|
+
prompt,
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
// =============================================================================
|
|
240
|
+
// Main
|
|
241
|
+
// =============================================================================
|
|
242
|
+
/**
|
|
243
|
+
* Runs `ai ask`: parses the arguments, picks the provider (from `-p` or by
|
|
244
|
+
* detection), sends the prompt and writes the reply to stdout. Invalid
|
|
245
|
+
* arguments fail before a provider is chosen.
|
|
246
|
+
*/
|
|
247
|
+
export const main = async (args) => {
|
|
248
|
+
const parsed = parseAskArgs(args ?? []);
|
|
249
|
+
if (results.isFail(parsed)) {
|
|
250
|
+
return results.fail({ message: parsed.error, exitCode: 1 });
|
|
251
|
+
}
|
|
252
|
+
const { provider: providerFlag, model: modelFlag, maxTokens, effort, json: jsonMode, verbose, prompt: promptText, } = parsed.value;
|
|
253
|
+
const log = await setupLogging(verbose);
|
|
254
|
+
try {
|
|
255
|
+
// Resolve provider
|
|
256
|
+
const providerName = providerFlag !== null
|
|
257
|
+
? resolveAlias(providerFlag)
|
|
258
|
+
: await detectProvider(log);
|
|
259
|
+
const displayName = PROVIDER_DISPLAY_NAMES.get(providerName) ?? providerName;
|
|
260
|
+
const modelId = modelFlag ?? getDefaultModel(providerName);
|
|
261
|
+
await log.info(`Using ${displayName} (${modelId})`);
|
|
262
|
+
// Load factory and create model
|
|
263
|
+
const factory = await loadFactory(providerName);
|
|
264
|
+
const modelRegistry = new registry.Registry({ factories: [factory] });
|
|
265
|
+
// The registry owns the model, so it is closed on every path, a failed
|
|
266
|
+
// request included.
|
|
267
|
+
try {
|
|
268
|
+
const model = await modelRegistry.addModel("default", {
|
|
269
|
+
provider: providerName,
|
|
270
|
+
model: modelId,
|
|
271
|
+
});
|
|
272
|
+
await log.info(`Sending prompt (${promptText.length} chars)...`);
|
|
273
|
+
return await sendPrompt(model, {
|
|
274
|
+
messages: [content.textMessage("user", promptText)],
|
|
275
|
+
maxTokens: maxTokens ?? undefined,
|
|
276
|
+
effort: effort ?? undefined,
|
|
277
|
+
}, jsonMode, log);
|
|
278
|
+
}
|
|
279
|
+
finally {
|
|
280
|
+
await modelRegistry.close();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
catch (err) {
|
|
284
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
285
|
+
await log.error(message);
|
|
286
|
+
if (verbose && err instanceof Error && err.cause !== undefined) {
|
|
287
|
+
await log.debug(`Cause: ${String(err.cause)}`);
|
|
288
|
+
}
|
|
289
|
+
return results.fail({ message, exitCode: 1 });
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
/**
|
|
293
|
+
* Sends the prompt and writes the reply to stdout: the whole result as JSON in
|
|
294
|
+
* JSON mode, or else the text as it streams in.
|
|
295
|
+
*/
|
|
296
|
+
const sendPrompt = async (model, options, jsonMode, log) => {
|
|
297
|
+
if (jsonMode) {
|
|
298
|
+
const generateResult = await model.generateText(options);
|
|
299
|
+
if (results.isFail(generateResult)) {
|
|
300
|
+
await log.error(generateResult.error.message);
|
|
301
|
+
return results.fail({
|
|
302
|
+
message: generateResult.error.message,
|
|
303
|
+
exitCode: 1,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
const result = generateResult.value;
|
|
307
|
+
await log.info(`Response received (${generationHelpers.text(result).length} chars)`);
|
|
308
|
+
const stdOut = streams.output({ sink: streams.sinks.stdout() });
|
|
309
|
+
stdOut.writeln(JSON.stringify(result, null, 2));
|
|
310
|
+
await stdOut.close();
|
|
311
|
+
return results.ok(undefined);
|
|
312
|
+
}
|
|
313
|
+
// Streaming: pipe text deltas through typewriter sink
|
|
314
|
+
await streams
|
|
315
|
+
.pipeline()
|
|
316
|
+
.from(aiStreams.aiTextSource(model, options))
|
|
317
|
+
.to(typewriterSink())
|
|
318
|
+
.run();
|
|
319
|
+
// Newline after stream
|
|
320
|
+
const writer = runtime.process.stdout.getWriter();
|
|
321
|
+
await writer.write(new TextEncoder().encode("\n"));
|
|
322
|
+
writer.releaseLock();
|
|
323
|
+
return results.ok(undefined);
|
|
324
|
+
};
|
|
325
|
+
// =============================================================================
|
|
326
|
+
// Typewriter Sink
|
|
327
|
+
// =============================================================================
|
|
328
|
+
const CHAR_DELAY_MS = 12;
|
|
329
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
330
|
+
const typewriterSink = (delayMs = CHAR_DELAY_MS) => {
|
|
331
|
+
const encoder = new TextEncoder();
|
|
332
|
+
return {
|
|
333
|
+
name: "typewriter",
|
|
334
|
+
writable: new WritableStream({
|
|
335
|
+
async write(chunk) {
|
|
336
|
+
const text = String(chunk.data);
|
|
337
|
+
const writer = runtime.process.stdout.getWriter();
|
|
338
|
+
try {
|
|
339
|
+
for (const char of text) {
|
|
340
|
+
// The typewriter prints one character, then pauses, in order.
|
|
341
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
342
|
+
await writer.write(encoder.encode(char));
|
|
343
|
+
// The pause between characters is the effect itself.
|
|
344
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
345
|
+
await sleep(delayMs);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
finally {
|
|
349
|
+
writer.releaseLock();
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
}),
|
|
353
|
+
};
|
|
354
|
+
};
|
|
355
|
+
// =============================================================================
|
|
356
|
+
// Default Models
|
|
357
|
+
// =============================================================================
|
|
358
|
+
// Two rules govern this table.
|
|
359
|
+
//
|
|
360
|
+
// Aliased IDs, never dated snapshots. A snapshot ("claude-sonnet-4-20250514")
|
|
361
|
+
// is frozen at the day it was typed and silently rots into a model nobody meant
|
|
362
|
+
// to pin -- which is how every entry here fell several releases behind. An alias
|
|
363
|
+
// tracks the current release of its tier. Pin a snapshot in config when
|
|
364
|
+
// reproducibility matters; that is what `--model` and ConfigTarget.Model are for.
|
|
365
|
+
//
|
|
366
|
+
// The BALANCED tier, not the flagship. These are the models a user gets for
|
|
367
|
+
// simply typing `eser ai ask`, so cost and latency matter more than peak
|
|
368
|
+
// capability, and anyone who wants the flagship can name it. That is why this is
|
|
369
|
+
// claude-sonnet-5 rather than claude-opus-5, and gpt-5.6-terra rather than
|
|
370
|
+
// gpt-5.6-sol -- note OpenAI's own deprecation page names sol (not terra) as the
|
|
371
|
+
// successor to gpt-4o, so this deliberately trades a little capability for the
|
|
372
|
+
// ~2.5x lower cost of the balanced tier.
|
|
373
|
+
//
|
|
374
|
+
// Avoid family-level floating aliases such as bare "gpt-5.6": it currently
|
|
375
|
+
// resolves to the flagship sol, and the vendor can silently repoint it to a
|
|
376
|
+
// future model, changing cost and behaviour with no change here. The per-tier
|
|
377
|
+
// IDs below are stable and undated, so they give the alias benefit without that.
|
|
378
|
+
const getDefaultModel = (providerName) => {
|
|
379
|
+
switch (providerName) {
|
|
380
|
+
case "claude-code":
|
|
381
|
+
return "claude-sonnet-5";
|
|
382
|
+
case "ollama":
|
|
383
|
+
return "llama3";
|
|
384
|
+
case "opencode":
|
|
385
|
+
return "default";
|
|
386
|
+
case "kiro":
|
|
387
|
+
return "default";
|
|
388
|
+
case "anthropic":
|
|
389
|
+
return "claude-sonnet-5";
|
|
390
|
+
case "openai":
|
|
391
|
+
return "gpt-5.6-terra";
|
|
392
|
+
// Gemini 3.6 Flash is Google's own documented replacement for the retired
|
|
393
|
+
// gemini-2.0-flash, and Vertex takes the same bare ID as the Gemini API for
|
|
394
|
+
// GA models. Note this generation silently IGNORES temperature/topP rather
|
|
395
|
+
// than erroring -- the Google adapters send those two and nothing else, so
|
|
396
|
+
// no request breaks, but a user-supplied --temperature has no effect here.
|
|
397
|
+
case "gemini":
|
|
398
|
+
return "gemini-3.6-flash";
|
|
399
|
+
case "vertexai":
|
|
400
|
+
return "gemini-3.6-flash";
|
|
401
|
+
default:
|
|
402
|
+
return "default";
|
|
403
|
+
}
|
|
404
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* `ai list` — List available AI providers and their status.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import * as results from "@eserstack/primitives/results";
|
|
8
|
+
import * as streams from "@eserstack/streams";
|
|
9
|
+
import * as span from "@eserstack/streams/span";
|
|
10
|
+
import * as detect from "../detect.js";
|
|
11
|
+
// =============================================================================
|
|
12
|
+
// Main
|
|
13
|
+
// =============================================================================
|
|
14
|
+
export const main = async (_args) => {
|
|
15
|
+
const out = streams.output({
|
|
16
|
+
renderer: streams.renderers.ansi(),
|
|
17
|
+
sink: streams.sinks.stdout(),
|
|
18
|
+
});
|
|
19
|
+
out.writeln(span.bold("AI Providers"));
|
|
20
|
+
out.writeln("");
|
|
21
|
+
const providers = await detect.detectAllProviders();
|
|
22
|
+
const rows = providers.map((p) => [
|
|
23
|
+
p.available ? span.green("●") : span.dim("○"),
|
|
24
|
+
p.available
|
|
25
|
+
? span.bold(`${p.name} (${p.alias})`)
|
|
26
|
+
: span.dim(`${p.name} (${p.alias})`),
|
|
27
|
+
span.dim(p.type),
|
|
28
|
+
p.available ? p.detail : span.dim(p.detail),
|
|
29
|
+
]);
|
|
30
|
+
out.writeln(span.table(["", "Provider", "Type", "Status"], rows));
|
|
31
|
+
const availableCount = providers.filter((p) => p.available).length;
|
|
32
|
+
out.writeln(span.dim(`${availableCount}/${providers.length} providers available`));
|
|
33
|
+
await out.close();
|
|
34
|
+
return results.ok(undefined);
|
|
35
|
+
};
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type ConfigTarget = {
|
|
2
|
+
readonly provider: string;
|
|
3
|
+
readonly apiKey?: string;
|
|
4
|
+
readonly model: string;
|
|
5
|
+
readonly baseUrl?: string;
|
|
6
|
+
readonly maxTokens?: number;
|
|
7
|
+
readonly temperature?: number;
|
|
8
|
+
readonly requestTimeoutMs?: number;
|
|
9
|
+
readonly projectId?: string;
|
|
10
|
+
readonly location?: string;
|
|
11
|
+
readonly properties?: Record<string, unknown>;
|
|
12
|
+
};
|
|
13
|
+
export type Config = {
|
|
14
|
+
readonly targets: Record<string, ConfigTarget>;
|
|
15
|
+
};
|
|
16
|
+
export type ResolvedConfigTarget = ConfigTarget & {
|
|
17
|
+
readonly maxTokens: number;
|
|
18
|
+
readonly temperature: number;
|
|
19
|
+
readonly requestTimeoutMs: number;
|
|
20
|
+
};
|
|
21
|
+
export declare const withDefaults: (target: ConfigTarget) => ResolvedConfigTarget;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// Defaults
|
|
4
|
+
// =============================================================================
|
|
5
|
+
// A ceiling, not a target: thinking counts toward it on current Claude models.
|
|
6
|
+
const DEFAULT_MAX_TOKENS = 16_000;
|
|
7
|
+
const DEFAULT_TEMPERATURE = 0.7;
|
|
8
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 60_000;
|
|
9
|
+
export const withDefaults = (target) => {
|
|
10
|
+
return {
|
|
11
|
+
...target,
|
|
12
|
+
maxTokens: target.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
13
|
+
temperature: target.temperature ?? DEFAULT_TEMPERATURE,
|
|
14
|
+
requestTimeoutMs: target.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type * as types from "./types.ts";
|
|
2
|
+
export declare const textMessage: (role: types.Role, text: string) => types.Message;
|
|
3
|
+
export declare const imageMessage: (role: types.Role, imageUrl: string, detail?: types.ImageDetail) => types.Message;
|
|
4
|
+
export declare const audioMessage: (role: types.Role, audioUrl: string, mimeType?: string) => types.Message;
|
|
5
|
+
export declare const toolResultMessage: (toolCallId: string, content: string, isError?: boolean) => types.Message;
|
|
6
|
+
export declare const textBlock: (text: string) => types.TextBlock;
|
|
7
|
+
export declare const toolCallBlock: (id: string, name: string, args: Record<string, unknown>) => types.ToolCallBlock;
|
|
8
|
+
export declare const toolResultBlock: (toolCallId: string, content: string, isError?: boolean) => types.ToolResultBlock;
|
|
9
|
+
export declare const isDataUrl: (url: string) => boolean;
|
|
10
|
+
export declare const decodeDataUrl: (dataUrl: string) => {
|
|
11
|
+
readonly mimeType: string;
|
|
12
|
+
readonly data: Uint8Array;
|
|
13
|
+
} | null;
|
|
14
|
+
export declare const detectMimeFromUrl: (url: string) => string | null;
|
package/dist/content.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// Message Constructors
|
|
4
|
+
// =============================================================================
|
|
5
|
+
export const textMessage = (role, text) => {
|
|
6
|
+
return {
|
|
7
|
+
role,
|
|
8
|
+
content: [{ kind: "text", text }],
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export const imageMessage = (role, imageUrl, detail) => {
|
|
12
|
+
return {
|
|
13
|
+
role,
|
|
14
|
+
content: [{ kind: "image", image: { url: imageUrl, detail } }],
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export const audioMessage = (role, audioUrl, mimeType) => {
|
|
18
|
+
return {
|
|
19
|
+
role,
|
|
20
|
+
content: [{ kind: "audio", audio: { url: audioUrl, mimeType } }],
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export const toolResultMessage = (toolCallId, content, isError = false) => {
|
|
24
|
+
return {
|
|
25
|
+
role: "tool",
|
|
26
|
+
content: [
|
|
27
|
+
{
|
|
28
|
+
kind: "tool_result",
|
|
29
|
+
toolResult: { toolCallId, content, isError },
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
// =============================================================================
|
|
35
|
+
// Content Block Constructors
|
|
36
|
+
// =============================================================================
|
|
37
|
+
export const textBlock = (text) => {
|
|
38
|
+
return { kind: "text", text };
|
|
39
|
+
};
|
|
40
|
+
export const toolCallBlock = (id, name, args) => {
|
|
41
|
+
return { kind: "tool_call", toolCall: { id, name, arguments: args } };
|
|
42
|
+
};
|
|
43
|
+
export const toolResultBlock = (toolCallId, content, isError = false) => {
|
|
44
|
+
return { kind: "tool_result", toolResult: { toolCallId, content, isError } };
|
|
45
|
+
};
|
|
46
|
+
// =============================================================================
|
|
47
|
+
// Data URL Utilities
|
|
48
|
+
// =============================================================================
|
|
49
|
+
const DATA_URL_PREFIX = "data:";
|
|
50
|
+
const BASE64_MARKER = ";base64,";
|
|
51
|
+
export const isDataUrl = (url) => {
|
|
52
|
+
return url.startsWith(DATA_URL_PREFIX);
|
|
53
|
+
};
|
|
54
|
+
export const decodeDataUrl = (dataUrl) => {
|
|
55
|
+
if (!dataUrl.startsWith(DATA_URL_PREFIX)) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const rest = dataUrl.slice(DATA_URL_PREFIX.length);
|
|
59
|
+
const base64Index = rest.indexOf(BASE64_MARKER);
|
|
60
|
+
if (base64Index === -1) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const mimeType = rest.slice(0, base64Index);
|
|
64
|
+
const base64Data = rest.slice(base64Index + BASE64_MARKER.length);
|
|
65
|
+
const binaryString = atob(base64Data);
|
|
66
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
67
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
68
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
69
|
+
}
|
|
70
|
+
return { mimeType, data: bytes };
|
|
71
|
+
};
|
|
72
|
+
// =============================================================================
|
|
73
|
+
// MIME Detection
|
|
74
|
+
// =============================================================================
|
|
75
|
+
// A Map, so an extension such as "constructor" or "__proto__" finds no entry
|
|
76
|
+
// instead of an Object.prototype member.
|
|
77
|
+
const EXTENSION_MIME_MAP = new Map([
|
|
78
|
+
["png", "image/png"],
|
|
79
|
+
["jpg", "image/jpeg"],
|
|
80
|
+
["jpeg", "image/jpeg"],
|
|
81
|
+
["gif", "image/gif"],
|
|
82
|
+
["webp", "image/webp"],
|
|
83
|
+
["svg", "image/svg+xml"],
|
|
84
|
+
["bmp", "image/bmp"],
|
|
85
|
+
["mp3", "audio/mpeg"],
|
|
86
|
+
["wav", "audio/wav"],
|
|
87
|
+
["ogg", "audio/ogg"],
|
|
88
|
+
["flac", "audio/flac"],
|
|
89
|
+
["m4a", "audio/mp4"],
|
|
90
|
+
["webm", "audio/webm"],
|
|
91
|
+
["pdf", "application/pdf"],
|
|
92
|
+
["json", "application/json"],
|
|
93
|
+
["txt", "text/plain"],
|
|
94
|
+
]);
|
|
95
|
+
export const detectMimeFromUrl = (url) => {
|
|
96
|
+
const urlWithoutQuery = url.split("?")[0] ?? url;
|
|
97
|
+
const lastDot = urlWithoutQuery.lastIndexOf(".");
|
|
98
|
+
if (lastDot === -1) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
const extension = urlWithoutQuery.slice(lastDot + 1).toLowerCase();
|
|
102
|
+
return EXTENSION_MIME_MAP.get(extension) ?? null;
|
|
103
|
+
};
|
package/dist/detect.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type ProviderStatus = {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly alias: string;
|
|
4
|
+
readonly type: string;
|
|
5
|
+
readonly available: boolean;
|
|
6
|
+
readonly detail: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const detectAllProviders: () => Promise<readonly ProviderStatus[]>;
|
|
9
|
+
export declare const getAvailableProviderNames: () => Promise<readonly string[]>;
|