thebird 1.2.7 → 1.2.8
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/docs/app.js +119 -84
- package/docs/index.html +12 -49
- package/package.json +1 -1
package/docs/app.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createElement, applyDiff } from 'webjsx';
|
|
2
|
+
|
|
3
|
+
const MODELS = [
|
|
4
|
+
'gemini-2.0-flash',
|
|
5
|
+
'gemini-2.0-flash-thinking-exp',
|
|
6
|
+
'gemini-1.5-pro',
|
|
7
|
+
'gemini-1.5-flash',
|
|
8
|
+
];
|
|
2
9
|
|
|
3
10
|
function convertMessages(messages) {
|
|
4
11
|
const contents = [];
|
|
@@ -26,102 +33,130 @@ function convertMessages(messages) {
|
|
|
26
33
|
return contents;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
function buildConfig({ system, temperature
|
|
30
|
-
const config = { maxOutputTokens:
|
|
36
|
+
function buildConfig({ system, temperature } = {}) {
|
|
37
|
+
const config = { maxOutputTokens: 8192, temperature: temperature ?? 0.7 };
|
|
31
38
|
if (system) config.systemInstruction = system;
|
|
32
39
|
return config;
|
|
33
40
|
}
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
class BirdChat extends HTMLElement {
|
|
43
|
+
constructor() {
|
|
44
|
+
super();
|
|
45
|
+
this.state = {
|
|
46
|
+
messages: [],
|
|
47
|
+
streaming: false,
|
|
48
|
+
model: MODELS[0],
|
|
49
|
+
apiKey: localStorage.getItem('gemini_api_key') || '',
|
|
50
|
+
status: '',
|
|
51
|
+
streamingText: '',
|
|
52
|
+
};
|
|
53
|
+
window.__debug = { get state() { return this.state; }.bind(this), get messages() { return this.state.messages; }.bind(this) };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
connectedCallback() { this.render(); }
|
|
36
57
|
|
|
37
|
-
|
|
38
|
-
get state() { return state; },
|
|
39
|
-
get messages() { return state.messages; },
|
|
40
|
-
};
|
|
58
|
+
setState(patch) { Object.assign(this.state, patch); this.render(); }
|
|
41
59
|
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
60
|
+
render() {
|
|
61
|
+
const { messages, streaming, model, apiKey, status, streamingText } = this.state;
|
|
62
|
+
applyDiff(this, (
|
|
63
|
+
<div class="flex flex-col h-full">
|
|
64
|
+
<header class="navbar bg-base-200 border-b border-base-300 gap-2 flex-wrap px-4 py-2">
|
|
65
|
+
<span class="text-primary font-bold text-lg mr-2">🐦 thebird</span>
|
|
66
|
+
<span class="text-base-content/50 text-xs hidden sm:inline">Anthropic SDK format → Gemini API</span>
|
|
67
|
+
<div class="flex gap-2 flex-1 min-w-0 items-center flex-wrap">
|
|
68
|
+
<input
|
|
69
|
+
id="api-key-input"
|
|
70
|
+
type="password"
|
|
71
|
+
class="input input-sm input-bordered flex-1 min-w-[160px]"
|
|
72
|
+
placeholder="GEMINI_API_KEY"
|
|
73
|
+
value={apiKey}
|
|
74
|
+
onchange={e => { const v = e.target.value.trim(); localStorage.setItem('gemini_api_key', v); this.setState({ apiKey: v }); }}
|
|
75
|
+
/>
|
|
76
|
+
<select
|
|
77
|
+
class="select select-sm select-bordered"
|
|
78
|
+
value={model}
|
|
79
|
+
onchange={e => this.setState({ model: e.target.value })}
|
|
80
|
+
>
|
|
81
|
+
{MODELS.map(m => <option value={m} selected={m === model}>{m}</option>)}
|
|
82
|
+
</select>
|
|
83
|
+
<button class="btn btn-sm btn-ghost" onclick={() => this.setState({ messages: [], status: '' })}>Clear</button>
|
|
84
|
+
</div>
|
|
85
|
+
</header>
|
|
50
86
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
$
|
|
54
|
-
$
|
|
87
|
+
<div id="msg-list" class="flex-1 overflow-y-auto flex flex-col gap-3 p-4">
|
|
88
|
+
{messages.map((m, i) => (
|
|
89
|
+
<div key={i} class={`flex ${m.role === 'user' ? 'justify-end' : 'justify-start'}`}>
|
|
90
|
+
<div class={`msg-bubble card px-4 py-3 text-sm leading-relaxed ${m.role === 'user' ? 'bg-primary text-primary-content' : 'bg-base-200 text-base-content'}`}>
|
|
91
|
+
{m.content}
|
|
92
|
+
</div>
|
|
93
|
+
</div>
|
|
94
|
+
))}
|
|
95
|
+
{streamingText && (
|
|
96
|
+
<div class="flex justify-start">
|
|
97
|
+
<div class="msg-bubble card bg-base-200 text-base-content px-4 py-3 text-sm leading-relaxed">{streamingText}<span class="animate-pulse ml-1">▋</span></div>
|
|
98
|
+
</div>
|
|
99
|
+
)}
|
|
100
|
+
{!streamingText && streaming && (
|
|
101
|
+
<div class="flex justify-start">
|
|
102
|
+
<div class="card bg-base-200 px-4 py-3"><span class="loading loading-dots loading-sm"></span></div>
|
|
103
|
+
</div>
|
|
104
|
+
)}
|
|
105
|
+
</div>
|
|
55
106
|
|
|
56
|
-
|
|
57
|
-
const el = document.createElement('div');
|
|
58
|
-
el.className = 'msg ' + role;
|
|
59
|
-
el.textContent = text;
|
|
60
|
-
$messages.appendChild(el);
|
|
61
|
-
$messages.scrollTop = $messages.scrollHeight;
|
|
62
|
-
return el;
|
|
63
|
-
}
|
|
107
|
+
{status && <div class="text-xs text-error px-4 pb-1">{status}</div>}
|
|
64
108
|
|
|
65
|
-
|
|
109
|
+
<form class="flex gap-2 p-3 border-t border-base-300 bg-base-200" onsubmit={e => { e.preventDefault(); this.send(); }}>
|
|
110
|
+
<textarea
|
|
111
|
+
id="chat-input"
|
|
112
|
+
class="textarea textarea-bordered flex-1 resize-none min-h-[42px] max-h-[120px] text-sm"
|
|
113
|
+
placeholder="Message… (Shift+Enter for newline)"
|
|
114
|
+
rows="1"
|
|
115
|
+
disabled={streaming}
|
|
116
|
+
onkeydown={e => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); this.send(); } }}
|
|
117
|
+
oninput={e => { e.target.style.height = 'auto'; e.target.style.height = Math.min(e.target.scrollHeight, 120) + 'px'; }}
|
|
118
|
+
></textarea>
|
|
119
|
+
<button type="submit" class="btn btn-primary self-end" disabled={streaming}>
|
|
120
|
+
{streaming ? <span class="loading loading-spinner loading-sm"></span> : 'Send'}
|
|
121
|
+
</button>
|
|
122
|
+
</form>
|
|
123
|
+
</div>
|
|
124
|
+
));
|
|
125
|
+
}
|
|
66
126
|
|
|
67
|
-
async
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
127
|
+
async send() {
|
|
128
|
+
const input = this.querySelector('#chat-input');
|
|
129
|
+
const text = input?.value.trim();
|
|
130
|
+
if (!text || this.state.streaming) return;
|
|
131
|
+
const { apiKey, model } = this.state;
|
|
132
|
+
if (!apiKey) { this.setState({ status: 'Enter a Gemini API key above.' }); return; }
|
|
133
|
+
input.value = '';
|
|
134
|
+
input.style.height = 'auto';
|
|
135
|
+
const messages = [...this.state.messages, { role: 'user', content: text }];
|
|
136
|
+
this.setState({ messages, streaming: true, status: '', streamingText: '' });
|
|
137
|
+
try {
|
|
138
|
+
const { GoogleGenAI } = await import('https://esm.sh/@google/genai@1');
|
|
139
|
+
const ai = new GoogleGenAI({ apiKey });
|
|
140
|
+
const stream = await ai.models.generateContentStream({
|
|
141
|
+
model,
|
|
142
|
+
contents: convertMessages(messages),
|
|
143
|
+
config: buildConfig(),
|
|
144
|
+
});
|
|
145
|
+
let full = '';
|
|
146
|
+
for await (const chunk of stream) {
|
|
147
|
+
for (const candidate of (chunk.candidates || [])) {
|
|
148
|
+
for (const part of (candidate.content?.parts || [])) {
|
|
149
|
+
if (part.text && !part.thought) { full += part.text; this.setState({ streamingText: full }); }
|
|
150
|
+
}
|
|
88
151
|
}
|
|
89
152
|
}
|
|
153
|
+
const list = document.getElementById('msg-list');
|
|
154
|
+
if (list) list.scrollTop = list.scrollHeight;
|
|
155
|
+
this.setState({ messages: [...messages, { role: 'assistant', content: full || '(empty)' }], streaming: false, streamingText: '' });
|
|
156
|
+
} catch (err) {
|
|
157
|
+
this.setState({ streaming: false, streamingText: '', status: 'Error: ' + (err?.message || String(err)) });
|
|
90
158
|
}
|
|
91
|
-
if (!full) full = '(empty response)';
|
|
92
|
-
modelEl.textContent = full;
|
|
93
|
-
state.messages.push({ role: 'assistant', content: full });
|
|
94
|
-
setStatus('');
|
|
95
|
-
} catch (err) {
|
|
96
|
-
modelEl.remove();
|
|
97
|
-
addMsg('error', 'Error: ' + (err?.message || String(err)));
|
|
98
|
-
state.messages.pop();
|
|
99
|
-
setStatus('');
|
|
100
|
-
} finally {
|
|
101
|
-
state.streaming = false;
|
|
102
|
-
$send.disabled = false;
|
|
103
|
-
$input.focus();
|
|
104
159
|
}
|
|
105
160
|
}
|
|
106
161
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
$form.addEventListener('submit', e => {
|
|
110
|
-
e.preventDefault();
|
|
111
|
-
const text = $input.value.trim();
|
|
112
|
-
if (!text || state.streaming) return;
|
|
113
|
-
$input.value = '';
|
|
114
|
-
$input.style.height = 'auto';
|
|
115
|
-
sendMessage(text);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
$input.addEventListener('keydown', e => {
|
|
119
|
-
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); $form.requestSubmit(); }
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
$clear.addEventListener('click', () => {
|
|
123
|
-
state.messages = [];
|
|
124
|
-
$messages.innerHTML = '';
|
|
125
|
-
setStatus('Cleared.');
|
|
126
|
-
setTimeout(() => setStatus(''), 1500);
|
|
127
|
-
});
|
|
162
|
+
customElements.define('bird-chat', BirdChat);
|
package/docs/index.html
CHANGED
|
@@ -1,59 +1,22 @@
|
|
|
1
1
|
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
2
|
+
<html lang="en" data-theme="dark">
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
-
<title>thebird —
|
|
6
|
+
<title>thebird — Gemini chat</title>
|
|
7
|
+
<script src="https://cdn.tailwindcss.com"></script>
|
|
8
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/rippleui@1.12.1/dist/css/styles.css" />
|
|
9
|
+
<script type="importmap">{"imports":{"webjsx":"https://unpkg.com/webjsx/dist/index.js"}}</script>
|
|
7
10
|
<style>
|
|
8
|
-
|
|
9
|
-
:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
body { background: var(--bg); color: var(--text); font-family: system-ui, sans-serif; height: 100dvh; display: flex; flex-direction: column; }
|
|
15
|
-
header { padding: 1rem 1.5rem; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
|
|
16
|
-
header h1 { font-size: 1.1rem; font-weight: 700; color: var(--accent); }
|
|
17
|
-
header span { font-size: 0.8rem; color: var(--muted); }
|
|
18
|
-
#key-row { display: flex; gap: 0.5rem; flex: 1; min-width: 240px; }
|
|
19
|
-
#api-key { flex: 1; background: var(--surface); border: 1px solid var(--border); color: var(--text); padding: 0.4rem 0.75rem; border-radius: 6px; font-size: 0.85rem; outline: none; }
|
|
20
|
-
#api-key:focus { border-color: var(--accent); }
|
|
21
|
-
#model-select { background: var(--surface); border: 1px solid var(--border); color: var(--text); padding: 0.4rem 0.5rem; border-radius: 6px; font-size: 0.85rem; outline: none; }
|
|
22
|
-
#messages { flex: 1; overflow-y: auto; padding: 1rem 1.5rem; display: flex; flex-direction: column; gap: 0.75rem; }
|
|
23
|
-
.msg { padding: 0.75rem 1rem; border-radius: 8px; font-size: 0.9rem; line-height: 1.6; white-space: pre-wrap; word-break: break-word; max-width: 800px; }
|
|
24
|
-
.msg.user { background: var(--user); align-self: flex-end; border: 1px solid var(--border); }
|
|
25
|
-
.msg.model { background: var(--model); align-self: flex-start; border: 1px solid #1e3a4a; }
|
|
26
|
-
.msg.error { background: #2d1515; border: 1px solid #5a2020; color: #fca5a5; align-self: center; font-size: 0.8rem; }
|
|
27
|
-
form { padding: 1rem 1.5rem; border-top: 1px solid var(--border); display: flex; gap: 0.5rem; }
|
|
28
|
-
textarea { flex: 1; background: var(--surface); border: 1px solid var(--border); color: var(--text); padding: 0.6rem 0.75rem; border-radius: 8px; font-size: 0.9rem; resize: none; outline: none; font-family: inherit; min-height: 42px; max-height: 160px; line-height: 1.5; }
|
|
29
|
-
textarea:focus { border-color: var(--accent); }
|
|
30
|
-
button { background: var(--accent); color: #fff; border: none; padding: 0.6rem 1.25rem; border-radius: 8px; font-size: 0.9rem; cursor: pointer; white-space: nowrap; }
|
|
31
|
-
button:disabled { opacity: 0.4; cursor: not-allowed; }
|
|
32
|
-
button#clear-btn { background: transparent; border: 1px solid var(--border); color: var(--muted); padding: 0.4rem 0.75rem; font-size: 0.8rem; }
|
|
33
|
-
#status { font-size: 0.75rem; color: var(--muted); padding: 0 1.5rem 0.5rem; }
|
|
11
|
+
:root { --bg: #0f1117; }
|
|
12
|
+
html, body { height: 100%; background: var(--bg); }
|
|
13
|
+
bird-chat { display: flex; flex-direction: column; height: 100dvh; }
|
|
14
|
+
.msg-bubble { max-width: 680px; white-space: pre-wrap; word-break: break-word; }
|
|
15
|
+
#msg-list { scroll-behavior: smooth; }
|
|
34
16
|
</style>
|
|
35
17
|
</head>
|
|
36
|
-
<body>
|
|
37
|
-
<
|
|
38
|
-
<h1>thebird</h1>
|
|
39
|
-
<span>Anthropic SDK format → Gemini API</span>
|
|
40
|
-
<div id="key-row">
|
|
41
|
-
<input id="api-key" type="password" placeholder="GEMINI_API_KEY" autocomplete="off" />
|
|
42
|
-
<select id="model-select">
|
|
43
|
-
<option value="gemini-2.0-flash">gemini-2.0-flash</option>
|
|
44
|
-
<option value="gemini-2.0-flash-thinking-exp">gemini-2.0-flash-thinking</option>
|
|
45
|
-
<option value="gemini-1.5-pro">gemini-1.5-pro</option>
|
|
46
|
-
<option value="gemini-1.5-flash">gemini-1.5-flash</option>
|
|
47
|
-
</select>
|
|
48
|
-
<button id="clear-btn" type="button">Clear</button>
|
|
49
|
-
</div>
|
|
50
|
-
</header>
|
|
51
|
-
<div id="messages"></div>
|
|
52
|
-
<div id="status"></div>
|
|
53
|
-
<form id="chat-form">
|
|
54
|
-
<textarea id="input" placeholder="Message… (Shift+Enter for newline)" rows="1"></textarea>
|
|
55
|
-
<button type="submit" id="send-btn">Send</button>
|
|
56
|
-
</form>
|
|
18
|
+
<body class="bg-base-100 text-base-content">
|
|
19
|
+
<bird-chat></bird-chat>
|
|
57
20
|
<script type="module" src="app.js"></script>
|
|
58
21
|
</body>
|
|
59
22
|
</html>
|
package/package.json
CHANGED