@matrix-ai/sdk 1.6.0 → 1.6.2

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/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from "./gen/types.gen.js";
2
2
  import { type Config } from "./gen/client/types.gen.js";
3
- import { OpencodeClient } from "./gen/sdk.gen.js";
4
- export { type Config as OpencodeClientConfig, OpencodeClient };
5
- export declare function createOpencodeClient(config?: Config & {
3
+ import { MatrixClient } from "./gen/sdk.gen.js";
4
+ export { type Config as MatrixClientConfig, MatrixClient };
5
+ export declare function createMatrixClient(config?: Config & {
6
6
  directory?: string;
7
- }): OpencodeClient;
7
+ }): MatrixClient;
package/dist/client.js CHANGED
@@ -1,8 +1,33 @@
1
1
  export * from "./gen/types.gen.js";
2
2
  import { createClient } from "./gen/client/client.gen.js";
3
- import { OpencodeClient } from "./gen/sdk.gen.js";
4
- export { OpencodeClient };
5
- export function createOpencodeClient(config) {
3
+ import { MatrixClient } from "./gen/sdk.gen.js";
4
+ export { MatrixClient };
5
+ function pick(value, fallback) {
6
+ if (!value)
7
+ return;
8
+ if (!fallback)
9
+ return value;
10
+ if (value === fallback)
11
+ return fallback;
12
+ if (value === encodeURIComponent(fallback))
13
+ return fallback;
14
+ return value;
15
+ }
16
+ function rewrite(request, directory) {
17
+ if (request.method !== "GET" && request.method !== "HEAD")
18
+ return request;
19
+ const value = pick(request.headers.get("x-matrix-directory"), directory);
20
+ if (!value)
21
+ return request;
22
+ const url = new URL(request.url);
23
+ if (!url.searchParams.has("directory")) {
24
+ url.searchParams.set("directory", value);
25
+ }
26
+ const next = new Request(url, request);
27
+ next.headers.delete("x-matrix-directory");
28
+ return next;
29
+ }
30
+ export function createMatrixClient(config) {
6
31
  if (!config?.fetch) {
7
32
  const customFetch = (req) => {
8
33
  // @ts-ignore
@@ -17,9 +42,10 @@ export function createOpencodeClient(config) {
17
42
  if (config?.directory) {
18
43
  config.headers = {
19
44
  ...config.headers,
20
- "x-opencode-directory": encodeURIComponent(config.directory),
45
+ "x-matrix-directory": encodeURIComponent(config.directory),
21
46
  };
22
47
  }
23
48
  const client = createClient(config);
24
- return new OpencodeClient({ client });
49
+ client.interceptors.request.use((request) => rewrite(request, config?.directory));
50
+ return new MatrixClient({ client });
25
51
  }
@@ -374,7 +374,7 @@ declare class Event extends _HeyApiClient {
374
374
  */
375
375
  subscribe<ThrowOnError extends boolean = false>(options?: Options<EventSubscribeData, ThrowOnError>): Promise<import("./core/serverSentEvents.gen.js").ServerSentEventsResult<EventSubscribeResponses, unknown>>;
376
376
  }
377
- export declare class OpencodeClient extends _HeyApiClient {
377
+ export declare class MatrixClient extends _HeyApiClient {
378
378
  /**
379
379
  * Respond to a permission request
380
380
  */
@@ -844,7 +844,7 @@ class Event extends _HeyApiClient {
844
844
  });
845
845
  }
846
846
  }
847
- export class OpencodeClient extends _HeyApiClient {
847
+ export class MatrixClient extends _HeyApiClient {
848
848
  /**
849
849
  * Respond to a permission request
850
850
  */
@@ -1050,7 +1050,7 @@ export type Config = {
1050
1050
  diff_style?: "auto" | "stacked";
1051
1051
  };
1052
1052
  /**
1053
- * Command configuration, see https://opencode.ai/docs/commands
1053
+ * Command configuration, see https://y-square-t3.github.io/matrix/docs/commands
1054
1054
  */
1055
1055
  command?: {
1056
1056
  [key: string]: {
@@ -1107,7 +1107,7 @@ export type Config = {
1107
1107
  [key: string]: AgentConfig | undefined;
1108
1108
  };
1109
1109
  /**
1110
- * Agent configuration, see https://opencode.ai/docs/agent
1110
+ * Agent configuration, see https://y-square-t3.github.io/matrix/docs/agent
1111
1111
  */
1112
1112
  agent?: {
1113
1113
  plan?: AgentConfig;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from "./client.js";
2
2
  export * from "./server.js";
3
3
  import type { ServerOptions } from "./server.js";
4
- export declare function createOpencode(options?: ServerOptions): Promise<{
5
- client: import("./client.js").OpencodeClient;
4
+ export declare function createMatrix(options?: ServerOptions): Promise<{
5
+ client: import("./client.js").MatrixClient;
6
6
  server: {
7
7
  url: string;
8
8
  close(): void;
package/dist/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  export * from "./client.js";
2
2
  export * from "./server.js";
3
- import { createOpencodeClient } from "./client.js";
4
- import { createOpencodeServer } from "./server.js";
5
- export async function createOpencode(options) {
6
- const server = await createOpencodeServer({
3
+ import { createMatrixClient } from "./client.js";
4
+ import { createMatrixServer } from "./server.js";
5
+ export async function createMatrix(options) {
6
+ const server = await createMatrixServer({
7
7
  ...options,
8
8
  });
9
- const client = createOpencodeClient({
9
+ const client = createMatrixClient({
10
10
  baseUrl: server.url,
11
11
  });
12
12
  return {
@@ -0,0 +1,3 @@
1
+ import { type ChildProcess } from "node:child_process";
2
+ export declare function stop(proc: ChildProcess): void;
3
+ export declare function bindAbort(proc: ChildProcess, signal?: AbortSignal, onAbort?: () => void): () => void;
@@ -0,0 +1,33 @@
1
+ import { spawnSync } from "node:child_process";
2
+ // Duplicated from `packages/matrix/src/util/process.ts` because the SDK cannot
3
+ // import `matrix` without creating a cycle (`matrix` depends on `@matrix-ai/sdk`).
4
+ export function stop(proc) {
5
+ if (proc.exitCode !== null || proc.signalCode !== null)
6
+ return;
7
+ if (process.platform === "win32" && proc.pid) {
8
+ const out = spawnSync("taskkill", ["/pid", String(proc.pid), "/T", "/F"], { windowsHide: true });
9
+ if (!out.error && out.status === 0)
10
+ return;
11
+ }
12
+ proc.kill();
13
+ }
14
+ export function bindAbort(proc, signal, onAbort) {
15
+ if (!signal)
16
+ return () => { };
17
+ const abort = () => {
18
+ clear();
19
+ stop(proc);
20
+ onAbort?.();
21
+ };
22
+ const clear = () => {
23
+ signal.removeEventListener("abort", abort);
24
+ proc.off("exit", clear);
25
+ proc.off("error", clear);
26
+ };
27
+ signal.addEventListener("abort", abort, { once: true });
28
+ proc.on("exit", clear);
29
+ proc.on("error", clear);
30
+ if (signal.aborted)
31
+ abort();
32
+ return clear;
33
+ }
package/dist/server.d.ts CHANGED
@@ -14,10 +14,10 @@ export type TuiOptions = {
14
14
  signal?: AbortSignal;
15
15
  config?: Config;
16
16
  };
17
- export declare function createOpencodeServer(options?: ServerOptions): Promise<{
17
+ export declare function createMatrixServer(options?: ServerOptions): Promise<{
18
18
  url: string;
19
19
  close(): void;
20
20
  }>;
21
- export declare function createOpencodeTui(options?: TuiOptions): {
21
+ export declare function createMatrixTui(options?: TuiOptions): {
22
22
  close(): void;
23
23
  };
package/dist/server.js CHANGED
@@ -1,5 +1,6 @@
1
- import { spawn } from "node:child_process";
2
- export async function createOpencodeServer(options) {
1
+ import launch from "cross-spawn";
2
+ import { stop, bindAbort } from "./process.js";
3
+ export async function createMatrixServer(options) {
3
4
  options = Object.assign({
4
5
  hostname: "127.0.0.1",
5
6
  port: 4096,
@@ -8,28 +9,38 @@ export async function createOpencodeServer(options) {
8
9
  const args = [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`];
9
10
  if (options.config?.logLevel)
10
11
  args.push(`--log-level=${options.config.logLevel}`);
11
- const proc = spawn(`opencode`, args, {
12
- signal: options.signal,
12
+ const proc = launch(`matrix`, args, {
13
13
  env: {
14
14
  ...process.env,
15
- OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}),
15
+ MATRIX_CONFIG_CONTENT: JSON.stringify(options.config ?? {}),
16
16
  },
17
17
  });
18
+ let clear = () => { };
18
19
  const url = await new Promise((resolve, reject) => {
19
20
  const id = setTimeout(() => {
21
+ clear();
22
+ stop(proc);
20
23
  reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`));
21
24
  }, options.timeout);
22
25
  let output = "";
26
+ let resolved = false;
23
27
  proc.stdout?.on("data", (chunk) => {
28
+ if (resolved)
29
+ return;
24
30
  output += chunk.toString();
25
31
  const lines = output.split("\n");
26
32
  for (const line of lines) {
27
- if (line.startsWith("opencode server listening")) {
33
+ if (line.startsWith("matrix server listening")) {
28
34
  const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
29
35
  if (!match) {
30
- throw new Error(`Failed to parse server url from output: ${line}`);
36
+ clear();
37
+ stop(proc);
38
+ clearTimeout(id);
39
+ reject(new Error(`Failed to parse server url from output: ${line}`));
40
+ return;
31
41
  }
32
42
  clearTimeout(id);
43
+ resolved = true;
33
44
  resolve(match[1]);
34
45
  return;
35
46
  }
@@ -50,21 +61,20 @@ export async function createOpencodeServer(options) {
50
61
  clearTimeout(id);
51
62
  reject(error);
52
63
  });
53
- if (options.signal) {
54
- options.signal.addEventListener("abort", () => {
55
- clearTimeout(id);
56
- reject(new Error("Aborted"));
57
- });
58
- }
64
+ clear = bindAbort(proc, options.signal, () => {
65
+ clearTimeout(id);
66
+ reject(options.signal?.reason);
67
+ });
59
68
  });
60
69
  return {
61
70
  url,
62
71
  close() {
63
- proc.kill();
72
+ clear();
73
+ stop(proc);
64
74
  },
65
75
  };
66
76
  }
67
- export function createOpencodeTui(options) {
77
+ export function createMatrixTui(options) {
68
78
  const args = [];
69
79
  if (options?.project) {
70
80
  args.push(`--project=${options.project}`);
@@ -78,17 +88,18 @@ export function createOpencodeTui(options) {
78
88
  if (options?.agent) {
79
89
  args.push(`--agent=${options.agent}`);
80
90
  }
81
- const proc = spawn(`opencode`, args, {
82
- signal: options?.signal,
91
+ const proc = launch(`matrix`, args, {
83
92
  stdio: "inherit",
84
93
  env: {
85
94
  ...process.env,
86
- OPENCODE_CONFIG_CONTENT: JSON.stringify(options?.config ?? {}),
95
+ MATRIX_CONFIG_CONTENT: JSON.stringify(options?.config ?? {}),
87
96
  },
88
97
  });
98
+ const clear = bindAbort(proc, options?.signal);
89
99
  return {
90
100
  close() {
91
- proc.kill();
101
+ clear();
102
+ stop(proc);
92
103
  },
93
104
  };
94
105
  }
@@ -1,8 +1,8 @@
1
1
  export * from "./gen/types.gen.js";
2
2
  import { type Config } from "./gen/client/types.gen.js";
3
- import { OpencodeClient } from "./gen/sdk.gen.js";
4
- export { type Config as OpencodeClientConfig, OpencodeClient };
5
- export declare function createOpencodeClient(config?: Config & {
3
+ import { MatrixClient } from "./gen/sdk.gen.js";
4
+ export { type Config as MatrixClientConfig, MatrixClient };
5
+ export declare function createMatrixClient(config?: Config & {
6
6
  directory?: string;
7
7
  experimental_workspaceID?: string;
8
- }): OpencodeClient;
8
+ }): MatrixClient;
package/dist/v2/client.js CHANGED
@@ -1,8 +1,43 @@
1
1
  export * from "./gen/types.gen.js";
2
2
  import { createClient } from "./gen/client/client.gen.js";
3
- import { OpencodeClient } from "./gen/sdk.gen.js";
4
- export { OpencodeClient };
5
- export function createOpencodeClient(config) {
3
+ import { MatrixClient } from "./gen/sdk.gen.js";
4
+ export { MatrixClient };
5
+ function pick(value, fallback, encode) {
6
+ if (!value)
7
+ return;
8
+ if (!fallback)
9
+ return value;
10
+ if (value === fallback)
11
+ return fallback;
12
+ if (encode && value === encode(fallback))
13
+ return fallback;
14
+ return value;
15
+ }
16
+ function rewrite(request, values) {
17
+ if (request.method !== "GET" && request.method !== "HEAD")
18
+ return request;
19
+ const url = new URL(request.url);
20
+ let changed = false;
21
+ for (const [name, key] of [
22
+ ["x-matrix-directory", "directory"],
23
+ ["x-matrix-workspace", "workspace"],
24
+ ]) {
25
+ const value = pick(request.headers.get(name), key === "directory" ? values.directory : values.workspace, key === "directory" ? encodeURIComponent : undefined);
26
+ if (!value)
27
+ continue;
28
+ if (!url.searchParams.has(key)) {
29
+ url.searchParams.set(key, value);
30
+ }
31
+ changed = true;
32
+ }
33
+ if (!changed)
34
+ return request;
35
+ const next = new Request(url, request);
36
+ next.headers.delete("x-matrix-directory");
37
+ next.headers.delete("x-matrix-workspace");
38
+ return next;
39
+ }
40
+ export function createMatrixClient(config) {
6
41
  if (!config?.fetch) {
7
42
  const customFetch = (req) => {
8
43
  // @ts-ignore
@@ -15,19 +50,21 @@ export function createOpencodeClient(config) {
15
50
  };
16
51
  }
17
52
  if (config?.directory) {
18
- const isNonASCII = /[^\x00-\x7F]/.test(config.directory);
19
- const encodedDirectory = isNonASCII ? encodeURIComponent(config.directory) : config.directory;
20
53
  config.headers = {
21
54
  ...config.headers,
22
- "x-opencode-directory": encodedDirectory,
55
+ "x-matrix-directory": encodeURIComponent(config.directory),
23
56
  };
24
57
  }
25
58
  if (config?.experimental_workspaceID) {
26
59
  config.headers = {
27
60
  ...config.headers,
28
- "x-opencode-workspace": config.experimental_workspaceID,
61
+ "x-matrix-workspace": config.experimental_workspaceID,
29
62
  };
30
63
  }
31
64
  const client = createClient(config);
32
- return new OpencodeClient({ client });
65
+ client.interceptors.request.use((request) => rewrite(request, {
66
+ directory: config?.directory,
67
+ workspace: config?.experimental_workspaceID,
68
+ }));
69
+ return new MatrixClient({ client });
33
70
  }