@devang0907/agent-dev 0.1.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 (52) hide show
  1. package/README.md +58 -0
  2. package/dist/agent/loop.d.ts +32 -0
  3. package/dist/agent/loop.js +83 -0
  4. package/dist/agent/session.d.ts +33 -0
  5. package/dist/agent/session.js +107 -0
  6. package/dist/agent/tools/index.d.ts +8 -0
  7. package/dist/agent/tools/index.js +21 -0
  8. package/dist/agent/tools/read.d.ts +20 -0
  9. package/dist/agent/tools/read.js +108 -0
  10. package/dist/cli/args.d.ts +9 -0
  11. package/dist/cli/args.js +60 -0
  12. package/dist/cli.d.ts +2 -0
  13. package/dist/cli.js +6 -0
  14. package/dist/config/models.d.ts +6 -0
  15. package/dist/config/models.js +43 -0
  16. package/dist/config/paths.d.ts +4 -0
  17. package/dist/config/paths.js +6 -0
  18. package/dist/config/settings.d.ts +12 -0
  19. package/dist/config/settings.js +29 -0
  20. package/dist/main.d.ts +1 -0
  21. package/dist/main.js +45 -0
  22. package/dist/modes/print-mode.d.ts +2 -0
  23. package/dist/modes/print-mode.js +32 -0
  24. package/dist/providers/gemini.d.ts +10 -0
  25. package/dist/providers/gemini.js +116 -0
  26. package/dist/providers/groq.d.ts +10 -0
  27. package/dist/providers/groq.js +113 -0
  28. package/dist/providers/openai.d.ts +10 -0
  29. package/dist/providers/openai.js +121 -0
  30. package/dist/providers/openrouter-free.d.ts +10 -0
  31. package/dist/providers/openrouter-free.js +133 -0
  32. package/dist/providers/registry.d.ts +7 -0
  33. package/dist/providers/registry.js +39 -0
  34. package/dist/providers/types.d.ts +60 -0
  35. package/dist/providers/types.js +1 -0
  36. package/dist/session/manager.d.ts +25 -0
  37. package/dist/session/manager.js +85 -0
  38. package/dist/ui/App.d.ts +14 -0
  39. package/dist/ui/App.js +131 -0
  40. package/dist/ui/ChatView.d.ts +10 -0
  41. package/dist/ui/ChatView.js +5 -0
  42. package/dist/ui/Editor.d.ts +10 -0
  43. package/dist/ui/Editor.js +35 -0
  44. package/dist/ui/Footer.d.ts +11 -0
  45. package/dist/ui/Footer.js +6 -0
  46. package/dist/ui/ModelSelector.d.ts +13 -0
  47. package/dist/ui/ModelSelector.js +42 -0
  48. package/dist/ui/SettingsView.d.ts +11 -0
  49. package/dist/ui/SettingsView.js +37 -0
  50. package/dist/ui/theme.d.ts +13 -0
  51. package/dist/ui/theme.js +25 -0
  52. package/package.json +42 -0
