wide-cli 0.3.101126 → 0.5.161126
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/README.md +6 -5
- package/dist/agent/context.d.ts.map +1 -1
- package/dist/agent/context.js +5 -1
- package/dist/agent/context.js.map +1 -1
- package/dist/agent/index.d.ts +33 -4
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/index.js +213 -79
- package/dist/agent/index.js.map +1 -1
- package/dist/completion.d.ts +13 -0
- package/dist/completion.d.ts.map +1 -0
- package/dist/completion.js +170 -0
- package/dist/completion.js.map +1 -0
- package/dist/config/index.d.ts +4 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +35 -7
- package/dist/config/index.js.map +1 -1
- package/dist/config/logger.d.ts +4 -0
- package/dist/config/logger.d.ts.map +1 -0
- package/dist/config/logger.js +13 -0
- package/dist/config/logger.js.map +1 -0
- package/dist/decision/index.d.ts.map +1 -1
- package/dist/decision/index.js +9 -7
- package/dist/decision/index.js.map +1 -1
- package/dist/index.js +205 -40
- package/dist/index.js.map +1 -1
- package/dist/providers/claude.d.ts +2 -2
- package/dist/providers/claude.d.ts.map +1 -1
- package/dist/providers/claude.js +54 -38
- package/dist/providers/claude.js.map +1 -1
- package/dist/providers/gemini.d.ts +2 -2
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +50 -34
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/index.d.ts +6 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +26 -7
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/ollama.d.ts +7 -2
- package/dist/providers/ollama.d.ts.map +1 -1
- package/dist/providers/ollama.js +90 -15
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.d.ts +2 -2
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +60 -44
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/retry.d.ts +7 -0
- package/dist/providers/retry.d.ts.map +1 -0
- package/dist/providers/retry.js +72 -0
- package/dist/providers/retry.js.map +1 -0
- package/dist/providers/types.d.ts +2 -1
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/repl/commands.d.ts +18 -0
- package/dist/repl/commands.d.ts.map +1 -0
- package/dist/repl/commands.js +351 -0
- package/dist/repl/commands.js.map +1 -0
- package/dist/repl/history.d.ts +3 -0
- package/dist/repl/history.d.ts.map +1 -0
- package/dist/repl/history.js +57 -0
- package/dist/repl/history.js.map +1 -0
- package/dist/repl/index.d.ts +9 -1
- package/dist/repl/index.d.ts.map +1 -1
- package/dist/repl/index.js +326 -75
- package/dist/repl/index.js.map +1 -1
- package/dist/repl/mentions.d.ts +8 -0
- package/dist/repl/mentions.d.ts.map +1 -0
- package/dist/repl/mentions.js +100 -0
- package/dist/repl/mentions.js.map +1 -0
- package/dist/repl/sessions.d.ts +21 -0
- package/dist/repl/sessions.d.ts.map +1 -0
- package/dist/repl/sessions.js +117 -0
- package/dist/repl/sessions.js.map +1 -0
- package/dist/repl/setup.d.ts +4 -0
- package/dist/repl/setup.d.ts.map +1 -0
- package/dist/repl/setup.js +73 -0
- package/dist/repl/setup.js.map +1 -0
- package/dist/security/fuzzer.d.ts +11 -1
- package/dist/security/fuzzer.d.ts.map +1 -1
- package/dist/security/fuzzer.js +131 -60
- package/dist/security/fuzzer.js.map +1 -1
- package/dist/security/loader.d.ts +15 -1
- package/dist/security/loader.d.ts.map +1 -1
- package/dist/security/loader.js +251 -11
- package/dist/security/loader.js.map +1 -1
- package/dist/security/rules.d.ts +2 -0
- package/dist/security/rules.d.ts.map +1 -1
- package/dist/security/rules.js +108 -15
- package/dist/security/rules.js.map +1 -1
- package/dist/security/sca.d.ts +39 -0
- package/dist/security/sca.d.ts.map +1 -0
- package/dist/security/sca.js +464 -0
- package/dist/security/sca.js.map +1 -0
- package/dist/security/scanner.d.ts +19 -1
- package/dist/security/scanner.d.ts.map +1 -1
- package/dist/security/scanner.js +294 -77
- package/dist/security/scanner.js.map +1 -1
- package/dist/security/secrets.d.ts +5 -0
- package/dist/security/secrets.d.ts.map +1 -0
- package/dist/security/secrets.js +109 -0
- package/dist/security/secrets.js.map +1 -0
- package/dist/security/taint.d.ts +14 -0
- package/dist/security/taint.d.ts.map +1 -0
- package/dist/security/taint.js +517 -0
- package/dist/security/taint.js.map +1 -0
- package/dist/tools/browser.d.ts +63 -0
- package/dist/tools/browser.d.ts.map +1 -1
- package/dist/tools/browser.js +1736 -72
- package/dist/tools/browser.js.map +1 -1
- package/dist/tools/db.d.ts +106 -0
- package/dist/tools/db.d.ts.map +1 -0
- package/dist/tools/db.js +1526 -0
- package/dist/tools/db.js.map +1 -0
- package/dist/tools/fs.d.ts.map +1 -1
- package/dist/tools/fs.js +48 -14
- package/dist/tools/fs.js.map +1 -1
- package/dist/tools/http.d.ts.map +1 -1
- package/dist/tools/http.js +7 -2
- package/dist/tools/http.js.map +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +24 -2
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/risk.d.ts +9 -0
- package/dist/tools/risk.d.ts.map +1 -0
- package/dist/tools/risk.js +156 -0
- package/dist/tools/risk.js.map +1 -0
- package/dist/tools/search.d.ts.map +1 -1
- package/dist/tools/search.js +31 -7
- package/dist/tools/search.js.map +1 -1
- package/dist/tools/security.d.ts.map +1 -1
- package/dist/tools/security.js +26 -8
- package/dist/tools/security.js.map +1 -1
- package/dist/tools/shell.d.ts +8 -1
- package/dist/tools/shell.d.ts.map +1 -1
- package/dist/tools/shell.js +169 -30
- package/dist/tools/shell.js.map +1 -1
- package/dist/tools/types.d.ts +12 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/ui/banner.d.ts +22 -0
- package/dist/ui/banner.d.ts.map +1 -1
- package/dist/ui/banner.js +391 -11
- package/dist/ui/banner.js.map +1 -1
- package/dist/ui/diff.d.ts +13 -1
- package/dist/ui/diff.d.ts.map +1 -1
- package/dist/ui/diff.js +39 -10
- package/dist/ui/diff.js.map +1 -1
- package/dist/ui/spinner.d.ts +2 -0
- package/dist/ui/spinner.d.ts.map +1 -1
- package/dist/ui/spinner.js +22 -9
- package/dist/ui/spinner.js.map +1 -1
- package/package.json +12 -1
- package/.github/workflows/ci.yml +0 -29
- package/assets/logo.png +0 -0
- package/assets/wide-logo.png +0 -0
- package/src/agent/context.ts +0 -44
- package/src/agent/index.ts +0 -166
- package/src/config/index.ts +0 -88
- package/src/config/workspace.ts +0 -39
- package/src/decision/index.ts +0 -528
- package/src/index.ts +0 -180
- package/src/providers/claude.ts +0 -163
- package/src/providers/gemini.ts +0 -147
- package/src/providers/index.ts +0 -52
- package/src/providers/ollama.ts +0 -108
- package/src/providers/openai.ts +0 -159
- package/src/providers/types.ts +0 -29
- package/src/repl/index.ts +0 -100
- package/src/security/fuzzer.ts +0 -140
- package/src/security/loader.ts +0 -129
- package/src/security/payloads.ts +0 -95
- package/src/security/rules.ts +0 -300
- package/src/security/scanner.ts +0 -180
- package/src/tools/browser.ts +0 -528
- package/src/tools/fs.ts +0 -143
- package/src/tools/git.ts +0 -51
- package/src/tools/http.ts +0 -77
- package/src/tools/index.ts +0 -64
- package/src/tools/search.ts +0 -109
- package/src/tools/security.ts +0 -140
- package/src/tools/shell.ts +0 -56
- package/src/tools/types.ts +0 -33
- package/src/ui/banner.ts +0 -17
- package/src/ui/colors.ts +0 -19
- package/src/ui/diff.ts +0 -24
- package/src/ui/markdown.ts +0 -34
- package/src/ui/spinner.ts +0 -53
- package/tests/browser.test.mjs +0 -71
- package/tests/decision.test.mjs +0 -68
- package/tests/loader.test.mjs +0 -24
- package/tests/suite.test.mjs +0 -61
- package/tsconfig.json +0 -20
package/src/providers/claude.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import type { LLMProvider, ChatMessage, ProviderResponse, ToolCall } from "./types.js";
|
|
2
|
-
import type { ToolDefinition } from "../tools/types.js";
|
|
3
|
-
|
|
4
|
-
export class ClaudeProvider implements LLMProvider {
|
|
5
|
-
public name = "claude";
|
|
6
|
-
private apiKey: string;
|
|
7
|
-
private model: string;
|
|
8
|
-
|
|
9
|
-
constructor(apiKey: string, model = "claude-3-7-sonnet-20250219") {
|
|
10
|
-
this.apiKey = apiKey;
|
|
11
|
-
this.model = model;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async chatStream(
|
|
15
|
-
messages: ChatMessage[],
|
|
16
|
-
tools: ToolDefinition[],
|
|
17
|
-
onChunk: (chunk: string) => void
|
|
18
|
-
): Promise<ProviderResponse> {
|
|
19
|
-
if (!this.apiKey) {
|
|
20
|
-
throw new Error("Anthropic API key is not configured. Run 'wide config set-key claude <YOUR_KEY>' or set ANTHROPIC_API_KEY.");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const systemMsg = messages.find(m => m.role === "system");
|
|
24
|
-
const chatMsgs = messages.filter(m => m.role !== "system");
|
|
25
|
-
|
|
26
|
-
const anthropicMessages = chatMsgs.map(m => {
|
|
27
|
-
if (m.role === "tool") {
|
|
28
|
-
return {
|
|
29
|
-
role: "user",
|
|
30
|
-
content: [
|
|
31
|
-
{
|
|
32
|
-
type: "tool_result",
|
|
33
|
-
tool_use_id: m.toolCallId || "tool_call",
|
|
34
|
-
content: m.content
|
|
35
|
-
}
|
|
36
|
-
]
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
if (m.role === "assistant" && m.toolCalls && m.toolCalls.length > 0) {
|
|
40
|
-
const contentBlocks: any[] = [];
|
|
41
|
-
if (m.content) {
|
|
42
|
-
contentBlocks.push({ type: "text", text: m.content });
|
|
43
|
-
}
|
|
44
|
-
for (const tc of m.toolCalls) {
|
|
45
|
-
contentBlocks.push({
|
|
46
|
-
type: "tool_use",
|
|
47
|
-
id: tc.id,
|
|
48
|
-
name: tc.name,
|
|
49
|
-
input: tc.arguments
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
return { role: "assistant", content: contentBlocks };
|
|
53
|
-
}
|
|
54
|
-
return { role: m.role, content: m.content };
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
const anthropicTools = tools.map(t => ({
|
|
58
|
-
name: t.name,
|
|
59
|
-
description: t.description,
|
|
60
|
-
input_schema: {
|
|
61
|
-
type: "object",
|
|
62
|
-
properties: t.parameters.properties,
|
|
63
|
-
required: t.parameters.required
|
|
64
|
-
}
|
|
65
|
-
}));
|
|
66
|
-
|
|
67
|
-
const body: any = {
|
|
68
|
-
model: this.model,
|
|
69
|
-
max_tokens: 4096,
|
|
70
|
-
messages: anthropicMessages,
|
|
71
|
-
stream: true
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
if (systemMsg) {
|
|
75
|
-
body.system = systemMsg.content;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (anthropicTools.length > 0) {
|
|
79
|
-
body.tools = anthropicTools;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const res = await fetch("https://api.anthropic.com/v1/messages", {
|
|
83
|
-
method: "POST",
|
|
84
|
-
headers: {
|
|
85
|
-
"Content-Type": "application/json",
|
|
86
|
-
"x-api-key": this.apiKey,
|
|
87
|
-
"anthropic-version": "2023-06-01"
|
|
88
|
-
},
|
|
89
|
-
body: JSON.stringify(body)
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
if (!res.ok) {
|
|
93
|
-
const errText = await res.text();
|
|
94
|
-
throw new Error(`Claude API error (${res.status}): ${errText}`);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const reader = res.body?.getReader();
|
|
98
|
-
if (!reader) throw new Error("Could not acquire stream reader from Claude response.");
|
|
99
|
-
|
|
100
|
-
const decoder = new TextDecoder();
|
|
101
|
-
let fullText = "";
|
|
102
|
-
const toolCallMap = new Map<number, { id: string; name: string; inputStr: string }>();
|
|
103
|
-
let buffer = "";
|
|
104
|
-
|
|
105
|
-
while (true) {
|
|
106
|
-
const { done, value } = await reader.read();
|
|
107
|
-
if (done) break;
|
|
108
|
-
|
|
109
|
-
buffer += decoder.decode(value, { stream: true });
|
|
110
|
-
const lines = buffer.split("\n");
|
|
111
|
-
buffer = lines.pop() || "";
|
|
112
|
-
|
|
113
|
-
for (const line of lines) {
|
|
114
|
-
if (!line.startsWith("data: ")) continue;
|
|
115
|
-
const jsonStr = line.slice(6).trim();
|
|
116
|
-
if (!jsonStr || jsonStr === "[DONE]") continue;
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
const evt = JSON.parse(jsonStr);
|
|
120
|
-
if (evt.type === "content_block_start") {
|
|
121
|
-
if (evt.content_block?.type === "tool_use") {
|
|
122
|
-
toolCallMap.set(evt.index, {
|
|
123
|
-
id: evt.content_block.id,
|
|
124
|
-
name: evt.content_block.name,
|
|
125
|
-
inputStr: ""
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
} else if (evt.type === "content_block_delta") {
|
|
129
|
-
if (evt.delta?.type === "text_delta") {
|
|
130
|
-
fullText += evt.delta.text;
|
|
131
|
-
onChunk(evt.delta.text);
|
|
132
|
-
} else if (evt.delta?.type === "input_json_delta") {
|
|
133
|
-
const current = toolCallMap.get(evt.index);
|
|
134
|
-
if (current) {
|
|
135
|
-
current.inputStr += evt.delta.partial_json;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
} catch {
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const collectedToolCalls: ToolCall[] = [];
|
|
145
|
-
toolCallMap.forEach(v => {
|
|
146
|
-
let args = {};
|
|
147
|
-
try {
|
|
148
|
-
args = JSON.parse(v.inputStr || "{}");
|
|
149
|
-
} catch {
|
|
150
|
-
}
|
|
151
|
-
collectedToolCalls.push({
|
|
152
|
-
id: v.id,
|
|
153
|
-
name: v.name,
|
|
154
|
-
arguments: args
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
return {
|
|
159
|
-
content: fullText,
|
|
160
|
-
toolCalls: collectedToolCalls.length > 0 ? collectedToolCalls : undefined
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
}
|
package/src/providers/gemini.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import type { LLMProvider, ChatMessage, ProviderResponse, ToolCall } from "./types.js";
|
|
2
|
-
import type { ToolDefinition } from "../tools/types.js";
|
|
3
|
-
|
|
4
|
-
export class GeminiProvider implements LLMProvider {
|
|
5
|
-
public name = "gemini";
|
|
6
|
-
private apiKey: string;
|
|
7
|
-
private model: string;
|
|
8
|
-
|
|
9
|
-
constructor(apiKey: string, model = "gemini-2.5-flash") {
|
|
10
|
-
this.apiKey = apiKey;
|
|
11
|
-
this.model = model;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async chatStream(
|
|
15
|
-
messages: ChatMessage[],
|
|
16
|
-
tools: ToolDefinition[],
|
|
17
|
-
onChunk: (chunk: string) => void
|
|
18
|
-
): Promise<ProviderResponse> {
|
|
19
|
-
if (!this.apiKey) {
|
|
20
|
-
throw new Error("Gemini API key is not configured. Run 'wide config set-key gemini <YOUR_KEY>' or set GEMINI_API_KEY.");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const systemMsg = messages.find(m => m.role === "system");
|
|
24
|
-
const chatMsgs = messages.filter(m => m.role !== "system");
|
|
25
|
-
|
|
26
|
-
const contents = chatMsgs.map(m => {
|
|
27
|
-
if (m.role === "user") {
|
|
28
|
-
return { role: "user", parts: [{ text: m.content }] };
|
|
29
|
-
}
|
|
30
|
-
if (m.role === "assistant") {
|
|
31
|
-
const parts: any[] = [];
|
|
32
|
-
if (m.content) parts.push({ text: m.content });
|
|
33
|
-
if (m.toolCalls && m.toolCalls.length > 0) {
|
|
34
|
-
for (const tc of m.toolCalls) {
|
|
35
|
-
parts.push({
|
|
36
|
-
functionCall: {
|
|
37
|
-
name: tc.name,
|
|
38
|
-
args: tc.arguments
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return { role: "model", parts };
|
|
44
|
-
}
|
|
45
|
-
if (m.role === "tool") {
|
|
46
|
-
return {
|
|
47
|
-
role: "user",
|
|
48
|
-
parts: [{
|
|
49
|
-
functionResponse: {
|
|
50
|
-
name: m.name || "tool_result",
|
|
51
|
-
response: { output: m.content }
|
|
52
|
-
}
|
|
53
|
-
}]
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
return { role: "user", parts: [{ text: m.content }] };
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
const functionDeclarations = tools.map(t => ({
|
|
60
|
-
name: t.name,
|
|
61
|
-
description: t.description,
|
|
62
|
-
parameters: {
|
|
63
|
-
type: "OBJECT",
|
|
64
|
-
properties: t.parameters.properties,
|
|
65
|
-
required: t.parameters.required
|
|
66
|
-
}
|
|
67
|
-
}));
|
|
68
|
-
|
|
69
|
-
const body: any = {
|
|
70
|
-
contents,
|
|
71
|
-
generationConfig: {
|
|
72
|
-
temperature: 0.2,
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
if (systemMsg) {
|
|
77
|
-
body.systemInstruction = {
|
|
78
|
-
parts: [{ text: systemMsg.content }]
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (functionDeclarations.length > 0) {
|
|
83
|
-
body.tools = [{ functionDeclarations }];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const url = `https://generativelanguage.googleapis.com/v1beta/models/${this.model}:streamGenerateContent?alt=sse&key=${this.apiKey}`;
|
|
87
|
-
const response = await fetch(url, {
|
|
88
|
-
method: "POST",
|
|
89
|
-
headers: { "Content-Type": "application/json" },
|
|
90
|
-
body: JSON.stringify(body)
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
if (!response.ok) {
|
|
94
|
-
const errText = await response.text();
|
|
95
|
-
throw new Error(`Gemini API error (${response.status}): ${errText}`);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const reader = response.body?.getReader();
|
|
99
|
-
if (!reader) throw new Error("Could not acquire stream reader from Gemini response.");
|
|
100
|
-
|
|
101
|
-
const decoder = new TextDecoder();
|
|
102
|
-
let fullText = "";
|
|
103
|
-
const collectedToolCalls: ToolCall[] = [];
|
|
104
|
-
let buffer = "";
|
|
105
|
-
|
|
106
|
-
while (true) {
|
|
107
|
-
const { done, value } = await reader.read();
|
|
108
|
-
if (done) break;
|
|
109
|
-
|
|
110
|
-
buffer += decoder.decode(value, { stream: true });
|
|
111
|
-
const lines = buffer.split("\n");
|
|
112
|
-
buffer = lines.pop() || "";
|
|
113
|
-
|
|
114
|
-
for (const line of lines) {
|
|
115
|
-
if (!line.startsWith("data: ")) continue;
|
|
116
|
-
const jsonStr = line.slice(6).trim();
|
|
117
|
-
if (!jsonStr) continue;
|
|
118
|
-
|
|
119
|
-
try {
|
|
120
|
-
const parsed = JSON.parse(jsonStr);
|
|
121
|
-
const candidate = parsed.candidates?.[0];
|
|
122
|
-
const parts = candidate?.content?.parts || [];
|
|
123
|
-
|
|
124
|
-
for (const part of parts) {
|
|
125
|
-
if (part.text) {
|
|
126
|
-
fullText += part.text;
|
|
127
|
-
onChunk(part.text);
|
|
128
|
-
}
|
|
129
|
-
if (part.functionCall) {
|
|
130
|
-
collectedToolCalls.push({
|
|
131
|
-
id: `call_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
|
|
132
|
-
name: part.functionCall.name,
|
|
133
|
-
arguments: part.functionCall.args || {}
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
} catch {
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
content: fullText,
|
|
144
|
-
toolCalls: collectedToolCalls.length > 0 ? collectedToolCalls : undefined
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
}
|
package/src/providers/index.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { configManager } from "../config/index.js";
|
|
2
|
-
import { GeminiProvider } from "./gemini.js";
|
|
3
|
-
import { OllamaProvider } from "./ollama.js";
|
|
4
|
-
import { ClaudeProvider } from "./claude.js";
|
|
5
|
-
import { OpenAICompatibleProvider } from "./openai.js";
|
|
6
|
-
import type { LLMProvider } from "./types.js";
|
|
7
|
-
|
|
8
|
-
export function createProvider(overrideProvider?: string, overrideModel?: string): LLMProvider {
|
|
9
|
-
const cfg = configManager.get();
|
|
10
|
-
const providerType = overrideProvider || cfg.provider;
|
|
11
|
-
const modelName = overrideModel || cfg.model;
|
|
12
|
-
|
|
13
|
-
if (providerType === "gemini") {
|
|
14
|
-
const key = configManager.getApiKey("gemini");
|
|
15
|
-
if (!key) {
|
|
16
|
-
throw new Error("Gemini API key is required. Run 'wide config set-key gemini <YOUR_KEY>' or set GEMINI_API_KEY.");
|
|
17
|
-
}
|
|
18
|
-
return new GeminiProvider(key, modelName || "gemini-2.5-flash");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (providerType === "ollama") {
|
|
22
|
-
return new OllamaProvider(cfg.ollamaBaseUrl, modelName || "deepseek-r1:7b");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (providerType === "claude") {
|
|
26
|
-
const key = configManager.getApiKey("claude");
|
|
27
|
-
if (!key) {
|
|
28
|
-
throw new Error("Claude API key is required. Run 'wide config set-key claude <YOUR_KEY>' or set ANTHROPIC_API_KEY.");
|
|
29
|
-
}
|
|
30
|
-
return new ClaudeProvider(key, modelName || "claude-3-7-sonnet-20250219");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (providerType === "deepseek") {
|
|
34
|
-
const key = configManager.getApiKey("deepseek");
|
|
35
|
-
if (!key) {
|
|
36
|
-
throw new Error("DeepSeek API key is required. Run 'wide config set-key deepseek <YOUR_KEY>' or set DEEPSEEK_API_KEY.");
|
|
37
|
-
}
|
|
38
|
-
return new OpenAICompatibleProvider("deepseek", key, modelName || "deepseek-chat", "https://api.deepseek.com/v1");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (providerType === "openai") {
|
|
42
|
-
const key = configManager.getApiKey("openai");
|
|
43
|
-
if (!key) {
|
|
44
|
-
throw new Error("OpenAI API key is required. Run 'wide config set-key openai <YOUR_KEY>' or set OPENAI_API_KEY.");
|
|
45
|
-
}
|
|
46
|
-
return new OpenAICompatibleProvider("openai", key, modelName || "gpt-4o", "https://api.openai.com/v1");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
throw new Error(`Unsupported provider: '${providerType}'. Supported: gemini, ollama, claude, deepseek, openai.`);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export * from "./types.js";
|
package/src/providers/ollama.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import type { LLMProvider, ChatMessage, ProviderResponse, ToolCall } from "./types.js";
|
|
2
|
-
import type { ToolDefinition } from "../tools/types.js";
|
|
3
|
-
|
|
4
|
-
export class OllamaProvider implements LLMProvider {
|
|
5
|
-
public name = "ollama";
|
|
6
|
-
private baseUrl: string;
|
|
7
|
-
private model: string;
|
|
8
|
-
|
|
9
|
-
constructor(baseUrl = "http://localhost:11434", model = "deepseek-r1:7b") {
|
|
10
|
-
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
11
|
-
this.model = model;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async chatStream(
|
|
15
|
-
messages: ChatMessage[],
|
|
16
|
-
tools: ToolDefinition[],
|
|
17
|
-
onChunk: (chunk: string) => void
|
|
18
|
-
): Promise<ProviderResponse> {
|
|
19
|
-
const formattedMessages = messages.map(m => {
|
|
20
|
-
if (m.role === "tool") {
|
|
21
|
-
return { role: "tool", content: m.content, name: m.name };
|
|
22
|
-
}
|
|
23
|
-
return { role: m.role, content: m.content };
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const formattedTools = tools.map(t => ({
|
|
27
|
-
type: "function",
|
|
28
|
-
function: {
|
|
29
|
-
name: t.name,
|
|
30
|
-
description: t.description,
|
|
31
|
-
parameters: t.parameters
|
|
32
|
-
}
|
|
33
|
-
}));
|
|
34
|
-
|
|
35
|
-
const body: any = {
|
|
36
|
-
model: this.model,
|
|
37
|
-
messages: formattedMessages,
|
|
38
|
-
stream: true,
|
|
39
|
-
options: {
|
|
40
|
-
temperature: 0.2
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
if (formattedTools.length > 0) {
|
|
45
|
-
body.tools = formattedTools;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
let response: Response;
|
|
49
|
-
try {
|
|
50
|
-
response = await fetch(`${this.baseUrl}/api/chat`, {
|
|
51
|
-
method: "POST",
|
|
52
|
-
headers: { "Content-Type": "application/json" },
|
|
53
|
-
body: JSON.stringify(body)
|
|
54
|
-
});
|
|
55
|
-
} catch (err: any) {
|
|
56
|
-
throw new Error(`Failed to connect to Ollama at ${this.baseUrl}: ${err.message}. Is Ollama running?`);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!response.ok) {
|
|
60
|
-
const errText = await response.text();
|
|
61
|
-
throw new Error(`Ollama error (${response.status}): ${errText}`);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const reader = response.body?.getReader();
|
|
65
|
-
if (!reader) throw new Error("Could not acquire stream reader from Ollama.");
|
|
66
|
-
|
|
67
|
-
const decoder = new TextDecoder();
|
|
68
|
-
let fullText = "";
|
|
69
|
-
const collectedToolCalls: ToolCall[] = [];
|
|
70
|
-
let buffer = "";
|
|
71
|
-
|
|
72
|
-
while (true) {
|
|
73
|
-
const { done, value } = await reader.read();
|
|
74
|
-
if (done) break;
|
|
75
|
-
|
|
76
|
-
buffer += decoder.decode(value, { stream: true });
|
|
77
|
-
const lines = buffer.split("\n");
|
|
78
|
-
buffer = lines.pop() || "";
|
|
79
|
-
|
|
80
|
-
for (const line of lines) {
|
|
81
|
-
if (!line.trim()) continue;
|
|
82
|
-
try {
|
|
83
|
-
const parsed = JSON.parse(line);
|
|
84
|
-
const msg = parsed.message;
|
|
85
|
-
if (msg?.content) {
|
|
86
|
-
fullText += msg.content;
|
|
87
|
-
onChunk(msg.content);
|
|
88
|
-
}
|
|
89
|
-
if (msg?.tool_calls) {
|
|
90
|
-
for (const tc of msg.tool_calls) {
|
|
91
|
-
collectedToolCalls.push({
|
|
92
|
-
id: `call_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
|
|
93
|
-
name: tc.function.name,
|
|
94
|
-
arguments: tc.function.arguments || {}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} catch {
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
content: fullText,
|
|
105
|
-
toolCalls: collectedToolCalls.length > 0 ? collectedToolCalls : undefined
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
}
|
package/src/providers/openai.ts
DELETED
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import type { LLMProvider, ChatMessage, ProviderResponse, ToolCall } from "./types.js";
|
|
2
|
-
import type { ToolDefinition } from "../tools/types.js";
|
|
3
|
-
|
|
4
|
-
export class OpenAICompatibleProvider implements LLMProvider {
|
|
5
|
-
public name: string;
|
|
6
|
-
private apiKey: string;
|
|
7
|
-
private model: string;
|
|
8
|
-
private baseUrl: string;
|
|
9
|
-
|
|
10
|
-
constructor(
|
|
11
|
-
name: string,
|
|
12
|
-
apiKey: string,
|
|
13
|
-
model: string,
|
|
14
|
-
baseUrl = "https://api.openai.com/v1"
|
|
15
|
-
) {
|
|
16
|
-
this.name = name;
|
|
17
|
-
this.apiKey = apiKey;
|
|
18
|
-
this.model = model;
|
|
19
|
-
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async chatStream(
|
|
23
|
-
messages: ChatMessage[],
|
|
24
|
-
tools: ToolDefinition[],
|
|
25
|
-
onChunk: (chunk: string) => void
|
|
26
|
-
): Promise<ProviderResponse> {
|
|
27
|
-
if (!this.apiKey) {
|
|
28
|
-
throw new Error(`${this.name.toUpperCase()} API key is not configured. Run 'wide config set-key ${this.name} <YOUR_KEY>'.`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const openAiMessages = messages.map(m => {
|
|
32
|
-
if (m.role === "tool") {
|
|
33
|
-
return {
|
|
34
|
-
role: "tool",
|
|
35
|
-
tool_call_id: m.toolCallId || "tool_call",
|
|
36
|
-
content: m.content
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
if (m.role === "assistant" && m.toolCalls && m.toolCalls.length > 0) {
|
|
40
|
-
return {
|
|
41
|
-
role: "assistant",
|
|
42
|
-
content: m.content || null,
|
|
43
|
-
tool_calls: m.toolCalls.map(tc => ({
|
|
44
|
-
id: tc.id,
|
|
45
|
-
type: "function",
|
|
46
|
-
function: {
|
|
47
|
-
name: tc.name,
|
|
48
|
-
arguments: JSON.stringify(tc.arguments)
|
|
49
|
-
}
|
|
50
|
-
}))
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
return { role: m.role, content: m.content };
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const openAiTools = tools.map(t => ({
|
|
57
|
-
type: "function",
|
|
58
|
-
function: {
|
|
59
|
-
name: t.name,
|
|
60
|
-
description: t.description,
|
|
61
|
-
parameters: t.parameters
|
|
62
|
-
}
|
|
63
|
-
}));
|
|
64
|
-
|
|
65
|
-
const body: any = {
|
|
66
|
-
model: this.model,
|
|
67
|
-
messages: openAiMessages,
|
|
68
|
-
stream: true,
|
|
69
|
-
temperature: 0.2
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
if (openAiTools.length > 0) {
|
|
73
|
-
body.tools = openAiTools;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
77
|
-
method: "POST",
|
|
78
|
-
headers: {
|
|
79
|
-
"Content-Type": "application/json",
|
|
80
|
-
"Authorization": `Bearer ${this.apiKey}`
|
|
81
|
-
},
|
|
82
|
-
body: JSON.stringify(body)
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
if (!res.ok) {
|
|
86
|
-
const errText = await res.text();
|
|
87
|
-
throw new Error(`${this.name} API error (${res.status}): ${errText}`);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const reader = res.body?.getReader();
|
|
91
|
-
if (!reader) throw new Error(`Could not acquire stream reader from ${this.name}.`);
|
|
92
|
-
|
|
93
|
-
const decoder = new TextDecoder();
|
|
94
|
-
let fullText = "";
|
|
95
|
-
const toolCallAccumulators = new Map<number, { id: string; name: string; argsStr: string }>();
|
|
96
|
-
let buffer = "";
|
|
97
|
-
|
|
98
|
-
while (true) {
|
|
99
|
-
const { done, value } = await reader.read();
|
|
100
|
-
if (done) break;
|
|
101
|
-
|
|
102
|
-
buffer += decoder.decode(value, { stream: true });
|
|
103
|
-
const lines = buffer.split("\n");
|
|
104
|
-
buffer = lines.pop() || "";
|
|
105
|
-
|
|
106
|
-
for (const line of lines) {
|
|
107
|
-
if (!line.startsWith("data: ")) continue;
|
|
108
|
-
const jsonStr = line.slice(6).trim();
|
|
109
|
-
if (!jsonStr || jsonStr === "[DONE]") continue;
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
const parsed = JSON.parse(jsonStr);
|
|
113
|
-
const delta = parsed.choices?.[0]?.delta;
|
|
114
|
-
if (delta?.content) {
|
|
115
|
-
fullText += delta.content;
|
|
116
|
-
onChunk(delta.content);
|
|
117
|
-
}
|
|
118
|
-
if (delta?.tool_calls) {
|
|
119
|
-
for (const tc of delta.tool_calls) {
|
|
120
|
-
const idx = tc.index ?? 0;
|
|
121
|
-
if (!toolCallAccumulators.has(idx)) {
|
|
122
|
-
toolCallAccumulators.set(idx, {
|
|
123
|
-
id: tc.id || `call_${Date.now()}_${idx}`,
|
|
124
|
-
name: tc.function?.name || "",
|
|
125
|
-
argsStr: tc.function?.arguments || ""
|
|
126
|
-
});
|
|
127
|
-
} else {
|
|
128
|
-
const acc = toolCallAccumulators.get(idx)!;
|
|
129
|
-
if (tc.id) acc.id = tc.id;
|
|
130
|
-
if (tc.function?.name) acc.name += tc.function.name;
|
|
131
|
-
if (tc.function?.arguments) acc.argsStr += tc.function.arguments;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
} catch {
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const collectedToolCalls: ToolCall[] = [];
|
|
141
|
-
toolCallAccumulators.forEach(v => {
|
|
142
|
-
let args = {};
|
|
143
|
-
try {
|
|
144
|
-
args = JSON.parse(v.argsStr || "{}");
|
|
145
|
-
} catch {
|
|
146
|
-
}
|
|
147
|
-
collectedToolCalls.push({
|
|
148
|
-
id: v.id,
|
|
149
|
-
name: v.name,
|
|
150
|
-
arguments: args
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
content: fullText,
|
|
156
|
-
toolCalls: collectedToolCalls.length > 0 ? collectedToolCalls : undefined
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
}
|
package/src/providers/types.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { ToolDefinition } from "../tools/types.js";
|
|
2
|
-
|
|
3
|
-
export interface ToolCall {
|
|
4
|
-
id: string;
|
|
5
|
-
name: string;
|
|
6
|
-
arguments: Record<string, any>;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface ChatMessage {
|
|
10
|
-
role: "system" | "user" | "assistant" | "tool";
|
|
11
|
-
content: string;
|
|
12
|
-
toolCalls?: ToolCall[];
|
|
13
|
-
toolCallId?: string;
|
|
14
|
-
name?: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface ProviderResponse {
|
|
18
|
-
content: string;
|
|
19
|
-
toolCalls?: ToolCall[];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface LLMProvider {
|
|
23
|
-
name: string;
|
|
24
|
-
chatStream(
|
|
25
|
-
messages: ChatMessage[],
|
|
26
|
-
tools: ToolDefinition[],
|
|
27
|
-
onChunk: (chunk: string) => void
|
|
28
|
-
): Promise<ProviderResponse>;
|
|
29
|
-
}
|