@ai-ecoverse/slicc-agent 1.2.0 → 2.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/README.md +33 -6
- package/dist/client.js +64 -0
- package/dist/client.js.map +7 -0
- package/dist/control.js +69 -0
- package/dist/control.js.map +7 -0
- package/dist/host.js +82 -0
- package/dist/host.js.map +7 -0
- package/dist/index.js +26 -4
- package/dist/index.js.map +2 -2
- package/dist/owner.js +34 -0
- package/dist/owner.js.map +7 -0
- package/dist/services.js +12 -0
- package/dist/services.js.map +7 -0
- package/dist/spectrum/agent-port.js +156 -0
- package/dist/spectrum/agent-port.js.map +7 -0
- package/dist/spectrum/emitter.js +20 -0
- package/dist/spectrum/emitter.js.map +7 -0
- package/dist/spectrum/index.js +17 -0
- package/dist/spectrum/index.js.map +7 -0
- package/dist/spectrum/messages.js +161 -0
- package/dist/spectrum/messages.js.map +7 -0
- package/dist/types/client.d.ts +14 -0
- package/dist/types/control.d.ts +4 -0
- package/dist/types/host.d.ts +13 -0
- package/dist/types/index.d.ts +7 -1
- package/dist/types/owner.d.ts +21 -0
- package/dist/types/services.d.ts +59 -0
- package/dist/types/spectrum/agent-port.d.ts +32 -0
- package/dist/types/spectrum/emitter.d.ts +7 -0
- package/dist/types/spectrum/index.d.ts +7 -0
- package/dist/types/spectrum/messages.d.ts +37 -0
- package/dist/types/wire.d.ts +27 -0
- package/dist/types/worker.d.ts +5 -0
- package/dist/wire.js +164 -0
- package/dist/wire.js.map +7 -0
- package/dist/worker.js +14 -0
- package/dist/worker.js.map +7 -0
- package/package.json +12 -1
- package/dist/port.js +0 -57
- package/dist/port.js.map +0 -7
- package/dist/types/port.d.ts +0 -23
package/dist/wire.js.map
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/wire.ts"],
|
|
4
|
+
"sourcesContent": ["import type { ByteTransportFactory, ByteTransportHandlers } from '@earendil-works/pi-client';\nimport type { ServerListener } from '@earendil-works/pi-server';\n\ntype ByteConnectionAcceptor = Parameters<ServerListener['start']>[0];\ntype ByteConnection = Parameters<ByteConnectionAcceptor>[0];\ntype ByteConnectionHandler = ReturnType<ByteConnectionAcceptor>;\n\nexport interface PortEndpoint {\n postMessage(message: unknown, transfer?: Transferable[]): void;\n addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;\n addEventListener(\n type: 'close' | 'messageerror' | 'error',\n listener: (event: Event) => void\n ): void;\n start?(): void;\n close?(): void;\n}\n\ntype Frame = { hello: string } | { close: true } | Uint8Array;\n\nfunction post(endpoint: PortEndpoint, chunk: Uint8Array): void {\n const copy = chunk.slice();\n endpoint.postMessage(copy, [copy.buffer]);\n}\n\nfunction unrefable(id: number): object {\n return {\n unref() {\n return this;\n },\n ref() {\n return this;\n },\n [Symbol.toPrimitive]: () => id,\n };\n}\n\nexport function withUnrefTimers<T>(\n run: () => T,\n scope: { setTimeout: typeof setTimeout } = globalThis\n): T {\n const original = scope.setTimeout;\n scope.setTimeout = ((...args: Parameters<typeof setTimeout>) => {\n const id: unknown = original(...args);\n return typeof id === 'number' ? unrefable(id) : id;\n }) as typeof setTimeout;\n try {\n return run();\n } finally {\n scope.setTimeout = original;\n }\n}\n\nclass PortConnection implements ByteConnection {\n closed = false;\n readonly #endpoint: PortEndpoint;\n readonly #ended: () => void;\n\n constructor(endpoint: PortEndpoint, ended: () => void) {\n this.#endpoint = endpoint;\n this.#ended = ended;\n }\n\n async send(chunk: Uint8Array): Promise<void> {\n if (this.closed) throw new Error('connection is closed');\n post(this.#endpoint, chunk);\n }\n\n close(finalChunk?: Uint8Array): void {\n if (this.closed) return;\n if (finalChunk) post(this.#endpoint, finalChunk);\n this.closed = true;\n this.#endpoint.postMessage({ close: true } satisfies Frame);\n this.#endpoint.close?.();\n this.#ended();\n }\n\n end(): void {\n this.closed = true;\n this.#ended();\n }\n}\n\nexport class PortListener implements ServerListener {\n readonly #serverId: string;\n readonly #connections = new Set<PortConnection>();\n #accept: ByteConnectionAcceptor | undefined;\n\n constructor(serverId: string) {\n this.#serverId = serverId;\n }\n\n async start(accept: ByteConnectionAcceptor): Promise<void> {\n this.#accept = accept;\n }\n\n async close(): Promise<void> {\n for (const connection of [...this.#connections]) connection.close();\n this.#accept = undefined;\n }\n\n connect(endpoint: PortEndpoint): void {\n const accept = this.#accept;\n if (!accept) throw new Error('agent host is not listening');\n const connection = new PortConnection(endpoint, () => this.#connections.delete(connection));\n this.#connections.add(connection);\n const handler: ByteConnectionHandler = withUnrefTimers(() => accept(connection));\n endpoint.addEventListener('message', ({ data }) => {\n if (data instanceof Uint8Array) handler.onData(data);\n else if ((data as { close?: boolean } | null)?.close) {\n connection.end();\n handler.onClose();\n }\n });\n const failed = (event: Event) => {\n connection.end();\n handler.onError(new Error(`agent port ${event.type}`));\n };\n endpoint.addEventListener('messageerror', failed);\n endpoint.addEventListener('close', () => {\n if (connection.closed) return;\n connection.end();\n handler.onClose();\n });\n endpoint.start?.();\n endpoint.postMessage({ hello: this.#serverId } satisfies Frame);\n }\n}\n\nexport class PortChannel {\n readonly hello: Promise<string>;\n readonly #endpoint: PortEndpoint;\n readonly #queue: Uint8Array[] = [];\n #handlers: ByteTransportHandlers | undefined;\n #ended: 'close' | Error | undefined;\n #greet!: (serverId: string) => void;\n #refuse!: (error: Error) => void;\n\n constructor(endpoint: PortEndpoint) {\n this.#endpoint = endpoint;\n this.hello = new Promise((greet, refuse) => {\n this.#greet = greet;\n this.#refuse = refuse;\n });\n endpoint.addEventListener('message', ({ data }) => this.#receive(data as Frame));\n endpoint.addEventListener('messageerror', (event) =>\n this.#end(new Error(`agent port ${event.type}`))\n );\n endpoint.addEventListener('error', (event) => this.#end(new Error(`agent port ${event.type}`)));\n endpoint.addEventListener('close', () => this.#end('close'));\n endpoint.start?.();\n }\n\n readonly transport: ByteTransportFactory = (handlers) => {\n this.#handlers = handlers;\n for (const chunk of this.#queue.splice(0)) handlers.onData(chunk);\n if (this.#ended) this.#deliver(this.#ended);\n return {\n send: async (chunk) => {\n if (this.#ended) throw new Error('agent port is closed');\n post(this.#endpoint, chunk);\n },\n close: () => {\n if (this.#ended) return;\n this.#ended = 'close';\n this.#refuse(new Error('agent port closed before hello'));\n this.#endpoint.postMessage({ close: true } satisfies Frame);\n this.#endpoint.close?.();\n },\n };\n };\n\n #receive(frame: Frame): void {\n if (frame instanceof Uint8Array) {\n if (this.#handlers) this.#handlers.onData(frame);\n else this.#queue.push(frame);\n } else if ('hello' in frame) {\n this.#greet(frame.hello);\n } else {\n this.#end('close');\n }\n }\n\n #end(reason: 'close' | Error): void {\n if (this.#ended) return;\n this.#ended = reason;\n this.#refuse(reason === 'close' ? new Error('agent port closed before hello') : reason);\n this.#deliver(reason);\n }\n\n #deliver(reason: 'close' | Error): void {\n const handlers = this.#handlers;\n if (!handlers) return;\n this.#handlers = undefined;\n if (reason === 'close') handlers.onClose();\n else handlers.onError(reason);\n }\n}\n"],
|
|
5
|
+
"mappings": "AAoBA,SAAS,KAAK,UAAwB,OAAyB;AAC7D,QAAM,OAAO,MAAM,MAAM;AACzB,WAAS,YAAY,MAAM,CAAC,KAAK,MAAM,CAAC;AAC1C;AAEA,SAAS,UAAU,IAAoB;AACrC,SAAO;AAAA,IACL,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,IACA,MAAM;AACJ,aAAO;AAAA,IACT;AAAA,IACA,CAAC,OAAO,WAAW,GAAG,MAAM;AAAA,EAC9B;AACF;AAEO,SAAS,gBACd,KACA,QAA2C,YACxC;AACH,QAAM,WAAW,MAAM;AACvB,QAAM,cAAc,IAAI,SAAwC;AAC9D,UAAM,KAAc,SAAS,GAAG,IAAI;AACpC,WAAO,OAAO,OAAO,WAAW,UAAU,EAAE,IAAI;AAAA,EAClD;AACA,MAAI;AACF,WAAO,IAAI;AAAA,EACb,UAAE;AACA,UAAM,aAAa;AAAA,EACrB;AACF;AAEA,MAAM,eAAyC;AAAA,EAC7C,SAAS;AAAA,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rB,YAAI,KAAK,OAAQ,OAAM,IAAI,MAAM,sBAAsB;AACvD,aAAK,KAAK,WAAW,KAAK;AAAA,MAC5B;AAAA,MACA,OAAO,MAAM;AACX,YAAI,KAAK,OAAQ;AACjB,aAAK,SAAS;AACd,aAAK,QAAQ,IAAI,MAAM,gCAAgC,CAAC;AACxD,aAAK,UAAU,YAAY,EAAE,OAAO,KAAK,CAAiB;AAC1D,aAAK,UAAU,QAAQ;AAAA,MACzB;AAAA,IACF;AAAA,EACF;AAAA,EAEA,SAAS,OAAoB;AAC3B,QAAI,iBAAiB,YAAY;AAC/B,UAAI,KAAK,UAAW,MAAK,UAAU,OAAO,KAAK;AAAA,UAC1C,MAAK,OAAO,KAAK,KAAK;AAAA,IAC7B,WAAW,WAAW,OAAO;AAC3B,WAAK,OAAO,MAAM,KAAK;AAAA,IACzB,OAAO;AACL,WAAK,KAAK,OAAO;AAAA,IACnB;AAAA,EACF;AAAA,EAEA,KAAK,QAA+B;AAClC,QAAI,KAAK,OAAQ;AACjB,SAAK,SAAS;AACd,SAAK,QAAQ,WAAW,UAAU,IAAI,MAAM,gCAAgC,IAAI,MAAM;AACtF,SAAK,SAAS,MAAM;AAAA,EACtB;AAAA,EAEA,SAAS,QAA+B;AACtC,UAAM,WAAW,KAAK;AACtB,QAAI,CAAC,SAAU;AACf,SAAK,YAAY;AACjB,QAAI,WAAW,QAAS,UAAS,QAAQ;AAAA,QACpC,UAAS,QAAQ,MAAM;AAAA,EAC9B;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/worker.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function serveConnections(scope, host) {
|
|
2
|
+
scope.addEventListener("message", ({ data }) => {
|
|
3
|
+
const port = data?.connect;
|
|
4
|
+
if (!port) return;
|
|
5
|
+
host.then(
|
|
6
|
+
(ready) => ready.connect(port),
|
|
7
|
+
() => port.close?.()
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
serveConnections
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/worker.ts"],
|
|
4
|
+
"sourcesContent": ["import type { AgentHost } from './host.ts';\nimport type { PortEndpoint } from './wire.ts';\n\nexport interface WorkerScope {\n addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;\n}\n\nexport function serveConnections(scope: WorkerScope, host: Promise<AgentHost>): void {\n scope.addEventListener('message', ({ data }) => {\n const port = (data as { connect?: PortEndpoint } | null)?.connect;\n if (!port) return;\n host.then(\n (ready) => ready.connect(port),\n () => port.close?.()\n );\n });\n}\n"],
|
|
5
|
+
"mappings": "AAOO,SAAS,iBAAiB,OAAoB,MAAgC;AACnF,QAAM,iBAAiB,WAAW,CAAC,EAAE,KAAK,MAAM;AAC9C,UAAM,OAAQ,MAA4C;AAC1D,QAAI,CAAC,KAAM;AACX,SAAK;AAAA,MACH,CAAC,UAAU,MAAM,QAAQ,IAAI;AAAA,MAC7B,MAAM,KAAK,QAAQ;AAAA,IACrB;AAAA,EACF,CAAC;AACH;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-ecoverse/slicc-agent",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "SLICC's agent core on pi durable: an agent worker, session storage, remoting and pi extensions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
".": {
|
|
26
26
|
"types": "./dist/types/index.d.ts",
|
|
27
27
|
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./spectrum": {
|
|
30
|
+
"types": "./dist/types/spectrum/index.d.ts",
|
|
31
|
+
"default": "./dist/spectrum/index.js"
|
|
28
32
|
}
|
|
29
33
|
},
|
|
30
34
|
"files": [
|
|
@@ -47,6 +51,7 @@
|
|
|
47
51
|
},
|
|
48
52
|
"devDependencies": {
|
|
49
53
|
"@ai-ecoverse/slicc-shared-web": "1.5.0",
|
|
54
|
+
"@ai-ecoverse/slicc-spectrum": "1.7.0",
|
|
50
55
|
"@biomejs/biome": "2.5.15",
|
|
51
56
|
"@types/node": "24.19.1",
|
|
52
57
|
"esbuild": "0.28.2",
|
|
@@ -59,7 +64,13 @@
|
|
|
59
64
|
"dependencies": {
|
|
60
65
|
"@earendil-works/chord": "1.0.4",
|
|
61
66
|
"@earendil-works/pi-ai": "1.0.4",
|
|
67
|
+
"@earendil-works/pi-client": "1.0.4",
|
|
62
68
|
"@earendil-works/pi-durable": "1.0.4",
|
|
69
|
+
"@earendil-works/pi-protocol": "1.0.4",
|
|
70
|
+
"@earendil-works/pi-server": "1.0.4",
|
|
63
71
|
"@sqlite.org/sqlite-wasm": "3.53.4-build2"
|
|
72
|
+
},
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"@ai-ecoverse/slicc-spectrum": "^1.7.0"
|
|
64
75
|
}
|
|
65
76
|
}
|
package/dist/port.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
function isRequest(data) {
|
|
2
|
-
const value = data;
|
|
3
|
-
return typeof value?.id === "number" && typeof value.prompt === "string";
|
|
4
|
-
}
|
|
5
|
-
function reason(error) {
|
|
6
|
-
return error instanceof Error ? error.message : String(error);
|
|
7
|
-
}
|
|
8
|
-
function serveAgent(endpoint, agent) {
|
|
9
|
-
endpoint.addEventListener("message", ({ data }) => {
|
|
10
|
-
if (!isRequest(data)) return;
|
|
11
|
-
agent.then((ready) => ready.prompt(data.prompt)).then(
|
|
12
|
-
(text) => endpoint.postMessage({ id: data.id, text }),
|
|
13
|
-
(error) => endpoint.postMessage({ id: data.id, error: reason(error) })
|
|
14
|
-
);
|
|
15
|
-
});
|
|
16
|
-
endpoint.start?.();
|
|
17
|
-
}
|
|
18
|
-
function failure(event) {
|
|
19
|
-
const message = event.message;
|
|
20
|
-
return new Error(message ? `agent failed: ${message}` : `agent failed: ${event.type}`);
|
|
21
|
-
}
|
|
22
|
-
function connectAgent(endpoint) {
|
|
23
|
-
const pending = /* @__PURE__ */ new Map();
|
|
24
|
-
let broken;
|
|
25
|
-
let next = 0;
|
|
26
|
-
const fail = (event) => {
|
|
27
|
-
broken = failure(event);
|
|
28
|
-
for (const call of pending.values()) call.reject(broken);
|
|
29
|
-
pending.clear();
|
|
30
|
-
};
|
|
31
|
-
endpoint.addEventListener("message", ({ data }) => {
|
|
32
|
-
const reply = data;
|
|
33
|
-
const call = pending.get(reply.id);
|
|
34
|
-
if (!call) return;
|
|
35
|
-
pending.delete(reply.id);
|
|
36
|
-
if ("error" in reply) call.reject(new Error(reply.error));
|
|
37
|
-
else call.resolve(reply.text);
|
|
38
|
-
});
|
|
39
|
-
endpoint.addEventListener("error", fail);
|
|
40
|
-
endpoint.addEventListener("messageerror", fail);
|
|
41
|
-
endpoint.start?.();
|
|
42
|
-
return {
|
|
43
|
-
prompt(text) {
|
|
44
|
-
if (broken) return Promise.reject(broken);
|
|
45
|
-
const id = ++next;
|
|
46
|
-
return new Promise((resolve, reject) => {
|
|
47
|
-
pending.set(id, { resolve, reject });
|
|
48
|
-
endpoint.postMessage({ id, prompt: text });
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
export {
|
|
54
|
-
connectAgent,
|
|
55
|
-
serveAgent
|
|
56
|
-
};
|
|
57
|
-
//# sourceMappingURL=port.js.map
|
package/dist/port.js.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/port.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Agent } from './agent.ts';\n\nexport interface AgentRequest {\n id: number;\n prompt: string;\n}\n\nexport type AgentReply = { id: number; text: string } | { id: number; error: string };\n\nexport interface MessageEndpoint {\n postMessage(message: unknown): void;\n addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;\n addEventListener(type: 'error' | 'messageerror', listener: (event: Event) => void): void;\n start?(): void;\n}\n\nfunction isRequest(data: unknown): data is AgentRequest {\n const value = data as Partial<AgentRequest> | null;\n return typeof value?.id === 'number' && typeof value.prompt === 'string';\n}\n\nfunction reason(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\nexport function serveAgent(endpoint: MessageEndpoint, agent: Promise<Agent>): void {\n endpoint.addEventListener('message', ({ data }) => {\n if (!isRequest(data)) return;\n agent\n .then((ready) => ready.prompt(data.prompt))\n .then(\n (text) => endpoint.postMessage({ id: data.id, text } satisfies AgentReply),\n (error: unknown) =>\n endpoint.postMessage({ id: data.id, error: reason(error) } satisfies AgentReply)\n );\n });\n endpoint.start?.();\n}\n\nexport interface AgentClient {\n prompt(text: string): Promise<string>;\n}\n\nfunction failure(event: Event): Error {\n const message = (event as ErrorEvent).message;\n return new Error(message ? `agent failed: ${message}` : `agent failed: ${event.type}`);\n}\n\nexport function connectAgent(endpoint: MessageEndpoint): AgentClient {\n const pending = new Map<number, { resolve(text: string): void; reject(error: Error): void }>();\n let broken: Error | undefined;\n let next = 0;\n const fail = (event: Event) => {\n broken = failure(event);\n for (const call of pending.values()) call.reject(broken);\n pending.clear();\n };\n endpoint.addEventListener('message', ({ data }) => {\n const reply = data as AgentReply;\n const call = pending.get(reply.id);\n if (!call) return;\n pending.delete(reply.id);\n if ('error' in reply) call.reject(new Error(reply.error));\n else call.resolve(reply.text);\n });\n endpoint.addEventListener('error', fail);\n endpoint.addEventListener('messageerror', fail);\n endpoint.start?.();\n return {\n prompt(text) {\n if (broken) return Promise.reject(broken);\n const id = ++next;\n return new Promise((resolve, reject) => {\n pending.set(id, { resolve, reject });\n endpoint.postMessage({ id, prompt: text } satisfies AgentRequest);\n });\n },\n };\n}\n"],
|
|
5
|
-
"mappings": "AAgB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B,CAAC;AACD,WAAS,iBAAiB,SAAS,IAAI;AACvC,WAAS,iBAAiB,gBAAgB,IAAI;AAC9C,WAAS,QAAQ;AACjB,SAAO;AAAA,IACL,OAAO,MAAM;AACX,UAAI,OAAQ,QAAO,QAAQ,OAAO,MAAM;AACxC,YAAM,KAAK,EAAE;AACb,aAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,gBAAQ,IAAI,IAAI,EAAE,SAAS,OAAO,CAAC;AACnC,iBAAS,YAAY,EAAE,IAAI,QAAQ,KAAK,CAAwB;AAAA,MAClE,CAAC;AAAA,IACH;AAAA,EACF;AACF;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
package/dist/types/port.d.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Agent } from './agent.ts';
|
|
2
|
-
export interface AgentRequest {
|
|
3
|
-
id: number;
|
|
4
|
-
prompt: string;
|
|
5
|
-
}
|
|
6
|
-
export type AgentReply = {
|
|
7
|
-
id: number;
|
|
8
|
-
text: string;
|
|
9
|
-
} | {
|
|
10
|
-
id: number;
|
|
11
|
-
error: string;
|
|
12
|
-
};
|
|
13
|
-
export interface MessageEndpoint {
|
|
14
|
-
postMessage(message: unknown): void;
|
|
15
|
-
addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
|
|
16
|
-
addEventListener(type: 'error' | 'messageerror', listener: (event: Event) => void): void;
|
|
17
|
-
start?(): void;
|
|
18
|
-
}
|
|
19
|
-
export declare function serveAgent(endpoint: MessageEndpoint, agent: Promise<Agent>): void;
|
|
20
|
-
export interface AgentClient {
|
|
21
|
-
prompt(text: string): Promise<string>;
|
|
22
|
-
}
|
|
23
|
-
export declare function connectAgent(endpoint: MessageEndpoint): AgentClient;
|