package/dist/main.js ADDED
@@ -0,0 +1,45 @@
1
+ import { render } from "ink";
2
+ import React from "react";
3
+ import { loadSettings } from "./config/settings.js";
4
+ import { SessionManager } from "./session/manager.js";
5
+ import { AgentSession } from "./agent/session.js";
6
+ import { parseArgs, printHelp } from "./cli/args.js";
7
+ import { runPrintMode } from "./modes/print-mode.js";
8
+ import { App } from "./ui/App.js";
9
+ export async function main() {
10
+ const args = parseArgs(process.argv);
11
+ if (args.help) {
12
+ printHelp();
13
+ return;
14
+ }
15
+ const settings = loadSettings();
16
+ const workdir = process.cwd();
17
+ let sessionManager;
18
+ if (args.continueSession) {
19
+ sessionManager = SessionManager.loadLast() ?? new SessionManager(undefined, workdir);
20
+ }
21
+ else {
22
+ sessionManager = new SessionManager(undefined, workdir);
23
+ }
24
+ const initialModel = AgentSession.resolveInitialModel(settings, args.model);
25
+ const session = new AgentSession(settings, sessionManager, workdir, initialModel);
26
+ const usePrint = args.print || !process.stdin.isTTY || !process.stdout.isTTY;
27
+ if (usePrint) {
28
+ const prompt = args.prompt ?? "";
29
+ if (!prompt) {
30
+ console.error("Print mode requires a prompt argument.");
31
+ process.exit(1);
32
+ }
33
+ await runPrintMode(session, prompt);
34
+ return;
35
+ }
36
+ const { waitUntilExit } = render(React.createElement(App, {
37
+ session,
38
+ workdir,
39
+ onQuit: () => { },
40
+ }));
41
+ if (args.prompt) {
42
+ setTimeout(() => session.prompt(args.prompt), 100);
43
+ }
44
+ await waitUntilExit();
45
+ }
@@ -0,0 +1,2 @@
1
+ import type { AgentSession } from "../agent/session.js";
2
+ export declare function runPrintMode(session: AgentSession, prompt: string): Promise<void>;
@@ -0,0 +1,32 @@
1
+ import chalk from "chalk";
2
+ import { runAgentLoop } from "../agent/loop.js";
3
+ export async function runPrintMode(session, prompt) {
4
+ const model = session.getModel();
5
+ console.log(chalk.gray(`Model: ${model.provider}/${model.id}`));
6
+ const userMsg = { role: "user", content: prompt };
7
+ const prior = session.getMessages();
8
+ let output = "";
9
+ await runAgentLoop({
10
+ model,
11
+ messages: [...prior, userMsg],
12
+ settings: session.getSettings(),
13
+ workdir: process.cwd(),
14
+ onEvent: (event) => {
15
+ if (event.type === "text_delta") {
16
+ process.stdout.write(event.delta);
17
+ output += event.delta;
18
+ }
19
+ else if (event.type === "tool_call") {
20
+ console.log(chalk.yellow(`\n[tool: ${event.toolCall.name}]`));
21
+ }
22
+ else if (event.type === "tool_result") {
23
+ console.log(chalk.gray(event.result.slice(0, 500)));
24
+ }
25
+ else if (event.type === "error") {
26
+ console.error(chalk.red(event.message));
27
+ }
28
+ },
29
+ });
30
+ if (!output.endsWith("\n"))
31
+ console.log();
32
+ }
@@ -0,0 +1,10 @@
1
+ import type { ChatContext, Model, StreamEvent } from "./types.js";
2
+ import type { Settings } from "../config/settings.js";
3
+ export declare const PROVIDER_ID: "gemini";
4
+ export declare const DEFAULT_MODEL = "gemini-2.0-flash";
5
+ export declare const API_KEY_ENV = "GEMINI_API_KEY";
6
+ export declare const API_KEY_ENV_ALT = "GOOGLE_API_KEY";
7
+ export declare const MODELS: Model[];
8
+ export declare function getApiKey(settings?: Settings): string | undefined;
9
+ export declare function hasAuth(settings?: Settings): boolean;
10
+ export declare function streamChat(model: Model, ctx: ChatContext, settings?: Settings): AsyncGenerator<StreamEvent>;
@@ -0,0 +1,116 @@
1
+ import { GoogleGenAI } from "@google/genai";
2
+ export const PROVIDER_ID = "gemini";
3
+ export const DEFAULT_MODEL = "gemini-2.0-flash";
4
+ export const API_KEY_ENV = "GEMINI_API_KEY";
5
+ export const API_KEY_ENV_ALT = "GOOGLE_API_KEY";
6
+ export const MODELS = [
7
+ { provider: PROVIDER_ID, id: "gemini-2.0-flash", name: "Gemini 2.0 Flash" },
8
+ { provider: PROVIDER_ID, id: "gemini-2.5-flash", name: "Gemini 2.5 Flash" },
9
+ ];
10
+ export function getApiKey(settings) {
11
+ return (process.env[API_KEY_ENV] ??
12
+ process.env[API_KEY_ENV_ALT] ??
13
+ settings?.apiKeys?.gemini);
14
+ }
15
+ export function hasAuth(settings) {
16
+ return !!getApiKey(settings);
17
+ }
18
+ function toFunctionDeclarations(tools) {
19
+ return tools.map((t) => ({
20
+ name: t.name,
21
+ description: t.description,
22
+ parametersJsonSchema: t.parameters,
23
+ }));
24
+ }
25
+ function toGeminiContents(messages) {
26
+ const contents = [];
27
+ for (const m of messages) {
28
+ if (m.role === "user") {
29
+ contents.push({ role: "user", parts: [{ text: m.content }] });
30
+ }
31
+ else if (m.role === "assistant") {
32
+ const parts = [];
33
+ if (m.content)
34
+ parts.push({ text: m.content });
35
+ if (m.toolCalls) {
36
+ for (const tc of m.toolCalls) {
37
+ parts.push({
38
+ functionCall: {
39
+ name: tc.name,
40
+ args: JSON.parse(tc.arguments || "{}"),
41
+ },
42
+ });
43
+ }
44
+ }
45
+ if (parts.length > 0)
46
+ contents.push({ role: "model", parts });
47
+ }
48
+ else if (m.role === "tool") {
49
+ contents.push({
50
+ role: "user",
51
+ parts: [{
52
+ functionResponse: {
53
+ name: m.name ?? "tool",
54
+ response: { result: m.content },
55
+ },
56
+ }],
57
+ });
58
+ }
59
+ }
60
+ return contents;
61
+ }
62
+ export async function* streamChat(model, ctx, settings) {
63
+ const apiKey = getApiKey(settings);
64
+ if (!apiKey) {
65
+ yield { type: "error", message: `Missing ${API_KEY_ENV} or ${API_KEY_ENV_ALT}` };
66
+ return;
67
+ }
68
+ const ai = new GoogleGenAI({ apiKey });
69
+ try {
70
+ const stream = await ai.models.generateContentStream({
71
+ model: model.id,
72
+ contents: toGeminiContents(ctx.messages),
73
+ config: {
74
+ systemInstruction: ctx.systemPrompt,
75
+ tools: ctx.tools.length > 0
76
+ ? [{ functionDeclarations: toFunctionDeclarations(ctx.tools) }]
77
+ : undefined,
78
+ abortSignal: ctx.signal,
79
+ },
80
+ });
81
+ const toolCalls = new Map();
82
+ let toolIndex = 0;
83
+ for await (const chunk of stream) {
84
+ const parts = chunk.candidates?.[0]?.content?.parts ?? [];
85
+ for (const part of parts) {
86
+ if (part.text) {
87
+ yield { type: "text_delta", delta: part.text };
88
+ }
89
+ if (part.functionCall) {
90
+ const fc = part.functionCall;
91
+ const idx = toolIndex++;
92
+ const args = JSON.stringify(fc.args ?? {});
93
+ toolCalls.set(idx, { id: `gemini_${idx}`, name: fc.name ?? "", arguments: args });
94
+ yield {
95
+ type: "tool_call_delta",
96
+ index: idx,
97
+ id: `gemini_${idx}`,
98
+ name: fc.name,
99
+ argumentsDelta: args,
100
+ };
101
+ }
102
+ }
103
+ if (chunk.usageMetadata) {
104
+ // usage available on final chunk
105
+ }
106
+ }
107
+ yield {
108
+ type: "done",
109
+ usage: undefined,
110
+ };
111
+ }
112
+ catch (err) {
113
+ const message = err instanceof Error ? err.message : String(err);
114
+ yield { type: "error", message };
115
+ }
116
+ }
@@ -0,0 +1,10 @@
1
+ import type { ChatContext, Model, StreamEvent } from "./types.js";
2
+ import type { Settings } from "../config/settings.js";
3
+ export declare const PROVIDER_ID: "groq";
4
+ export declare const DEFAULT_MODEL = "llama-3.3-70b-versatile";
5
+ export declare const API_KEY_ENV = "GROQ_API_KEY";
6
+ export declare const BASE_URL = "https://api.groq.com/openai/v1";
7
+ export declare const MODELS: Model[];
8
+ export declare function getApiKey(settings?: Settings): string | undefined;
9
+ export declare function hasAuth(settings?: Settings): boolean;
10
+ export declare function streamChat(model: Model, ctx: ChatContext, settings?: Settings): AsyncGenerator<StreamEvent>;
@@ -0,0 +1,113 @@
1
+ import OpenAI from "openai";
2
+ export const PROVIDER_ID = "groq";
3
+ export const DEFAULT_MODEL = "llama-3.3-70b-versatile";
4
+ export const API_KEY_ENV = "GROQ_API_KEY";
5
+ export const BASE_URL = "https://api.groq.com/openai/v1";
6
+ export const MODELS = [
7
+ { provider: PROVIDER_ID, id: "llama-3.3-70b-versatile", name: "Llama 3.3 70B" },
8
+ { provider: PROVIDER_ID, id: "openai/gpt-oss-120b", name: "GPT-OSS 120B" },
9
+ ];
10
+ export function getApiKey(settings) {
11
+ return process.env[API_KEY_ENV] ?? settings?.apiKeys?.groq;
12
+ }
13
+ export function hasAuth(settings) {
14
+ return !!getApiKey(settings);
15
+ }
16
+ function toOpenAIMessages(ctx) {
17
+ const msgs = [];
18
+ if (ctx.systemPrompt) {
19
+ msgs.push({ role: "system", content: ctx.systemPrompt });
20
+ }
21
+ for (const m of ctx.messages) {
22
+ if (m.role === "user") {
23
+ msgs.push({ role: "user", content: m.content });
24
+ }
25
+ else if (m.role === "assistant") {
26
+ if (m.toolCalls?.length) {
27
+ msgs.push({
28
+ role: "assistant",
29
+ content: m.content || null,
30
+ tool_calls: m.toolCalls.map((tc) => ({
31
+ id: tc.id,
32
+ type: "function",
33
+ function: { name: tc.name, arguments: tc.arguments },
34
+ })),
35
+ });
36
+ }
37
+ else {
38
+ msgs.push({ role: "assistant", content: m.content });
39
+ }
40
+ }
41
+ else if (m.role === "tool") {
42
+ msgs.push({
43
+ role: "tool",
44
+ tool_call_id: m.toolCallId,
45
+ content: m.content,
46
+ });
47
+ }
48
+ }
49
+ return msgs;
50
+ }
51
+ function toOpenAITools(tools) {
52
+ return tools.map((t) => ({
53
+ type: "function",
54
+ function: {
55
+ name: t.name,
56
+ description: t.description,
57
+ parameters: t.parameters,
58
+ },
59
+ }));
60
+ }
61
+ export async function* streamChat(model, ctx, settings) {
62
+ const apiKey = getApiKey(settings);
63
+ if (!apiKey) {
64
+ yield { type: "error", message: `Missing ${API_KEY_ENV}` };
65
+ return;
66
+ }
67
+ const client = new OpenAI({ apiKey, baseURL: BASE_URL });
68
+ try {
69
+ const stream = await client.chat.completions.create({
70
+ model: model.id,
71
+ messages: toOpenAIMessages(ctx),
72
+ tools: ctx.tools.length > 0 ? toOpenAITools(ctx.tools) : undefined,
73
+ stream: true,
74
+ }, { signal: ctx.signal });
75
+ const toolCalls = new Map();
76
+ for await (const chunk of stream) {
77
+ const delta = chunk.choices[0]?.delta;
78
+ if (!delta)
79
+ continue;
80
+ if (delta.content) {
81
+ yield { type: "text_delta", delta: delta.content };
82
+ }
83
+ if (delta.tool_calls) {
84
+ for (const tc of delta.tool_calls) {
85
+ const idx = tc.index;
86
+ if (!toolCalls.has(idx)) {
87
+ toolCalls.set(idx, { id: tc.id ?? "", name: tc.function?.name ?? "", arguments: "" });
88
+ }
89
+ const existing = toolCalls.get(idx);
90
+ if (tc.id)
91
+ existing.id = tc.id;
92
+ if (tc.function?.name)
93
+ existing.name = tc.function.name;
94
+ if (tc.function?.arguments) {
95
+ existing.arguments += tc.function.arguments;
96
+ yield {
97
+ type: "tool_call_delta",
98
+ index: idx,
99
+ id: existing.id,
100
+ name: existing.name,
101
+ argumentsDelta: tc.function.arguments,
102
+ };
103
+ }
104
+ }
105
+ }
106
+ }
107
+ yield { type: "done" };
108
+ }
109
+ catch (err) {
110
+ const message = err instanceof Error ? err.message : String(err);
111
+ yield { type: "error", message };
112
+ }
113
+ }
@@ -0,0 +1,10 @@
1
+ import type { ChatContext, Model, StreamEvent } from "./types.js";
2
+ import type { Settings } from "../config/settings.js";
3
+ export declare const PROVIDER_ID: "openai";
4
+ export declare const DEFAULT_MODEL = "gpt-4o";
5
+ export declare const API_KEY_ENV = "OPENAI_API_KEY";
6
+ export declare const BASE_URL = "https://api.openai.com/v1";
7
+ export declare const MODELS: Model[];
8
+ export declare function getApiKey(settings?: Settings): string | undefined;
9
+ export declare function hasAuth(settings?: Settings): boolean;
10
+ export declare function streamChat(model: Model, ctx: ChatContext, settings?: Settings): AsyncGenerator<StreamEvent>;
@@ -0,0 +1,121 @@
1
+ import OpenAI from "openai";
2
+ export const PROVIDER_ID = "openai";
3
+ export const DEFAULT_MODEL = "gpt-4o";
4
+ export const API_KEY_ENV = "OPENAI_API_KEY";
5
+ export const BASE_URL = "https://api.openai.com/v1";
6
+ export const MODELS = [
7
+ { provider: PROVIDER_ID, id: "gpt-4o", name: "GPT-4o" },
8
+ { provider: PROVIDER_ID, id: "gpt-4o-mini", name: "GPT-4o Mini" },
9
+ ];
10
+ export function getApiKey(settings) {
11
+ return process.env[API_KEY_ENV] ?? settings?.apiKeys?.openai;
12
+ }
13
+ export function hasAuth(settings) {
14
+ return !!getApiKey(settings);
15
+ }
16
+ function toOpenAIMessages(ctx) {
17
+ const msgs = [];
18
+ if (ctx.systemPrompt) {
19
+ msgs.push({ role: "system", content: ctx.systemPrompt });
20
+ }
21
+ for (const m of ctx.messages) {
22
+ if (m.role === "user") {
23
+ msgs.push({ role: "user", content: m.content });
24
+ }
25
+ else if (m.role === "assistant") {
26
+ if (m.toolCalls?.length) {
27
+ msgs.push({
28
+ role: "assistant",
29
+ content: m.content || null,
30
+ tool_calls: m.toolCalls.map((tc) => ({
31
+ id: tc.id,
32
+ type: "function",
33
+ function: { name: tc.name, arguments: tc.arguments },
34
+ })),
35
+ });
36
+ }
37
+ else {
38
+ msgs.push({ role: "assistant", content: m.content });
39
+ }
40
+ }
41
+ else if (m.role === "tool") {
42
+ msgs.push({
43
+ role: "tool",
44
+ tool_call_id: m.toolCallId,
45
+ content: m.content,
46
+ });
47
+ }
48
+ }
49
+ return msgs;
50
+ }
51
+ function toOpenAITools(tools) {
52
+ return tools.map((t) => ({
53
+ type: "function",
54
+ function: {
55
+ name: t.name,
56
+ description: t.description,
57
+ parameters: t.parameters,
58
+ },
59
+ }));
60
+ }
61
+ export async function* streamChat(model, ctx, settings) {
62
+ const apiKey = getApiKey(settings);
63
+ if (!apiKey) {
64
+ yield { type: "error", message: `Missing ${API_KEY_ENV}` };
65
+ return;
66
+ }
67
+ const client = new OpenAI({ apiKey, baseURL: BASE_URL });
68
+ try {
69
+ const stream = await client.chat.completions.create({
70
+ model: model.id,
71
+ messages: toOpenAIMessages(ctx),
72
+ tools: ctx.tools.length > 0 ? toOpenAITools(ctx.tools) : undefined,
73
+ stream: true,
74
+ stream_options: { include_usage: true },
75
+ }, { signal: ctx.signal });
76
+ const toolCalls = new Map();
77
+ let usage;
78
+ for await (const chunk of stream) {
79
+ if (chunk.usage) {
80
+ usage = {
81
+ inputTokens: chunk.usage.prompt_tokens,
82
+ outputTokens: chunk.usage.completion_tokens,
83
+ };
84
+ }
85
+ const delta = chunk.choices[0]?.delta;
86
+ if (!delta)
87
+ continue;
88
+ if (delta.content) {
89
+ yield { type: "text_delta", delta: delta.content };
90
+ }
91
+ if (delta.tool_calls) {
92
+ for (const tc of delta.tool_calls) {
93
+ const idx = tc.index;
94
+ if (!toolCalls.has(idx)) {
95
+ toolCalls.set(idx, { id: tc.id ?? "", name: tc.function?.name ?? "", arguments: "" });
96
+ }
97
+ const existing = toolCalls.get(idx);
98
+ if (tc.id)
99
+ existing.id = tc.id;
100
+ if (tc.function?.name)
101
+ existing.name = tc.function.name;
102
+ if (tc.function?.arguments) {
103
+ existing.arguments += tc.function.arguments;
104
+ yield {
105
+ type: "tool_call_delta",
106
+ index: idx,
107
+ id: existing.id,
108
+ name: existing.name,
109
+ argumentsDelta: tc.function.arguments,
110
+ };
111
+ }
112
+ }
113
+ }
114
+ }
115
+ yield { type: "done", usage };
116
+ }
117
+ catch (err) {
118
+ const message = err instanceof Error ? err.message : String(err);
119
+ yield { type: "error", message };
120
+ }
121
+ }
@@ -0,0 +1,10 @@
1
+ import type { ChatContext, Model, StreamEvent } from "./types.js";
2
+ import type { Settings } from "../config/settings.js";
3
+ export declare const PROVIDER_ID: "free";
4
+ export declare const DEFAULT_MODEL = "meta-llama/llama-3.3-70b-instruct:free";
5
+ export declare const API_KEY_ENV = "OPENROUTER_API_KEY";
6
+ export declare const BASE_URL = "https://openrouter.ai/api/v1";
7
+ export declare const MODELS: Model[];
8
+ export declare function getApiKey(settings?: Settings): string | undefined;
9
+ export declare function hasAuth(settings?: Settings): boolean;
10
+ export declare function streamChat(model: Model, ctx: ChatContext, settings?: Settings): AsyncGenerator<StreamEvent>;
@@ -0,0 +1,133 @@
1
+ import OpenAI from "openai";
2
+ export const PROVIDER_ID = "free";
3
+ export const DEFAULT_MODEL = "meta-llama/llama-3.3-70b-instruct:free";
4
+ export const API_KEY_ENV = "OPENROUTER_API_KEY";
5
+ export const BASE_URL = "https://openrouter.ai/api/v1";
6
+ export const MODELS = [
7
+ {
8
+ provider: PROVIDER_ID,
9
+ id: "meta-llama/llama-3.3-70b-instruct:free",
10
+ name: "Llama 3.3 70B (free)",
11
+ },
12
+ {
13
+ provider: PROVIDER_ID,
14
+ id: "google/gemini-2.0-flash-exp:free",
15
+ name: "Gemini 2.0 Flash (free)",
16
+ },
17
+ {
18
+ provider: PROVIDER_ID,
19
+ id: "qwen/qwen-2.5-72b-instruct:free",
20
+ name: "Qwen 2.5 72B (free)",
21
+ },
22
+ ];
23
+ export function getApiKey(settings) {
24
+ return process.env[API_KEY_ENV] ?? settings?.apiKeys?.free;
25
+ }
26
+ export function hasAuth(settings) {
27
+ return !!getApiKey(settings);
28
+ }
29
+ function toOpenAIMessages(ctx) {
30
+ const msgs = [];
31
+ if (ctx.systemPrompt) {
32
+ msgs.push({ role: "system", content: ctx.systemPrompt });
33
+ }
34
+ for (const m of ctx.messages) {
35
+ if (m.role === "user") {
36
+ msgs.push({ role: "user", content: m.content });
37
+ }
38
+ else if (m.role === "assistant") {
39
+ if (m.toolCalls?.length) {
40
+ msgs.push({
41
+ role: "assistant",
42
+ content: m.content || null,
43
+ tool_calls: m.toolCalls.map((tc) => ({
44
+ id: tc.id,
45
+ type: "function",
46
+ function: { name: tc.name, arguments: tc.arguments },
47
+ })),
48
+ });
49
+ }
50
+ else {
51
+ msgs.push({ role: "assistant", content: m.content });
52
+ }
53
+ }
54
+ else if (m.role === "tool") {
55
+ msgs.push({
56
+ role: "tool",
57
+ tool_call_id: m.toolCallId,
58
+ content: m.content,
59
+ });
60
+ }
61
+ }
62
+ return msgs;
63
+ }
64
+ function toOpenAITools(tools) {
65
+ return tools.map((t) => ({
66
+ type: "function",
67
+ function: {
68
+ name: t.name,
69
+ description: t.description,
70
+ parameters: t.parameters,
71
+ },
72
+ }));
73
+ }
74
+ export async function* streamChat(model, ctx, settings) {
75
+ const apiKey = getApiKey(settings);
76
+ if (!apiKey) {
77
+ yield { type: "error", message: `Missing ${API_KEY_ENV}` };
78
+ return;
79
+ }
80
+ const client = new OpenAI({
81
+ apiKey,
82
+ baseURL: BASE_URL,
83
+ defaultHeaders: {
84
+ "HTTP-Referer": "https://github.com/agent-dev",
85
+ "X-Title": "agent-dev",
86
+ },
87
+ });
88
+ try {
89
+ const stream = await client.chat.completions.create({
90
+ model: model.id,
91
+ messages: toOpenAIMessages(ctx),
92
+ tools: ctx.tools.length > 0 ? toOpenAITools(ctx.tools) : undefined,
93
+ stream: true,
94
+ }, { signal: ctx.signal });
95
+ const toolCalls = new Map();
96
+ for await (const chunk of stream) {
97
+ const delta = chunk.choices[0]?.delta;
98
+ if (!delta)
99
+ continue;
100
+ if (delta.content) {
101
+ yield { type: "text_delta", delta: delta.content };
102
+ }
103
+ if (delta.tool_calls) {
104
+ for (const tc of delta.tool_calls) {
105
+ const idx = tc.index;
106
+ if (!toolCalls.has(idx)) {
107
+ toolCalls.set(idx, { id: tc.id ?? "", name: tc.function?.name ?? "", arguments: "" });
108
+ }
109
+ const existing = toolCalls.get(idx);
110
+ if (tc.id)
111
+ existing.id = tc.id;
112
+ if (tc.function?.name)
113
+ existing.name = tc.function.name;
114
+ if (tc.function?.arguments) {
115
+ existing.arguments += tc.function.arguments;
116
+ yield {
117
+ type: "tool_call_delta",
118
+ index: idx,
119
+ id: existing.id,
120
+ name: existing.name,
121
+ argumentsDelta: tc.function.arguments,
122
+ };
123
+ }
124
+ }
125
+ }
126
+ }
127
+ yield { type: "done" };
128
+ }
129
+ catch (err) {
130
+ const message = err instanceof Error ? err.message : String(err);
131
+ yield { type: "error", message };
132
+ }
133
+ }
@@ -0,0 +1,7 @@
1
+ import type { ChatContext, Model, ProviderId, StreamEvent } from "./types.js";
2
+ import type { Settings } from "../config/settings.js";
3
+ export declare function hasProviderAuth(provider: ProviderId, settings?: Settings): boolean;
4
+ export declare function getAvailableModels(settings?: Settings): Model[];
5
+ export declare function streamChat(model: Model, ctx: ChatContext, settings?: Settings): AsyncGenerator<StreamEvent>;
6
+ export declare function getDefaultModelForProvider(provider: ProviderId): Model | undefined;
7
+ export declare const PROVIDER_ENV_VARS: Record<ProviderId, string[]>;
@@ -0,0 +1,39 @@
1
+ import * as openai from "./openai.js";
2
+ import * as groq from "./groq.js";
3
+ import * as gemini from "./gemini.js";
4
+ import * as openrouterFree from "./openrouter-free.js";
5
+ import { ALL_MODELS } from "../config/models.js";
6
+ const PROVIDERS = {
7
+ openai: openai,
8
+ groq: groq,
9
+ gemini: gemini,
10
+ free: openrouterFree,
11
+ };
12
+ export function hasProviderAuth(provider, settings) {
13
+ return PROVIDERS[provider].hasAuth(settings);
14
+ }
15
+ export function getAvailableModels(settings) {
16
+ return ALL_MODELS.filter((m) => hasProviderAuth(m.provider, settings));
17
+ }
18
+ export function streamChat(model, ctx, settings) {
19
+ const provider = PROVIDERS[model.provider];
20
+ return provider.streamChat(model, ctx, settings);
21
+ }
22
+ export function getDefaultModelForProvider(provider) {
23
+ switch (provider) {
24
+ case "openai":
25
+ return openai.MODELS[0];
26
+ case "groq":
27
+ return groq.MODELS[0];
28
+ case "gemini":
29
+ return gemini.MODELS[0];
30
+ case "free":
31
+ return openrouterFree.MODELS[0];
32
+ }
33
+ }
34
+ export const PROVIDER_ENV_VARS = {
35
+ openai: [openai.API_KEY_ENV],
36
+ groq: [groq.API_KEY_ENV],
37
+ gemini: [gemini.API_KEY_ENV, gemini.API_KEY_ENV_ALT],
38
+ free: [openrouterFree.API_KEY_ENV],
39
+ };