@elizaos/capacitor-agent 1.0.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/dist/esm/definitions.d.ts +35 -0
- package/dist/esm/definitions.d.ts.map +1 -0
- package/dist/esm/definitions.js +11 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +43 -0
- package/dist/esm/web.d.ts.map +1 -0
- package/dist/esm/web.js +172 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +188 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +191 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @elizaos/capacitor-agent — Agent lifecycle management for Capacitor.
|
|
3
|
+
*
|
|
4
|
+
* Provides a cross-platform interface for starting, stopping, and
|
|
5
|
+
* communicating with the embedded Eliza agent.
|
|
6
|
+
*
|
|
7
|
+
* - Electrobun desktop: RPC to the main-process AgentManager
|
|
8
|
+
* - iOS/Android/Web: HTTP calls to the API server
|
|
9
|
+
*/
|
|
10
|
+
export interface AgentStatus {
|
|
11
|
+
state: "not_started" | "starting" | "running" | "stopped" | "error";
|
|
12
|
+
agentName: string | null;
|
|
13
|
+
port: number | null;
|
|
14
|
+
startedAt: number | null;
|
|
15
|
+
error: string | null;
|
|
16
|
+
}
|
|
17
|
+
export interface ChatResult {
|
|
18
|
+
text: string;
|
|
19
|
+
agentName: string;
|
|
20
|
+
}
|
|
21
|
+
export interface AgentPlugin {
|
|
22
|
+
/** Start the agent runtime. Resolves when it's ready. */
|
|
23
|
+
start(): Promise<AgentStatus>;
|
|
24
|
+
/** Stop the agent runtime. */
|
|
25
|
+
stop(): Promise<{
|
|
26
|
+
ok: boolean;
|
|
27
|
+
}>;
|
|
28
|
+
/** Get current agent status. */
|
|
29
|
+
getStatus(): Promise<AgentStatus>;
|
|
30
|
+
/** Send a chat message and get the response. */
|
|
31
|
+
chat(options: {
|
|
32
|
+
text: string;
|
|
33
|
+
}): Promise<ChatResult>;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=definitions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definitions.d.ts","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,aAAa,GAAG,UAAU,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,CAAC;IACpE,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,WAAW;IAC1B,yDAAyD;IACzD,KAAK,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IAE9B,8BAA8B;IAC9B,IAAI,IAAI,OAAO,CAAC;QAAE,EAAE,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAEjC,gCAAgC;IAChC,SAAS,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IAElC,gDAAgD;IAChD,IAAI,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CACtD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @elizaos/capacitor-agent — Agent lifecycle management for Capacitor.
|
|
3
|
+
*
|
|
4
|
+
* Provides a cross-platform interface for starting, stopping, and
|
|
5
|
+
* communicating with the embedded Eliza agent.
|
|
6
|
+
*
|
|
7
|
+
* - Electrobun desktop: RPC to the main-process AgentManager
|
|
8
|
+
* - iOS/Android/Web: HTTP calls to the API server
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=definitions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD,cAAc,eAAe,CAAC;AAE9B,eAAO,MAAM,KAAK,aAIhB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { registerPlugin } from "@capacitor/core";
|
|
2
|
+
export * from "./definitions";
|
|
3
|
+
export const Agent = registerPlugin("Agent", {
|
|
4
|
+
web: () => import("./web").then((m) => new m.AgentWeb()),
|
|
5
|
+
// Electrobun uses the preload bridge (agent:start, agent:stop, etc.)
|
|
6
|
+
// iOS/Android will use the web fallback (HTTP to API server) for now
|
|
7
|
+
});
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGjD,cAAc,eAAe,CAAC;AAE9B,MAAM,CAAC,MAAM,KAAK,GAAG,cAAc,CAAc,OAAO,EAAE;IACxD,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC;IACxD,qEAAqE;IACrE,qEAAqE;CACtE,CAAC,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { WebPlugin } from "@capacitor/core";
|
|
2
|
+
import type { AgentPlugin, AgentStatus, ChatResult } from "./definitions";
|
|
3
|
+
/**
|
|
4
|
+
* Web fallback implementation.
|
|
5
|
+
*
|
|
6
|
+
* On non-desktop platforms (iOS, Android, web), the agent runtime runs
|
|
7
|
+
* on a server. This implementation delegates to the HTTP API.
|
|
8
|
+
*
|
|
9
|
+
* In Electrobun the desktop bridge calls the native main-process
|
|
10
|
+
* implementation via RPC instead — this web fallback is only used when
|
|
11
|
+
* no native plugin is available. If the page is served from a non-HTTP
|
|
12
|
+
* origin (e.g. electrobun://), relative fetches would hit the
|
|
13
|
+
* app shell HTML, so we bail early.
|
|
14
|
+
*
|
|
15
|
+
* Local-agent-on-Android (Phase E): when the host UI selects the
|
|
16
|
+
* "Local Agent" tile, it sets `apiBase` to `http://127.0.0.1:31337`,
|
|
17
|
+
* which the runtime mirrors into `window.__ELIZA_API_BASE__`. From this
|
|
18
|
+
* plugin's perspective there is no special case — it simply HTTP-POSTs
|
|
19
|
+
* to `${apiBase}/api/agent/start|stop|status`, which is exactly the same
|
|
20
|
+
* surface Phase B's `ElizaAgentService` exposes. The web fallback path
|
|
21
|
+
* therefore works unchanged for both remote and on-device agents.
|
|
22
|
+
*/
|
|
23
|
+
export declare class AgentWeb extends WebPlugin implements AgentPlugin {
|
|
24
|
+
private legacyConversationStorageKey;
|
|
25
|
+
private readLegacyConversationId;
|
|
26
|
+
private writeLegacyConversationId;
|
|
27
|
+
private ensureLegacyConversationId;
|
|
28
|
+
private chatViaConversation;
|
|
29
|
+
private apiBase;
|
|
30
|
+
private apiToken;
|
|
31
|
+
private authHeaders;
|
|
32
|
+
/** True when we can reach the API via HTTP. */
|
|
33
|
+
private canReachApi;
|
|
34
|
+
start(): Promise<AgentStatus>;
|
|
35
|
+
stop(): Promise<{
|
|
36
|
+
ok: boolean;
|
|
37
|
+
}>;
|
|
38
|
+
getStatus(): Promise<AgentStatus>;
|
|
39
|
+
chat(options: {
|
|
40
|
+
text: string;
|
|
41
|
+
}): Promise<ChatResult>;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=web.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAO1E;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,QAAS,SAAQ,SAAU,YAAW,WAAW;IAC5D,OAAO,CAAC,4BAA4B;IAOpC,OAAO,CAAC,wBAAwB;IAQhC,OAAO,CAAC,yBAAyB;YAUnB,0BAA0B;YA0B1B,mBAAmB;IA6BjC,OAAO,CAAC,OAAO;IAUf,OAAO,CAAC,QAAQ;IAWhB,OAAO,CAAC,WAAW;IAKnB,+CAA+C;IAC/C,OAAO,CAAC,WAAW;IAYb,KAAK,IAAI,OAAO,CAAC,WAAW,CAAC;IAkB7B,IAAI,IAAI,OAAO,CAAC;QAAE,EAAE,EAAE,OAAO,CAAA;KAAE,CAAC;IAWhC,SAAS,IAAI,OAAO,CAAC,WAAW,CAAC;IAgBjC,IAAI,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC;CAM3D"}
|
package/dist/esm/web.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { WebPlugin } from "@capacitor/core";
|
|
2
|
+
/**
|
|
3
|
+
* Web fallback implementation.
|
|
4
|
+
*
|
|
5
|
+
* On non-desktop platforms (iOS, Android, web), the agent runtime runs
|
|
6
|
+
* on a server. This implementation delegates to the HTTP API.
|
|
7
|
+
*
|
|
8
|
+
* In Electrobun the desktop bridge calls the native main-process
|
|
9
|
+
* implementation via RPC instead — this web fallback is only used when
|
|
10
|
+
* no native plugin is available. If the page is served from a non-HTTP
|
|
11
|
+
* origin (e.g. electrobun://), relative fetches would hit the
|
|
12
|
+
* app shell HTML, so we bail early.
|
|
13
|
+
*
|
|
14
|
+
* Local-agent-on-Android (Phase E): when the host UI selects the
|
|
15
|
+
* "Local Agent" tile, it sets `apiBase` to `http://127.0.0.1:31337`,
|
|
16
|
+
* which the runtime mirrors into `window.__ELIZA_API_BASE__`. From this
|
|
17
|
+
* plugin's perspective there is no special case — it simply HTTP-POSTs
|
|
18
|
+
* to `${apiBase}/api/agent/start|stop|status`, which is exactly the same
|
|
19
|
+
* surface Phase B's `ElizaAgentService` exposes. The web fallback path
|
|
20
|
+
* therefore works unchanged for both remote and on-device agents.
|
|
21
|
+
*/
|
|
22
|
+
export class AgentWeb extends WebPlugin {
|
|
23
|
+
legacyConversationStorageKey() {
|
|
24
|
+
const base = this.apiBase() ||
|
|
25
|
+
(typeof window !== "undefined" ? window.location.origin : "same-origin");
|
|
26
|
+
return `eliza_agent_web_conversation:${encodeURIComponent(base)}`;
|
|
27
|
+
}
|
|
28
|
+
readLegacyConversationId() {
|
|
29
|
+
if (typeof window === "undefined")
|
|
30
|
+
return null;
|
|
31
|
+
const stored = window.sessionStorage.getItem(this.legacyConversationStorageKey());
|
|
32
|
+
return stored?.trim() ? stored.trim() : null;
|
|
33
|
+
}
|
|
34
|
+
writeLegacyConversationId(conversationId) {
|
|
35
|
+
if (typeof window === "undefined")
|
|
36
|
+
return;
|
|
37
|
+
const key = this.legacyConversationStorageKey();
|
|
38
|
+
if (conversationId?.trim()) {
|
|
39
|
+
window.sessionStorage.setItem(key, conversationId.trim());
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
window.sessionStorage.removeItem(key);
|
|
43
|
+
}
|
|
44
|
+
async ensureLegacyConversationId() {
|
|
45
|
+
const cached = this.readLegacyConversationId();
|
|
46
|
+
if (cached)
|
|
47
|
+
return cached;
|
|
48
|
+
const res = await fetch(`${this.apiBase()}/api/conversations`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"Content-Type": "application/json",
|
|
52
|
+
...this.authHeaders(),
|
|
53
|
+
},
|
|
54
|
+
body: JSON.stringify({ title: "Quick Chat" }),
|
|
55
|
+
});
|
|
56
|
+
if (!res.ok) {
|
|
57
|
+
throw new Error(`Failed to create conversation: ${res.status}`);
|
|
58
|
+
}
|
|
59
|
+
const data = (await res.json());
|
|
60
|
+
const conversationId = data.conversation?.id?.trim();
|
|
61
|
+
if (!conversationId) {
|
|
62
|
+
throw new Error("Conversation create response missing id");
|
|
63
|
+
}
|
|
64
|
+
this.writeLegacyConversationId(conversationId);
|
|
65
|
+
return conversationId;
|
|
66
|
+
}
|
|
67
|
+
async chatViaConversation(text, retryOnMissingConversation = true) {
|
|
68
|
+
const conversationId = await this.ensureLegacyConversationId();
|
|
69
|
+
const res = await fetch(`${this.apiBase()}/api/conversations/${encodeURIComponent(conversationId)}/messages`, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: {
|
|
72
|
+
"Content-Type": "application/json",
|
|
73
|
+
...this.authHeaders(),
|
|
74
|
+
},
|
|
75
|
+
body: JSON.stringify({ text, channelType: "DM" }),
|
|
76
|
+
});
|
|
77
|
+
if (res.status === 404 && retryOnMissingConversation) {
|
|
78
|
+
this.writeLegacyConversationId(null);
|
|
79
|
+
return this.chatViaConversation(text, false);
|
|
80
|
+
}
|
|
81
|
+
if (!res.ok) {
|
|
82
|
+
throw new Error(`Chat request failed: ${res.status}`);
|
|
83
|
+
}
|
|
84
|
+
return res.json();
|
|
85
|
+
}
|
|
86
|
+
apiBase() {
|
|
87
|
+
const global = typeof window !== "undefined"
|
|
88
|
+
? window.__ELIZA_API_BASE__
|
|
89
|
+
: undefined;
|
|
90
|
+
if (typeof global === "string" && global.trim().length > 0)
|
|
91
|
+
return global;
|
|
92
|
+
// No explicit base — use relative URLs (works on http/https origins).
|
|
93
|
+
return "";
|
|
94
|
+
}
|
|
95
|
+
apiToken() {
|
|
96
|
+
const global = typeof window !== "undefined"
|
|
97
|
+
? window.__ELIZA_API_TOKEN__
|
|
98
|
+
: undefined;
|
|
99
|
+
if (typeof global === "string" && global.trim())
|
|
100
|
+
return global.trim();
|
|
101
|
+
if (typeof window === "undefined")
|
|
102
|
+
return null;
|
|
103
|
+
const stored = window.sessionStorage.getItem("eliza_api_token");
|
|
104
|
+
return stored?.trim() ? stored.trim() : null;
|
|
105
|
+
}
|
|
106
|
+
authHeaders() {
|
|
107
|
+
const token = this.apiToken();
|
|
108
|
+
return token ? { Authorization: `Bearer ${token}` } : {};
|
|
109
|
+
}
|
|
110
|
+
/** True when we can reach the API via HTTP. */
|
|
111
|
+
canReachApi() {
|
|
112
|
+
const global = typeof window !== "undefined"
|
|
113
|
+
? window.__ELIZA_API_BASE__
|
|
114
|
+
: undefined;
|
|
115
|
+
if (typeof global === "string" && global.trim().length > 0)
|
|
116
|
+
return true;
|
|
117
|
+
// No explicit base — relative fetches only work on http(s) origins.
|
|
118
|
+
if (typeof window === "undefined")
|
|
119
|
+
return false;
|
|
120
|
+
const proto = window.location.protocol;
|
|
121
|
+
return proto === "http:" || proto === "https:";
|
|
122
|
+
}
|
|
123
|
+
async start() {
|
|
124
|
+
if (!this.canReachApi()) {
|
|
125
|
+
return {
|
|
126
|
+
state: "not_started",
|
|
127
|
+
agentName: null,
|
|
128
|
+
port: null,
|
|
129
|
+
startedAt: null,
|
|
130
|
+
error: "No API endpoint",
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const res = await fetch(`${this.apiBase()}/api/agent/start`, {
|
|
134
|
+
method: "POST",
|
|
135
|
+
headers: this.authHeaders(),
|
|
136
|
+
});
|
|
137
|
+
const data = await res.json();
|
|
138
|
+
return data.status ?? data;
|
|
139
|
+
}
|
|
140
|
+
async stop() {
|
|
141
|
+
if (!this.canReachApi()) {
|
|
142
|
+
return { ok: false };
|
|
143
|
+
}
|
|
144
|
+
const res = await fetch(`${this.apiBase()}/api/agent/stop`, {
|
|
145
|
+
method: "POST",
|
|
146
|
+
headers: this.authHeaders(),
|
|
147
|
+
});
|
|
148
|
+
return res.json();
|
|
149
|
+
}
|
|
150
|
+
async getStatus() {
|
|
151
|
+
if (!this.canReachApi()) {
|
|
152
|
+
return {
|
|
153
|
+
state: "not_started",
|
|
154
|
+
agentName: null,
|
|
155
|
+
port: null,
|
|
156
|
+
startedAt: null,
|
|
157
|
+
error: "No API endpoint",
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const res = await fetch(`${this.apiBase()}/api/status`, {
|
|
161
|
+
headers: this.authHeaders(),
|
|
162
|
+
});
|
|
163
|
+
return res.json();
|
|
164
|
+
}
|
|
165
|
+
async chat(options) {
|
|
166
|
+
if (!this.canReachApi()) {
|
|
167
|
+
return { text: "Agent API not available", agentName: "System" };
|
|
168
|
+
}
|
|
169
|
+
return this.chatViaConversation(options.text);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAQ5C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,QAAS,SAAQ,SAAS;IAC7B,4BAA4B;QAClC,MAAM,IAAI,GACR,IAAI,CAAC,OAAO,EAAE;YACd,CAAC,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC;QAC3E,OAAO,gCAAgC,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC;IACpE,CAAC;IAEO,wBAAwB;QAC9B,IAAI,OAAO,MAAM,KAAK,WAAW;YAAE,OAAO,IAAI,CAAC;QAC/C,M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,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,EAAE,aAAa,EAAE;YACtD,OAAO,EAAE,IAAI,CAAC,WAAW,EAAE;SAC5B,CAAC,CAAC;QACH,OAAO,GAAG,CAAC,IAAI,EAAE,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,OAAyB;QAClC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;YACxB,OAAO,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;QAClE,CAAC;QACD,OAAO,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD,CAAC;CACF"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@capacitor/core');
|
|
4
|
+
|
|
5
|
+
const Agent = core.registerPlugin("Agent", {
|
|
6
|
+
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.AgentWeb()),
|
|
7
|
+
// Electrobun uses the preload bridge (agent:start, agent:stop, etc.)
|
|
8
|
+
// iOS/Android will use the web fallback (HTTP to API server) for now
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Web fallback implementation.
|
|
13
|
+
*
|
|
14
|
+
* On non-desktop platforms (iOS, Android, web), the agent runtime runs
|
|
15
|
+
* on a server. This implementation delegates to the HTTP API.
|
|
16
|
+
*
|
|
17
|
+
* In Electrobun the desktop bridge calls the native main-process
|
|
18
|
+
* implementation via RPC instead — this web fallback is only used when
|
|
19
|
+
* no native plugin is available. If the page is served from a non-HTTP
|
|
20
|
+
* origin (e.g. electrobun://), relative fetches would hit the
|
|
21
|
+
* app shell HTML, so we bail early.
|
|
22
|
+
*
|
|
23
|
+
* Local-agent-on-Android (Phase E): when the host UI selects the
|
|
24
|
+
* "Local Agent" tile, it sets `apiBase` to `http://127.0.0.1:31337`,
|
|
25
|
+
* which the runtime mirrors into `window.__ELIZA_API_BASE__`. From this
|
|
26
|
+
* plugin's perspective there is no special case — it simply HTTP-POSTs
|
|
27
|
+
* to `${apiBase}/api/agent/start|stop|status`, which is exactly the same
|
|
28
|
+
* surface Phase B's `ElizaAgentService` exposes. The web fallback path
|
|
29
|
+
* therefore works unchanged for both remote and on-device agents.
|
|
30
|
+
*/
|
|
31
|
+
class AgentWeb extends core.WebPlugin {
|
|
32
|
+
legacyConversationStorageKey() {
|
|
33
|
+
const base = this.apiBase() ||
|
|
34
|
+
(typeof window !== "undefined" ? window.location.origin : "same-origin");
|
|
35
|
+
return `eliza_agent_web_conversation:${encodeURIComponent(base)}`;
|
|
36
|
+
}
|
|
37
|
+
readLegacyConversationId() {
|
|
38
|
+
if (typeof window === "undefined")
|
|
39
|
+
return null;
|
|
40
|
+
const stored = window.sessionStorage.getItem(this.legacyConversationStorageKey());
|
|
41
|
+
return stored?.trim() ? stored.trim() : null;
|
|
42
|
+
}
|
|
43
|
+
writeLegacyConversationId(conversationId) {
|
|
44
|
+
if (typeof window === "undefined")
|
|
45
|
+
return;
|
|
46
|
+
const key = this.legacyConversationStorageKey();
|
|
47
|
+
if (conversationId?.trim()) {
|
|
48
|
+
window.sessionStorage.setItem(key, conversationId.trim());
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
window.sessionStorage.removeItem(key);
|
|
52
|
+
}
|
|
53
|
+
async ensureLegacyConversationId() {
|
|
54
|
+
const cached = this.readLegacyConversationId();
|
|
55
|
+
if (cached)
|
|
56
|
+
return cached;
|
|
57
|
+
const res = await fetch(`${this.apiBase()}/api/conversations`, {
|
|
58
|
+
method: "POST",
|
|
59
|
+
headers: {
|
|
60
|
+
"Content-Type": "application/json",
|
|
61
|
+
...this.authHeaders(),
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({ title: "Quick Chat" }),
|
|
64
|
+
});
|
|
65
|
+
if (!res.ok) {
|
|
66
|
+
throw new Error(`Failed to create conversation: ${res.status}`);
|
|
67
|
+
}
|
|
68
|
+
const data = (await res.json());
|
|
69
|
+
const conversationId = data.conversation?.id?.trim();
|
|
70
|
+
if (!conversationId) {
|
|
71
|
+
throw new Error("Conversation create response missing id");
|
|
72
|
+
}
|
|
73
|
+
this.writeLegacyConversationId(conversationId);
|
|
74
|
+
return conversationId;
|
|
75
|
+
}
|
|
76
|
+
async chatViaConversation(text, retryOnMissingConversation = true) {
|
|
77
|
+
const conversationId = await this.ensureLegacyConversationId();
|
|
78
|
+
const res = await fetch(`${this.apiBase()}/api/conversations/${encodeURIComponent(conversationId)}/messages`, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: {
|
|
81
|
+
"Content-Type": "application/json",
|
|
82
|
+
...this.authHeaders(),
|
|
83
|
+
},
|
|
84
|
+
body: JSON.stringify({ text, channelType: "DM" }),
|
|
85
|
+
});
|
|
86
|
+
if (res.status === 404 && retryOnMissingConversation) {
|
|
87
|
+
this.writeLegacyConversationId(null);
|
|
88
|
+
return this.chatViaConversation(text, false);
|
|
89
|
+
}
|
|
90
|
+
if (!res.ok) {
|
|
91
|
+
throw new Error(`Chat request failed: ${res.status}`);
|
|
92
|
+
}
|
|
93
|
+
return res.json();
|
|
94
|
+
}
|
|
95
|
+
apiBase() {
|
|
96
|
+
const global = typeof window !== "undefined"
|
|
97
|
+
? window.__ELIZA_API_BASE__
|
|
98
|
+
: undefined;
|
|
99
|
+
if (typeof global === "string" && global.trim().length > 0)
|
|
100
|
+
return global;
|
|
101
|
+
// No explicit base — use relative URLs (works on http/https origins).
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
apiToken() {
|
|
105
|
+
const global = typeof window !== "undefined"
|
|
106
|
+
? window.__ELIZA_API_TOKEN__
|
|
107
|
+
: undefined;
|
|
108
|
+
if (typeof global === "string" && global.trim())
|
|
109
|
+
return global.trim();
|
|
110
|
+
if (typeof window === "undefined")
|
|
111
|
+
return null;
|
|
112
|
+
const stored = window.sessionStorage.getItem("eliza_api_token");
|
|
113
|
+
return stored?.trim() ? stored.trim() : null;
|
|
114
|
+
}
|
|
115
|
+
authHeaders() {
|
|
116
|
+
const token = this.apiToken();
|
|
117
|
+
return token ? { Authorization: `Bearer ${token}` } : {};
|
|
118
|
+
}
|
|
119
|
+
/** True when we can reach the API via HTTP. */
|
|
120
|
+
canReachApi() {
|
|
121
|
+
const global = typeof window !== "undefined"
|
|
122
|
+
? window.__ELIZA_API_BASE__
|
|
123
|
+
: undefined;
|
|
124
|
+
if (typeof global === "string" && global.trim().length > 0)
|
|
125
|
+
return true;
|
|
126
|
+
// No explicit base — relative fetches only work on http(s) origins.
|
|
127
|
+
if (typeof window === "undefined")
|
|
128
|
+
return false;
|
|
129
|
+
const proto = window.location.protocol;
|
|
130
|
+
return proto === "http:" || proto === "https:";
|
|
131
|
+
}
|
|
132
|
+
async start() {
|
|
133
|
+
if (!this.canReachApi()) {
|
|
134
|
+
return {
|
|
135
|
+
state: "not_started",
|
|
136
|
+
agentName: null,
|
|
137
|
+
port: null,
|
|
138
|
+
startedAt: null,
|
|
139
|
+
error: "No API endpoint",
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
const res = await fetch(`${this.apiBase()}/api/agent/start`, {
|
|
143
|
+
method: "POST",
|
|
144
|
+
headers: this.authHeaders(),
|
|
145
|
+
});
|
|
146
|
+
const data = await res.json();
|
|
147
|
+
return data.status ?? data;
|
|
148
|
+
}
|
|
149
|
+
async stop() {
|
|
150
|
+
if (!this.canReachApi()) {
|
|
151
|
+
return { ok: false };
|
|
152
|
+
}
|
|
153
|
+
const res = await fetch(`${this.apiBase()}/api/agent/stop`, {
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: this.authHeaders(),
|
|
156
|
+
});
|
|
157
|
+
return res.json();
|
|
158
|
+
}
|
|
159
|
+
async getStatus() {
|
|
160
|
+
if (!this.canReachApi()) {
|
|
161
|
+
return {
|
|
162
|
+
state: "not_started",
|
|
163
|
+
agentName: null,
|
|
164
|
+
port: null,
|
|
165
|
+
startedAt: null,
|
|
166
|
+
error: "No API endpoint",
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const res = await fetch(`${this.apiBase()}/api/status`, {
|
|
170
|
+
headers: this.authHeaders(),
|
|
171
|
+
});
|
|
172
|
+
return res.json();
|
|
173
|
+
}
|
|
174
|
+
async chat(options) {
|
|
175
|
+
if (!this.canReachApi()) {
|
|
176
|
+
return { text: "Agent API not available", agentName: "System" };
|
|
177
|
+
}
|
|
178
|
+
return this.chatViaConversation(options.text);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
183
|
+
__proto__: null,
|
|
184
|
+
AgentWeb: AgentWeb
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
exports.Agent = Agent;
|
|
188
|
+
//# sourceMappingURL=plugin.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.cjs.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from \"@capacitor/core\";\nexport * from \"./definitions\";\nexport const Agent = registerPlugin(\"Agent\", {\n web: () => import(\"./web\").then((m) => new m.AgentWeb()),\n // Electrobun uses the preload bridge (agent:start, agent:stop, etc.)\n // iOS/Android will use the web fallback (HTTP to API server) for now\n});\n//# sourceMappingURL=index.js.map","import { WebPlugin } from \"@capacitor/core\";\n/**\n * Web fallback implementation.\n *\n * On non-desktop platforms (iOS, Android, web), the agent runtime runs\n * on a server. This implementation delegates to the HTTP API.\n *\n * In Electrobun the desktop bridge calls the native main-process\n * implementation via RPC instead — this web fallback is only used when\n * no native plugin is available. If the page is served from a non-HTTP\n * origin (e.g. electrobun://), relative fetches would hit the\n * app shell HTML, so we bail early.\n *\n * Local-agent-on-Android (Phase E): when the host UI selects the\n * \"Local Agent\" tile, it sets `apiBase` to `http://127.0.0.1:31337`,\n * which the runtime mirrors into `window.__ELIZA_API_BASE__`. From this\n * plugin's perspective there is no special case — it simply HTTP-POSTs\n * to `${apiBase}/api/agent/start|stop|status`, which is exactly the same\n * surface Phase B's `ElizaAgentService` exposes. The web fallback path\n * therefore works unchanged for both remote and on-device agents.\n */\nexport class AgentWeb extends WebPlugin {\n legacyConversationStorageKey() {\n const base = this.apiBase() ||\n (typeof window !== \"undefined\" ? window.location.origin : \"same-origin\");\n return `eliza_agent_web_conversation:${encodeURIComponent(base)}`;\n }\n readLegacyConversationId() {\n if (typeof window === \"undefined\")\n return null;\n const stored = window.sessionStorage.getItem(this.legacyConversationStorageKey());\n return stored?.trim() ? stored.trim() : null;\n }\n writeLegacyConversationId(conversationId) {\n if (typeof window === \"undefined\")\n return;\n const key = this.legacyConversationStorageKey();\n if (conversationId?.trim()) {\n window.sessionStorage.setItem(key, conversationId.trim());\n return;\n }\n window.sessionStorage.removeItem(key);\n }\n async ensureLegacyConversationId() {\n const cached = this.readLegacyConversationId();\n if (cached)\n return cached;\n const res = await fetch(`${this.apiBase()}/api/conversations`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.authHeaders(),\n },\n body: JSON.stringify({ title: \"Quick Chat\" }),\n });\n if (!res.ok) {\n throw new Error(`Failed to create conversation: ${res.status}`);\n }\n const data = (await res.json());\n const conversationId = data.conversation?.id?.trim();\n if (!conversationId) {\n throw new Error(\"Conversation create response missing id\");\n }\n this.writeLegacyConversationId(conversationId);\n return conversationId;\n }\n async chatViaConversation(text, retryOnMissingConversation = true) {\n const conversationId = await this.ensureLegacyConversationId();\n const res = await fetch(`${this.apiBase()}/api/conversations/${encodeURIComponent(conversationId)}/messages`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.authHeaders(),\n },\n body: JSON.stringify({ text, channelType: \"DM\" }),\n });\n if (res.status === 404 && retryOnMissingConversation) {\n this.writeLegacyConversationId(null);\n return this.chatViaConversation(text, false);\n }\n if (!res.ok) {\n throw new Error(`Chat request failed: ${res.status}`);\n }\n return res.json();\n }\n apiBase() {\n const global = typeof window !== \"undefined\"\n ? window.__ELIZA_API_BASE__\n : undefined;\n if (typeof global === \"string\" && global.trim().length > 0)\n return global;\n // No explicit base — use relative URLs (works on http/https origins).\n return \"\";\n }\n apiToken() {\n const global = typeof window !== \"undefined\"\n ? window.__ELIZA_API_TOKEN__\n : undefined;\n if (typeof global === \"string\" && global.trim())\n return global.trim();\n if (typeof window === \"undefined\")\n return null;\n const stored = window.sessionStorage.getItem(\"eliza_api_token\");\n return stored?.trim() ? stored.trim() : null;\n }\n authHeaders() {\n const token = this.apiToken();\n return token ? { Authorization: `Bearer ${token}` } : {};\n }\n /** True when we can reach the API via HTTP. */\n canReachApi() {\n const global = typeof window !== \"undefined\"\n ? window.__ELIZA_API_BASE__\n : undefined;\n if (typeof global === \"string\" && global.trim().length > 0)\n return true;\n // No explicit base — relative fetches only work on http(s) origins.\n if (typeof window === \"undefined\")\n return false;\n const proto = window.location.protocol;\n return proto === \"http:\" || proto === \"https:\";\n }\n async start() {\n if (!this.canReachApi()) {\n return {\n state: \"not_started\",\n agentName: null,\n port: null,\n startedAt: null,\n error: \"No API endpoint\",\n };\n }\n const res = await fetch(`${this.apiBase()}/api/agent/start`, {\n method: \"POST\",\n headers: this.authHeaders(),\n });\n const data = await res.json();\n return data.status ?? data;\n }\n async stop() {\n if (!this.canReachApi()) {\n return { ok: false };\n }\n const res = await fetch(`${this.apiBase()}/api/agent/stop`, {\n method: \"POST\",\n headers: this.authHeaders(),\n });\n return res.json();\n }\n async getStatus() {\n if (!this.canReachApi()) {\n return {\n state: \"not_started\",\n agentName: null,\n port: null,\n startedAt: null,\n error: \"No API endpoint\",\n };\n }\n const res = await fetch(`${this.apiBase()}/api/status`, {\n headers: this.authHeaders(),\n });\n return res.json();\n }\n async chat(options) {\n if (!this.canReachApi()) {\n return { text: \"Agent API not available\", agentName: \"System\" };\n }\n return this.chatViaConversation(options.text);\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;;AA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,IAAI,EAAE;AACzB,IAAI;AACJ,IAAI,MAAM,IAAI,CAAC,OAAO,EAAE;AACxB,QAAQ,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE;AACjC,YAAY,OAAO,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,QAAQ,EAAE;AAC3E,QAAQ;AACR,QAAQ,OAAO,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,IAAI,CAAC;AACrD,IAAI;AACJ;;;;;;;;;"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
var capacitorAgent = (function (exports, core) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const Agent = core.registerPlugin("Agent", {
|
|
5
|
+
web: () => Promise.resolve().then(function () { return web; }).then((m) => new m.AgentWeb()),
|
|
6
|
+
// Electrobun uses the preload bridge (agent:start, agent:stop, etc.)
|
|
7
|
+
// iOS/Android will use the web fallback (HTTP to API server) for now
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Web fallback implementation.
|
|
12
|
+
*
|
|
13
|
+
* On non-desktop platforms (iOS, Android, web), the agent runtime runs
|
|
14
|
+
* on a server. This implementation delegates to the HTTP API.
|
|
15
|
+
*
|
|
16
|
+
* In Electrobun the desktop bridge calls the native main-process
|
|
17
|
+
* implementation via RPC instead — this web fallback is only used when
|
|
18
|
+
* no native plugin is available. If the page is served from a non-HTTP
|
|
19
|
+
* origin (e.g. electrobun://), relative fetches would hit the
|
|
20
|
+
* app shell HTML, so we bail early.
|
|
21
|
+
*
|
|
22
|
+
* Local-agent-on-Android (Phase E): when the host UI selects the
|
|
23
|
+
* "Local Agent" tile, it sets `apiBase` to `http://127.0.0.1:31337`,
|
|
24
|
+
* which the runtime mirrors into `window.__ELIZA_API_BASE__`. From this
|
|
25
|
+
* plugin's perspective there is no special case — it simply HTTP-POSTs
|
|
26
|
+
* to `${apiBase}/api/agent/start|stop|status`, which is exactly the same
|
|
27
|
+
* surface Phase B's `ElizaAgentService` exposes. The web fallback path
|
|
28
|
+
* therefore works unchanged for both remote and on-device agents.
|
|
29
|
+
*/
|
|
30
|
+
class AgentWeb extends core.WebPlugin {
|
|
31
|
+
legacyConversationStorageKey() {
|
|
32
|
+
const base = this.apiBase() ||
|
|
33
|
+
(typeof window !== "undefined" ? window.location.origin : "same-origin");
|
|
34
|
+
return `eliza_agent_web_conversation:${encodeURIComponent(base)}`;
|
|
35
|
+
}
|
|
36
|
+
readLegacyConversationId() {
|
|
37
|
+
if (typeof window === "undefined")
|
|
38
|
+
return null;
|
|
39
|
+
const stored = window.sessionStorage.getItem(this.legacyConversationStorageKey());
|
|
40
|
+
return stored?.trim() ? stored.trim() : null;
|
|
41
|
+
}
|
|
42
|
+
writeLegacyConversationId(conversationId) {
|
|
43
|
+
if (typeof window === "undefined")
|
|
44
|
+
return;
|
|
45
|
+
const key = this.legacyConversationStorageKey();
|
|
46
|
+
if (conversationId?.trim()) {
|
|
47
|
+
window.sessionStorage.setItem(key, conversationId.trim());
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
window.sessionStorage.removeItem(key);
|
|
51
|
+
}
|
|
52
|
+
async ensureLegacyConversationId() {
|
|
53
|
+
const cached = this.readLegacyConversationId();
|
|
54
|
+
if (cached)
|
|
55
|
+
return cached;
|
|
56
|
+
const res = await fetch(`${this.apiBase()}/api/conversations`, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: {
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
...this.authHeaders(),
|
|
61
|
+
},
|
|
62
|
+
body: JSON.stringify({ title: "Quick Chat" }),
|
|
63
|
+
});
|
|
64
|
+
if (!res.ok) {
|
|
65
|
+
throw new Error(`Failed to create conversation: ${res.status}`);
|
|
66
|
+
}
|
|
67
|
+
const data = (await res.json());
|
|
68
|
+
const conversationId = data.conversation?.id?.trim();
|
|
69
|
+
if (!conversationId) {
|
|
70
|
+
throw new Error("Conversation create response missing id");
|
|
71
|
+
}
|
|
72
|
+
this.writeLegacyConversationId(conversationId);
|
|
73
|
+
return conversationId;
|
|
74
|
+
}
|
|
75
|
+
async chatViaConversation(text, retryOnMissingConversation = true) {
|
|
76
|
+
const conversationId = await this.ensureLegacyConversationId();
|
|
77
|
+
const res = await fetch(`${this.apiBase()}/api/conversations/${encodeURIComponent(conversationId)}/messages`, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: {
|
|
80
|
+
"Content-Type": "application/json",
|
|
81
|
+
...this.authHeaders(),
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify({ text, channelType: "DM" }),
|
|
84
|
+
});
|
|
85
|
+
if (res.status === 404 && retryOnMissingConversation) {
|
|
86
|
+
this.writeLegacyConversationId(null);
|
|
87
|
+
return this.chatViaConversation(text, false);
|
|
88
|
+
}
|
|
89
|
+
if (!res.ok) {
|
|
90
|
+
throw new Error(`Chat request failed: ${res.status}`);
|
|
91
|
+
}
|
|
92
|
+
return res.json();
|
|
93
|
+
}
|
|
94
|
+
apiBase() {
|
|
95
|
+
const global = typeof window !== "undefined"
|
|
96
|
+
? window.__ELIZA_API_BASE__
|
|
97
|
+
: undefined;
|
|
98
|
+
if (typeof global === "string" && global.trim().length > 0)
|
|
99
|
+
return global;
|
|
100
|
+
// No explicit base — use relative URLs (works on http/https origins).
|
|
101
|
+
return "";
|
|
102
|
+
}
|
|
103
|
+
apiToken() {
|
|
104
|
+
const global = typeof window !== "undefined"
|
|
105
|
+
? window.__ELIZA_API_TOKEN__
|
|
106
|
+
: undefined;
|
|
107
|
+
if (typeof global === "string" && global.trim())
|
|
108
|
+
return global.trim();
|
|
109
|
+
if (typeof window === "undefined")
|
|
110
|
+
return null;
|
|
111
|
+
const stored = window.sessionStorage.getItem("eliza_api_token");
|
|
112
|
+
return stored?.trim() ? stored.trim() : null;
|
|
113
|
+
}
|
|
114
|
+
authHeaders() {
|
|
115
|
+
const token = this.apiToken();
|
|
116
|
+
return token ? { Authorization: `Bearer ${token}` } : {};
|
|
117
|
+
}
|
|
118
|
+
/** True when we can reach the API via HTTP. */
|
|
119
|
+
canReachApi() {
|
|
120
|
+
const global = typeof window !== "undefined"
|
|
121
|
+
? window.__ELIZA_API_BASE__
|
|
122
|
+
: undefined;
|
|
123
|
+
if (typeof global === "string" && global.trim().length > 0)
|
|
124
|
+
return true;
|
|
125
|
+
// No explicit base — relative fetches only work on http(s) origins.
|
|
126
|
+
if (typeof window === "undefined")
|
|
127
|
+
return false;
|
|
128
|
+
const proto = window.location.protocol;
|
|
129
|
+
return proto === "http:" || proto === "https:";
|
|
130
|
+
}
|
|
131
|
+
async start() {
|
|
132
|
+
if (!this.canReachApi()) {
|
|
133
|
+
return {
|
|
134
|
+
state: "not_started",
|
|
135
|
+
agentName: null,
|
|
136
|
+
port: null,
|
|
137
|
+
startedAt: null,
|
|
138
|
+
error: "No API endpoint",
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const res = await fetch(`${this.apiBase()}/api/agent/start`, {
|
|
142
|
+
method: "POST",
|
|
143
|
+
headers: this.authHeaders(),
|
|
144
|
+
});
|
|
145
|
+
const data = await res.json();
|
|
146
|
+
return data.status ?? data;
|
|
147
|
+
}
|
|
148
|
+
async stop() {
|
|
149
|
+
if (!this.canReachApi()) {
|
|
150
|
+
return { ok: false };
|
|
151
|
+
}
|
|
152
|
+
const res = await fetch(`${this.apiBase()}/api/agent/stop`, {
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: this.authHeaders(),
|
|
155
|
+
});
|
|
156
|
+
return res.json();
|
|
157
|
+
}
|
|
158
|
+
async getStatus() {
|
|
159
|
+
if (!this.canReachApi()) {
|
|
160
|
+
return {
|
|
161
|
+
state: "not_started",
|
|
162
|
+
agentName: null,
|
|
163
|
+
port: null,
|
|
164
|
+
startedAt: null,
|
|
165
|
+
error: "No API endpoint",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const res = await fetch(`${this.apiBase()}/api/status`, {
|
|
169
|
+
headers: this.authHeaders(),
|
|
170
|
+
});
|
|
171
|
+
return res.json();
|
|
172
|
+
}
|
|
173
|
+
async chat(options) {
|
|
174
|
+
if (!this.canReachApi()) {
|
|
175
|
+
return { text: "Agent API not available", agentName: "System" };
|
|
176
|
+
}
|
|
177
|
+
return this.chatViaConversation(options.text);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
182
|
+
__proto__: null,
|
|
183
|
+
AgentWeb: AgentWeb
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
exports.Agent = Agent;
|
|
187
|
+
|
|
188
|
+
return exports;
|
|
189
|
+
|
|
190
|
+
})({}, capacitorExports);
|
|
191
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["esm/index.js","esm/web.js"],"sourcesContent":["import { registerPlugin } from \"@capacitor/core\";\nexport * from \"./definitions\";\nexport const Agent = registerPlugin(\"Agent\", {\n web: () => import(\"./web\").then((m) => new m.AgentWeb()),\n // Electrobun uses the preload bridge (agent:start, agent:stop, etc.)\n // iOS/Android will use the web fallback (HTTP to API server) for now\n});\n//# sourceMappingURL=index.js.map","import { WebPlugin } from \"@capacitor/core\";\n/**\n * Web fallback implementation.\n *\n * On non-desktop platforms (iOS, Android, web), the agent runtime runs\n * on a server. This implementation delegates to the HTTP API.\n *\n * In Electrobun the desktop bridge calls the native main-process\n * implementation via RPC instead — this web fallback is only used when\n * no native plugin is available. If the page is served from a non-HTTP\n * origin (e.g. electrobun://), relative fetches would hit the\n * app shell HTML, so we bail early.\n *\n * Local-agent-on-Android (Phase E): when the host UI selects the\n * \"Local Agent\" tile, it sets `apiBase` to `http://127.0.0.1:31337`,\n * which the runtime mirrors into `window.__ELIZA_API_BASE__`. From this\n * plugin's perspective there is no special case — it simply HTTP-POSTs\n * to `${apiBase}/api/agent/start|stop|status`, which is exactly the same\n * surface Phase B's `ElizaAgentService` exposes. The web fallback path\n * therefore works unchanged for both remote and on-device agents.\n */\nexport class AgentWeb extends WebPlugin {\n legacyConversationStorageKey() {\n const base = this.apiBase() ||\n (typeof window !== \"undefined\" ? window.location.origin : \"same-origin\");\n return `eliza_agent_web_conversation:${encodeURIComponent(base)}`;\n }\n readLegacyConversationId() {\n if (typeof window === \"undefined\")\n return null;\n const stored = window.sessionStorage.getItem(this.legacyConversationStorageKey());\n return stored?.trim() ? stored.trim() : null;\n }\n writeLegacyConversationId(conversationId) {\n if (typeof window === \"undefined\")\n return;\n const key = this.legacyConversationStorageKey();\n if (conversationId?.trim()) {\n window.sessionStorage.setItem(key, conversationId.trim());\n return;\n }\n window.sessionStorage.removeItem(key);\n }\n async ensureLegacyConversationId() {\n const cached = this.readLegacyConversationId();\n if (cached)\n return cached;\n const res = await fetch(`${this.apiBase()}/api/conversations`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.authHeaders(),\n },\n body: JSON.stringify({ title: \"Quick Chat\" }),\n });\n if (!res.ok) {\n throw new Error(`Failed to create conversation: ${res.status}`);\n }\n const data = (await res.json());\n const conversationId = data.conversation?.id?.trim();\n if (!conversationId) {\n throw new Error(\"Conversation create response missing id\");\n }\n this.writeLegacyConversationId(conversationId);\n return conversationId;\n }\n async chatViaConversation(text, retryOnMissingConversation = true) {\n const conversationId = await this.ensureLegacyConversationId();\n const res = await fetch(`${this.apiBase()}/api/conversations/${encodeURIComponent(conversationId)}/messages`, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.authHeaders(),\n },\n body: JSON.stringify({ text, channelType: \"DM\" }),\n });\n if (res.status === 404 && retryOnMissingConversation) {\n this.writeLegacyConversationId(null);\n return this.chatViaConversation(text, false);\n }\n if (!res.ok) {\n throw new Error(`Chat request failed: ${res.status}`);\n }\n return res.json();\n }\n apiBase() {\n const global = typeof window !== \"undefined\"\n ? window.__ELIZA_API_BASE__\n : undefined;\n if (typeof global === \"string\" && global.trim().length > 0)\n return global;\n // No explicit base — use relative URLs (works on http/https origins).\n return \"\";\n }\n apiToken() {\n const global = typeof window !== \"undefined\"\n ? window.__ELIZA_API_TOKEN__\n : undefined;\n if (typeof global === \"string\" && global.trim())\n return global.trim();\n if (typeof window === \"undefined\")\n return null;\n const stored = window.sessionStorage.getItem(\"eliza_api_token\");\n return stored?.trim() ? stored.trim() : null;\n }\n authHeaders() {\n const token = this.apiToken();\n return token ? { Authorization: `Bearer ${token}` } : {};\n }\n /** True when we can reach the API via HTTP. */\n canReachApi() {\n const global = typeof window !== \"undefined\"\n ? window.__ELIZA_API_BASE__\n : undefined;\n if (typeof global === \"string\" && global.trim().length > 0)\n return true;\n // No explicit base — relative fetches only work on http(s) origins.\n if (typeof window === \"undefined\")\n return false;\n const proto = window.location.protocol;\n return proto === \"http:\" || proto === \"https:\";\n }\n async start() {\n if (!this.canReachApi()) {\n return {\n state: \"not_started\",\n agentName: null,\n port: null,\n startedAt: null,\n error: \"No API endpoint\",\n };\n }\n const res = await fetch(`${this.apiBase()}/api/agent/start`, {\n method: \"POST\",\n headers: this.authHeaders(),\n });\n const data = await res.json();\n return data.status ?? data;\n }\n async stop() {\n if (!this.canReachApi()) {\n return { ok: false };\n }\n const res = await fetch(`${this.apiBase()}/api/agent/stop`, {\n method: \"POST\",\n headers: this.authHeaders(),\n });\n return res.json();\n }\n async getStatus() {\n if (!this.canReachApi()) {\n return {\n state: \"not_started\",\n agentName: null,\n port: null,\n startedAt: null,\n error: \"No API endpoint\",\n };\n }\n const res = await fetch(`${this.apiBase()}/api/status`, {\n headers: this.authHeaders(),\n });\n return res.json();\n }\n async chat(options) {\n if (!this.canReachApi()) {\n return { text: \"Agent API not available\", agentName: \"System\" };\n }\n return this.chatViaConversation(options.text);\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["registerPlugin","WebPlugin"],"mappings":";;;AAEY,UAAC,KAAK,GAAGA,mBAAc,CAAC,OAAO,EAAE;IAC7C,IAAI,G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AAE;IACxB,QAAQ,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE;IACjC,YAAY,OAAO,EAAE,IAAI,EAAE,yBAAyB,EAAE,SAAS,EAAE,QAAQ,EAAE;IAC3E,QAAQ;IACR,QAAQ,OAAO,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,IAAI,CAAC;IACrD,IAAI;IACJ;;;;;;;;;;;;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elizaos/capacitor-agent",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Starts, stops, and monitors the embedded Eliza agent runtime.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agent-runtime",
|
|
7
|
+
"lifecycle",
|
|
8
|
+
"chat",
|
|
9
|
+
"runtime-control"
|
|
10
|
+
],
|
|
11
|
+
"main": "./dist/plugin.cjs.js",
|
|
12
|
+
"module": "./dist/esm/index.js",
|
|
13
|
+
"types": "./dist/esm/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/esm/index.d.ts",
|
|
17
|
+
"import": "./dist/esm/index.js",
|
|
18
|
+
"require": "./dist/plugin.cjs.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"unpkg": "dist/plugin.js",
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/"
|
|
25
|
+
],
|
|
26
|
+
"author": "elizaOS",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/elizaOS/eliza.git",
|
|
31
|
+
"directory": "apps/app/plugins/agent"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "npm run clean && tsc && rollup -c rollup.config.mjs",
|
|
35
|
+
"clean": "rimraf ./dist",
|
|
36
|
+
"prepublishOnly": "npm run build",
|
|
37
|
+
"watch": "tsc --watch"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@capacitor/core": "^8.3.1",
|
|
41
|
+
"rimraf": "^6.0.0",
|
|
42
|
+
"rollup": "^4.60.2",
|
|
43
|
+
"typescript": "^6.0.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@capacitor/core": "^8.3.1"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"capacitor": {
|
|
52
|
+
"ios": {
|
|
53
|
+
"src": "ios",
|
|
54
|
+
"podName": "ElizaCapacitorAgent"
|
|
55
|
+
},
|
|
56
|
+
"android": {
|
|
57
|
+
"src": "android"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|