clawser-embed 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.
- package/LICENSE +21 -0
- package/README.md +37 -0
- package/package.json +16 -0
- package/src/index.mjs +136 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Clawser Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# clawser-embed
|
|
2
|
+
|
|
3
|
+
Embeddable Clawser agent pod — drop a Clawser-powered agent into any web app.
|
|
4
|
+
|
|
5
|
+
`EmbeddedPod` extends `browsermesh-pod`'s `Pod` with a minimal messaging API
|
|
6
|
+
(`sendMessage`, `on`/`off`/`emit`) and a lazy-attached agent slot, so a host
|
|
7
|
+
app can wire up its own `ClawserAgent` instance (from the main
|
|
8
|
+
[clawser](https://github.com/johnhenry/clawser) repo) and drive it through a
|
|
9
|
+
stable embedding surface without depending on the full Clawser UI.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install clawser-embed browsermesh-pod
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { EmbeddedPod } from 'clawser-embed'
|
|
21
|
+
|
|
22
|
+
const pod = new EmbeddedPod({ containerId: 'my-agent', agent: myClawserAgent })
|
|
23
|
+
|
|
24
|
+
pod.on('response', (msg) => console.log(msg))
|
|
25
|
+
|
|
26
|
+
const { content, toolCalls } = await pod.sendMessage('Summarize this page')
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`config.agent` accepts any object shaped like `clawser-agent.js`'s
|
|
30
|
+
`ClawserAgent` — this package doesn't depend on the main Clawser codebase,
|
|
31
|
+
so the agent instance is duck-typed (`sendMessage`, `getEventLog().query()`,
|
|
32
|
+
`run()`).
|
|
33
|
+
|
|
34
|
+
## Backward compatibility
|
|
35
|
+
|
|
36
|
+
`ClawserEmbed` is exported as an alias of `EmbeddedPod` for callers migrating
|
|
37
|
+
from an earlier naming.
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "clawser-embed",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Embeddable Clawser agent pod — drop a Clawser-powered agent into any web app",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": ["src", "LICENSE", "README.md"],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": { "type": "git", "url": "https://github.com/johnhenry/clawser" },
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"browsermesh-pod": "^0.1.0"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// clawser-embed — standalone npm distribution of EmbeddedPod.
|
|
2
|
+
//
|
|
3
|
+
// This is a synced copy of web/clawser-embed.js from the main Clawser repo,
|
|
4
|
+
// adapted for standalone npm distribution: the single import is rewritten
|
|
5
|
+
// from the repo-internal bridge `./packages-pod.js` to the published
|
|
6
|
+
// `browsermesh-pod` package directly. If web/clawser-embed.js changes,
|
|
7
|
+
// mirror the change here (a build step to generate this file automatically
|
|
8
|
+
// is a natural follow-up, not yet built).
|
|
9
|
+
//
|
|
10
|
+
// EmbeddedPod: Drop-in class for embedding the Clawser agent into any web app.
|
|
11
|
+
// Extends Pod with container rendering, messaging, and lazy agent init.
|
|
12
|
+
// Re-exports as ClawserEmbed for backward compatibility.
|
|
13
|
+
|
|
14
|
+
import { Pod } from 'browsermesh-pod'
|
|
15
|
+
|
|
16
|
+
// ── EmbeddedPod ────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Embeddable Clawser agent pod.
|
|
20
|
+
* Provides a minimal API for integrating the agent into external web apps.
|
|
21
|
+
* Extends Pod for identity, discovery, and peer messaging.
|
|
22
|
+
*/
|
|
23
|
+
export class EmbeddedPod extends Pod {
|
|
24
|
+
#config
|
|
25
|
+
#agent = null
|
|
26
|
+
#listeners = new Map()
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {object} [config]
|
|
30
|
+
* @param {string} [config.containerId] - DOM element ID to render into
|
|
31
|
+
* @param {string} [config.provider] - Default LLM provider
|
|
32
|
+
* @param {string} [config.model] - Default model
|
|
33
|
+
* @param {object} [config.tools] - Tool configuration overrides
|
|
34
|
+
* @param {object} [config.theme] - UI theme overrides
|
|
35
|
+
* @param {object} [config.agent] - Pre-configured agent instance (a ClawserAgent
|
|
36
|
+
* from the main clawser repo; typed as `object` here since this package
|
|
37
|
+
* doesn't depend on clawser-agent.js)
|
|
38
|
+
*/
|
|
39
|
+
constructor(config = {}) {
|
|
40
|
+
super()
|
|
41
|
+
this.#config = {
|
|
42
|
+
containerId: config.containerId || 'clawser',
|
|
43
|
+
provider: config.provider || null,
|
|
44
|
+
model: config.model || null,
|
|
45
|
+
tools: config.tools || {},
|
|
46
|
+
theme: config.theme || {},
|
|
47
|
+
...config,
|
|
48
|
+
}
|
|
49
|
+
if (config.agent) this.#agent = config.agent
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get config() { return { ...this.#config } }
|
|
53
|
+
|
|
54
|
+
/** Get the attached agent (if any). */
|
|
55
|
+
get agent() { return this.#agent }
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Attach or replace the agent instance.
|
|
59
|
+
* @param {object} agent
|
|
60
|
+
*/
|
|
61
|
+
setAgent(agent) { this.#agent = agent }
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Send a message to the agent.
|
|
65
|
+
* @param {string} text - User message
|
|
66
|
+
* @param {object} [opts] - Options (streaming, model override, etc.)
|
|
67
|
+
* @returns {Promise<{ content: string, toolCalls?: Array }>}
|
|
68
|
+
*/
|
|
69
|
+
async sendMessage(text, opts = {}) {
|
|
70
|
+
if (!this.#agent) {
|
|
71
|
+
throw new Error('No agent attached. Call setAgent(agent) or pass { agent } in config before sending messages.')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 1. Add the user message to agent history
|
|
75
|
+
this.#agent.sendMessage(text, opts)
|
|
76
|
+
|
|
77
|
+
// Snapshot event log length so we can extract tool_call events from this run
|
|
78
|
+
const logBefore = this.#agent.getEventLog().query({ type: 'tool_call' }).length
|
|
79
|
+
|
|
80
|
+
// 2. Run the agent (handles tool call loops internally)
|
|
81
|
+
const result = await this.#agent.run()
|
|
82
|
+
|
|
83
|
+
// 3. Extract tool calls that occurred during this run from the event log
|
|
84
|
+
const allToolEvents = this.#agent.getEventLog().query({ type: 'tool_call' })
|
|
85
|
+
const newToolEvents = allToolEvents.slice(logBefore)
|
|
86
|
+
const toolCalls = newToolEvents.map(evt => ({
|
|
87
|
+
id: evt.data.call_id,
|
|
88
|
+
name: evt.data.name,
|
|
89
|
+
arguments: evt.data.arguments,
|
|
90
|
+
}))
|
|
91
|
+
|
|
92
|
+
// 4. Return normalized response
|
|
93
|
+
if (result.status === 1) {
|
|
94
|
+
return { content: result.data, toolCalls, usage: result.usage, model: result.model }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Error or blocked
|
|
98
|
+
return { content: result.data || '', toolCalls, error: result.status < 0, usage: result.usage }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Register an event listener.
|
|
103
|
+
* @param {string} event
|
|
104
|
+
* @param {Function} fn
|
|
105
|
+
*/
|
|
106
|
+
on(event, fn) {
|
|
107
|
+
const s = this.#listeners.get(event) || new Set()
|
|
108
|
+
s.add(fn)
|
|
109
|
+
this.#listeners.set(event, s)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Remove an event listener.
|
|
114
|
+
* @param {string} event
|
|
115
|
+
* @param {Function} fn
|
|
116
|
+
*/
|
|
117
|
+
off(event, fn) {
|
|
118
|
+
this.#listeners.get(event)?.delete(fn)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Emit an event to all registered listeners.
|
|
123
|
+
* @param {string} event
|
|
124
|
+
* @param {...any} args
|
|
125
|
+
*/
|
|
126
|
+
emit(event, ...args) {
|
|
127
|
+
for (const fn of this.#listeners.get(event) || []) fn(...args)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
_onMessage(msg) {
|
|
131
|
+
// Subclass hook — forward pod messages to the event bus
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Backward-compatible alias */
|
|
136
|
+
export const ClawserEmbed = EmbeddedPod
|