@jaibber/sdk 0.1.0 → 0.1.2
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 +70 -0
- package/dist/cli.js +340 -12
- package/dist/index.cjs +263 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +65 -1
- package/dist/index.d.ts +65 -1
- package/dist/index.js +260 -3
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -31,8 +31,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
AnthropicProvider: () => AnthropicProvider,
|
|
34
|
+
ClaudeCLIProvider: () => ClaudeCLIProvider,
|
|
35
|
+
GoogleProvider: () => GoogleProvider,
|
|
34
36
|
JaibberAgent: () => JaibberAgent,
|
|
35
37
|
MessageContext: () => MessageContext,
|
|
38
|
+
OpenAIProvider: () => OpenAIProvider,
|
|
36
39
|
TaskContext: () => TaskContext
|
|
37
40
|
});
|
|
38
41
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -51,6 +54,25 @@ var JaibberClient = class {
|
|
|
51
54
|
this.serverUrl = serverUrl.replace(/\/+$/, "");
|
|
52
55
|
}
|
|
53
56
|
// ── Auth ────────────────────────────────────────────────────────────
|
|
57
|
+
/** POST /api/auth/register — create a new account, stores JWT. */
|
|
58
|
+
async register(username, password) {
|
|
59
|
+
const res = await fetch(`${this.serverUrl}/api/auth/register`, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: { "Content-Type": "application/json" },
|
|
62
|
+
body: JSON.stringify({ username, password })
|
|
63
|
+
});
|
|
64
|
+
if (!res.ok) {
|
|
65
|
+
const text = await res.text();
|
|
66
|
+
if (res.status === 409) {
|
|
67
|
+
throw new Error(`Username "${username}" is already taken.`);
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`Registration failed (${res.status}): ${text}`);
|
|
70
|
+
}
|
|
71
|
+
const data = await res.json();
|
|
72
|
+
this.jwt = data.token;
|
|
73
|
+
this.userId = data.userId;
|
|
74
|
+
this.username = data.username;
|
|
75
|
+
}
|
|
54
76
|
/** POST /api/auth/token — login with credentials, stores JWT. */
|
|
55
77
|
async login(username, password) {
|
|
56
78
|
const res = await fetch(`${this.serverUrl}/api/auth/token`, {
|
|
@@ -99,6 +121,26 @@ var JaibberClient = class {
|
|
|
99
121
|
role: p.role
|
|
100
122
|
}));
|
|
101
123
|
}
|
|
124
|
+
/** POST /api/projects — create a new project, returns it. */
|
|
125
|
+
async createProject(name, description) {
|
|
126
|
+
const res = await fetch(`${this.serverUrl}/api/projects`, {
|
|
127
|
+
method: "POST",
|
|
128
|
+
headers: this.authHeaders(),
|
|
129
|
+
body: JSON.stringify({ name, description })
|
|
130
|
+
});
|
|
131
|
+
if (!res.ok) {
|
|
132
|
+
const text = await res.text();
|
|
133
|
+
throw new Error(`Failed to create project (${res.status}): ${text}`);
|
|
134
|
+
}
|
|
135
|
+
const data = await res.json();
|
|
136
|
+
return {
|
|
137
|
+
id: data.id,
|
|
138
|
+
name: data.name,
|
|
139
|
+
description: data.description,
|
|
140
|
+
ablyChannelName: data.ablyChannelName,
|
|
141
|
+
role: data.role
|
|
142
|
+
};
|
|
143
|
+
}
|
|
102
144
|
// ── Ably token ──────────────────────────────────────────────────────
|
|
103
145
|
/** POST /api/ably/token — get scoped Ably TokenRequest. */
|
|
104
146
|
async getAblyToken() {
|
|
@@ -605,6 +647,211 @@ var AnthropicProvider = class {
|
|
|
605
647
|
}
|
|
606
648
|
};
|
|
607
649
|
|
|
650
|
+
// src/providers/openai.ts
|
|
651
|
+
var OPENAI_API_URL = "https://api.openai.com/v1/chat/completions";
|
|
652
|
+
var DEFAULT_MODEL2 = "gpt-4o";
|
|
653
|
+
var DEFAULT_MAX_TOKENS2 = 8192;
|
|
654
|
+
var OpenAIProvider = class {
|
|
655
|
+
apiKey;
|
|
656
|
+
model;
|
|
657
|
+
maxTokens;
|
|
658
|
+
constructor(options) {
|
|
659
|
+
this.apiKey = options.apiKey;
|
|
660
|
+
this.model = options.model ?? DEFAULT_MODEL2;
|
|
661
|
+
this.maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS2;
|
|
662
|
+
}
|
|
663
|
+
async *generate(prompt, systemPrompt, conversationContext) {
|
|
664
|
+
let fullText = "";
|
|
665
|
+
if (conversationContext) {
|
|
666
|
+
fullText += 'Below is the recent conversation history for context. Respond ONLY to the final user message. Be conversational and concise \u2014 reply directly to the user as a chat participant. Do NOT narrate your thought process, planning steps, or internal reasoning. Do NOT describe actions you would take (e.g. "I should...", "Let me...", "I will..."). Just answer.\n\n';
|
|
667
|
+
fullText += conversationContext;
|
|
668
|
+
fullText += "\n\n---\n\n";
|
|
669
|
+
}
|
|
670
|
+
fullText += prompt;
|
|
671
|
+
const messages = [];
|
|
672
|
+
if (systemPrompt) {
|
|
673
|
+
messages.push({ role: "system", content: systemPrompt });
|
|
674
|
+
}
|
|
675
|
+
messages.push({ role: "user", content: fullText });
|
|
676
|
+
const response = await fetch(OPENAI_API_URL, {
|
|
677
|
+
method: "POST",
|
|
678
|
+
headers: {
|
|
679
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
680
|
+
"Content-Type": "application/json"
|
|
681
|
+
},
|
|
682
|
+
body: JSON.stringify({
|
|
683
|
+
model: this.model,
|
|
684
|
+
max_tokens: this.maxTokens,
|
|
685
|
+
stream: true,
|
|
686
|
+
messages
|
|
687
|
+
})
|
|
688
|
+
});
|
|
689
|
+
if (!response.ok) {
|
|
690
|
+
const text = await response.text();
|
|
691
|
+
const status = response.status;
|
|
692
|
+
if (status === 401) {
|
|
693
|
+
throw new Error("Invalid OpenAI API key. Check your API key.");
|
|
694
|
+
} else if (status === 429) {
|
|
695
|
+
throw new Error("OpenAI API rate limit exceeded. Please wait and try again.");
|
|
696
|
+
}
|
|
697
|
+
throw new Error(`OpenAI API returned ${status}: ${text}`);
|
|
698
|
+
}
|
|
699
|
+
if (!response.body) {
|
|
700
|
+
throw new Error("OpenAI API returned no response body");
|
|
701
|
+
}
|
|
702
|
+
const reader = response.body.getReader();
|
|
703
|
+
const decoder = new TextDecoder();
|
|
704
|
+
let buffer = "";
|
|
705
|
+
try {
|
|
706
|
+
while (true) {
|
|
707
|
+
const { done, value } = await reader.read();
|
|
708
|
+
if (done) break;
|
|
709
|
+
buffer += decoder.decode(value, { stream: true });
|
|
710
|
+
while (true) {
|
|
711
|
+
const newlinePos = buffer.indexOf("\n");
|
|
712
|
+
if (newlinePos === -1) break;
|
|
713
|
+
const line = buffer.slice(0, newlinePos).replace(/\r$/, "");
|
|
714
|
+
buffer = buffer.slice(newlinePos + 1);
|
|
715
|
+
if (!line || !line.startsWith("data: ")) continue;
|
|
716
|
+
const data = line.slice(6).trim();
|
|
717
|
+
if (data === "[DONE]") return;
|
|
718
|
+
try {
|
|
719
|
+
const json = JSON.parse(data);
|
|
720
|
+
const content = json?.choices?.[0]?.delta?.content;
|
|
721
|
+
if (content) yield content;
|
|
722
|
+
} catch {
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
} finally {
|
|
727
|
+
reader.releaseLock();
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
// src/providers/google.ts
|
|
733
|
+
var DEFAULT_MODEL3 = "gemini-2.0-flash";
|
|
734
|
+
var DEFAULT_MAX_TOKENS3 = 8192;
|
|
735
|
+
var GoogleProvider = class {
|
|
736
|
+
apiKey;
|
|
737
|
+
model;
|
|
738
|
+
maxTokens;
|
|
739
|
+
constructor(options) {
|
|
740
|
+
this.apiKey = options.apiKey;
|
|
741
|
+
this.model = options.model ?? DEFAULT_MODEL3;
|
|
742
|
+
this.maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS3;
|
|
743
|
+
}
|
|
744
|
+
async *generate(prompt, systemPrompt, conversationContext) {
|
|
745
|
+
let fullText = "";
|
|
746
|
+
if (conversationContext) {
|
|
747
|
+
fullText += 'Below is the recent conversation history for context. Respond ONLY to the final user message. Be conversational and concise \u2014 reply directly to the user as a chat participant. Do NOT narrate your thought process, planning steps, or internal reasoning. Do NOT describe actions you would take (e.g. "I should...", "Let me...", "I will..."). Just answer.\n\n';
|
|
748
|
+
fullText += conversationContext;
|
|
749
|
+
fullText += "\n\n---\n\n";
|
|
750
|
+
}
|
|
751
|
+
fullText += prompt;
|
|
752
|
+
const url = `https://generativelanguage.googleapis.com/v1beta/models/${this.model}:streamGenerateContent?alt=sse&key=${this.apiKey}`;
|
|
753
|
+
const body = {
|
|
754
|
+
contents: [{ parts: [{ text: fullText }] }],
|
|
755
|
+
generationConfig: {
|
|
756
|
+
maxOutputTokens: this.maxTokens
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
if (systemPrompt) {
|
|
760
|
+
body.systemInstruction = { parts: [{ text: systemPrompt }] };
|
|
761
|
+
}
|
|
762
|
+
const response = await fetch(url, {
|
|
763
|
+
method: "POST",
|
|
764
|
+
headers: { "Content-Type": "application/json" },
|
|
765
|
+
body: JSON.stringify(body)
|
|
766
|
+
});
|
|
767
|
+
if (!response.ok) {
|
|
768
|
+
const text = await response.text();
|
|
769
|
+
const status = response.status;
|
|
770
|
+
if (status === 400 || status === 403) {
|
|
771
|
+
throw new Error(`Google AI API error (${status}): ${text}`);
|
|
772
|
+
} else if (status === 429) {
|
|
773
|
+
throw new Error("Google AI API rate limit exceeded. Please wait and try again.");
|
|
774
|
+
}
|
|
775
|
+
throw new Error(`Google AI API returned ${status}: ${text}`);
|
|
776
|
+
}
|
|
777
|
+
if (!response.body) {
|
|
778
|
+
throw new Error("Google AI API returned no response body");
|
|
779
|
+
}
|
|
780
|
+
const reader = response.body.getReader();
|
|
781
|
+
const decoder = new TextDecoder();
|
|
782
|
+
let buffer = "";
|
|
783
|
+
try {
|
|
784
|
+
while (true) {
|
|
785
|
+
const { done, value } = await reader.read();
|
|
786
|
+
if (done) break;
|
|
787
|
+
buffer += decoder.decode(value, { stream: true });
|
|
788
|
+
while (true) {
|
|
789
|
+
const newlinePos = buffer.indexOf("\n");
|
|
790
|
+
if (newlinePos === -1) break;
|
|
791
|
+
const line = buffer.slice(0, newlinePos).replace(/\r$/, "");
|
|
792
|
+
buffer = buffer.slice(newlinePos + 1);
|
|
793
|
+
if (!line || !line.startsWith("data: ")) continue;
|
|
794
|
+
const data = line.slice(6).trim();
|
|
795
|
+
if (data === "[DONE]") return;
|
|
796
|
+
try {
|
|
797
|
+
const json = JSON.parse(data);
|
|
798
|
+
const text = json?.candidates?.[0]?.content?.parts?.[0]?.text;
|
|
799
|
+
if (text) yield text;
|
|
800
|
+
} catch {
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
} finally {
|
|
805
|
+
reader.releaseLock();
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
// src/providers/claude-cli.ts
|
|
811
|
+
var import_child_process = require("child_process");
|
|
812
|
+
var ClaudeCLIProvider = class {
|
|
813
|
+
projectDir;
|
|
814
|
+
constructor(options = {}) {
|
|
815
|
+
this.projectDir = options.projectDir ?? process.cwd();
|
|
816
|
+
}
|
|
817
|
+
async *generate(prompt, systemPrompt, conversationContext) {
|
|
818
|
+
let fullPrompt = "";
|
|
819
|
+
if (systemPrompt) {
|
|
820
|
+
fullPrompt += `[System Instructions]
|
|
821
|
+
${systemPrompt}
|
|
822
|
+
|
|
823
|
+
`;
|
|
824
|
+
}
|
|
825
|
+
if (conversationContext) {
|
|
826
|
+
fullPrompt += 'Below is the recent conversation history for context. Respond ONLY to the final user message. Be conversational and concise \u2014 reply directly to the user as a chat participant. Do NOT narrate your thought process, planning steps, or internal reasoning. Do NOT describe actions you would take (e.g. "I should...", "Let me...", "I will..."). Just answer.\n\n';
|
|
827
|
+
fullPrompt += conversationContext;
|
|
828
|
+
fullPrompt += "\n\n---\n\n";
|
|
829
|
+
}
|
|
830
|
+
fullPrompt += prompt;
|
|
831
|
+
const child = (0, import_child_process.spawn)("claude", ["--print", "--dangerously-skip-permissions", fullPrompt], {
|
|
832
|
+
cwd: this.projectDir,
|
|
833
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
834
|
+
shell: true
|
|
835
|
+
});
|
|
836
|
+
let stderr = "";
|
|
837
|
+
child.stderr?.on("data", (chunk) => {
|
|
838
|
+
stderr += chunk.toString();
|
|
839
|
+
});
|
|
840
|
+
const stdout = child.stdout;
|
|
841
|
+
for await (const chunk of stdout) {
|
|
842
|
+
const text = chunk.toString();
|
|
843
|
+
if (text) yield text;
|
|
844
|
+
}
|
|
845
|
+
const exitCode = await new Promise((resolve) => {
|
|
846
|
+
child.on("close", resolve);
|
|
847
|
+
});
|
|
848
|
+
if (exitCode !== 0 && exitCode !== null) {
|
|
849
|
+
if (!stderr.trim()) return;
|
|
850
|
+
throw new Error(`Claude CLI exited with code ${exitCode}: ${stderr.trim()}`);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
|
|
608
855
|
// src/agent.ts
|
|
609
856
|
var DEFAULT_MAX_DEPTH = 3;
|
|
610
857
|
var DEFAULT_CHUNK_BATCH_MS = 200;
|
|
@@ -703,10 +950,20 @@ var JaibberAgent = class extends import_events.EventEmitter {
|
|
|
703
950
|
onTask(handler) {
|
|
704
951
|
this.taskHandler = handler;
|
|
705
952
|
}
|
|
706
|
-
/** Use a built-in LLM provider. */
|
|
707
953
|
useProvider(name, options) {
|
|
708
|
-
|
|
709
|
-
|
|
954
|
+
switch (name) {
|
|
955
|
+
case "anthropic":
|
|
956
|
+
this.provider = new AnthropicProvider(options);
|
|
957
|
+
break;
|
|
958
|
+
case "openai":
|
|
959
|
+
this.provider = new OpenAIProvider(options);
|
|
960
|
+
break;
|
|
961
|
+
case "google":
|
|
962
|
+
this.provider = new GoogleProvider(options);
|
|
963
|
+
break;
|
|
964
|
+
case "claude-cli":
|
|
965
|
+
this.provider = new ClaudeCLIProvider(options);
|
|
966
|
+
break;
|
|
710
967
|
}
|
|
711
968
|
}
|
|
712
969
|
// ── Message handling ────────────────────────────────────────────────
|
|
@@ -923,8 +1180,11 @@ ${task.description}` : `Task: ${task.title}`;
|
|
|
923
1180
|
// Annotate the CommonJS export names for ESM import in node:
|
|
924
1181
|
0 && (module.exports = {
|
|
925
1182
|
AnthropicProvider,
|
|
1183
|
+
ClaudeCLIProvider,
|
|
1184
|
+
GoogleProvider,
|
|
926
1185
|
JaibberAgent,
|
|
927
1186
|
MessageContext,
|
|
1187
|
+
OpenAIProvider,
|
|
928
1188
|
TaskContext
|
|
929
1189
|
});
|
|
930
1190
|
//# sourceMappingURL=index.cjs.map
|