pi-msgpack-rpc 0.1.0 → 0.1.1

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.
Files changed (3) hide show
  1. package/index.ts +2 -32
  2. package/package.json +1 -1
  3. package/rpc.ts +38 -0
package/index.ts CHANGED
@@ -2,16 +2,9 @@
2
2
  import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
3
3
  import { Type } from "typebox";
4
4
 
5
- import * as net from 'node:net';
6
- import { pipeline } from 'node:stream';
7
- import { pack, UnpackrStream } from 'msgpackr';
8
-
5
+ import { msgpackRpcCall } from "./rpc";
9
6
 
10
7
  export default function (pi: ExtensionAPI) {
11
- pi.on("session_start", async (_event, ctx) => {
12
- ctx.ui.notify("pi-msgpack-rpc extension loaded!", "info");
13
- });
14
-
15
8
  pi.registerTool({
16
9
  name: "msgpack_rpc_call",
17
10
  label: "MessagePack RPC Call",
@@ -37,30 +30,7 @@ export default function (pi: ExtensionAPI) {
37
30
 
38
31
  ctx.ui.notify(`[RPC] Starting call to ${server} → ${method}`, "info");
39
32
 
40
- const unpackStream = new UnpackrStream();
41
-
42
- const result = await new Promise((resolve, reject) => {
43
- const client = net.createConnection(server, () => {
44
- ctx.ui.notify(`[RPC] Connected to Neovim ${server}, calling ${method}`, "info");
45
- client.write(pack([0, Date.now() % 1000000, method, args]));
46
- })
47
-
48
- pipeline(client, unpackStream, (err) => {
49
- if (err) ctx.ui.notify(`[RPC] Pipeline error: ${err.message}`, "error");
50
- });
51
-
52
- unpackStream.on('data', (msg: any) => {
53
- ctx.ui.notify(`[RPC] Received response`, "success");
54
- client.end();
55
-
56
- if (Array.isArray(msg) && msg[0] === 1) {
57
- const error = msg[2]; const result = msg[3];
58
- error !== null && error !== undefined ? reject(error) : resolve(result);
59
- } else {
60
- resolve(msg);
61
- }
62
- });
63
- });
33
+ const result = await msgpackRpcCall(ctx, server, method, args, timeout);
64
34
 
65
35
  return {
66
36
  content: [{ type: "text", text: result }],
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pi-msgpack-rpc",
3
3
  "description": "A MessagePack RPC tool for Pi",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/paperlib/pi-msgpack-rpc.git"
package/rpc.ts ADDED
@@ -0,0 +1,38 @@
1
+
2
+ import * as net from 'node:net';
3
+ import { pipeline } from 'node:stream';
4
+ import { pack, UnpackrStream } from 'msgpackr';
5
+
6
+ export async function msgpackRpcCall(
7
+ ctx: any,
8
+ server: string,
9
+ method: string,
10
+ args : any[] = [],
11
+ timeout = 5000
12
+ ) {
13
+ return new Promise((resolve, reject) => {
14
+ const unpackStream = new UnpackrStream();
15
+
16
+ const client = net.createConnection(server, () => {
17
+ ctx.ui.notify(`[RPC] Connected to ${server}, calling ${method}`, "info");
18
+ client.write(pack([0, Date.now() % 1000000, method, args]));
19
+ })
20
+
21
+ pipeline(client, unpackStream, (err) => {
22
+ if (err) ctx.ui.notify(`[RPC] Pipeline error: ${err.message}`, "error");
23
+ });
24
+
25
+ unpackStream.on('data', (msg: any) => {
26
+ ctx.ui.notify(`[RPC] Received response`, "success");
27
+ client.end();
28
+
29
+ if (Array.isArray(msg) && msg[0] === 1) {
30
+ const error = msg[2]; const result = msg[3];
31
+ error !== null && error !== undefined ? reject(error) : resolve(result);
32
+ } else {
33
+ resolve(msg);
34
+ }
35
+ });
36
+ });
37
+ }
38
+