channelstack 0.0.0-stage → 0.2.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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -2
  3. package/bin/channelstack.mjs +31 -0
  4. package/dist/integrations/checks/checks.test.d.ts +1 -0
  5. package/dist/integrations/checks/checks.test.js +31 -0
  6. package/dist/integrations/checks/index.d.ts +102 -0
  7. package/dist/integrations/checks/index.js +1034 -0
  8. package/dist/integrations/matchers-core/index.d.ts +217 -0
  9. package/dist/integrations/matchers-core/index.js +799 -0
  10. package/dist/integrations/matchers-core/matchers.test.d.ts +1 -0
  11. package/dist/integrations/matchers-core/matchers.test.js +407 -0
  12. package/dist/integrations/vitest/global-setup.d.ts +3 -0
  13. package/dist/integrations/vitest/global-setup.js +19 -0
  14. package/dist/integrations/vitest/index.d.ts +82 -0
  15. package/dist/integrations/vitest/index.js +42 -0
  16. package/dist/integrations/vitest/plugin.test.d.ts +1 -0
  17. package/dist/integrations/vitest/plugin.test.js +19 -0
  18. package/dist/integrations/vitest/refused.d.ts +22 -0
  19. package/dist/integrations/vitest/refused.js +49 -0
  20. package/dist/integrations/vitest/setup.d.ts +1 -0
  21. package/dist/integrations/vitest/setup.js +139 -0
  22. package/dist/sdks/typescript/client.d.ts +14 -0
  23. package/dist/sdks/typescript/client.js +11 -0
  24. package/dist/sdks/typescript/generated/channels.d.ts +32 -0
  25. package/dist/sdks/typescript/generated/channels.js +4 -0
  26. package/dist/sdks/typescript/generated/index.d.ts +4178 -0
  27. package/dist/sdks/typescript/generated/index.js +4608 -0
  28. package/dist/sdks/typescript/generated/normalisation.d.ts +18 -0
  29. package/dist/sdks/typescript/generated/normalisation.js +14 -0
  30. package/dist/sdks/typescript/runtime/client.d.ts +56 -0
  31. package/dist/sdks/typescript/runtime/client.js +74 -0
  32. package/dist/sdks/typescript/runtime/credentials.d.ts +7 -0
  33. package/dist/sdks/typescript/runtime/credentials.js +20 -0
  34. package/dist/sdks/typescript/runtime/duration.d.ts +4 -0
  35. package/dist/sdks/typescript/runtime/duration.js +13 -0
  36. package/dist/sdks/typescript/runtime/files.d.ts +47 -0
  37. package/dist/sdks/typescript/runtime/files.js +53 -0
  38. package/dist/sdks/typescript/runtime/launcher.d.ts +27 -0
  39. package/dist/sdks/typescript/runtime/launcher.js +124 -0
  40. package/dist/sdks/typescript/runtime/responder.d.ts +26 -0
  41. package/dist/sdks/typescript/runtime/responder.js +42 -0
  42. package/dist/sdks/typescript/runtime/transport.d.ts +43 -0
  43. package/dist/sdks/typescript/runtime/transport.js +95 -0
  44. package/dist/sdks/typescript/runtime/wait.d.ts +96 -0
  45. package/dist/sdks/typescript/runtime/wait.js +159 -0
  46. package/dist/sdks/typescript/types.d.ts +82 -0
  47. package/dist/sdks/typescript/types.js +1 -0
  48. package/package.json +82 -4
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,139 @@
1
+ import { dirname } from "node:path";
2
+ import { expect, inject, test } from "vitest";
3
+ import { CHANNEL_NAMES, ChannelstackError, connect, onRefusal, PROVIDER_NAMES, setFilesRoot, setWaitDefaults, unreportedRefusals, untilQuiet, } from "../../sdks/typescript/client.js";
4
+ import { matchers } from "../matchers-core/index.js";
5
+ import { refusedFixture, SKIPS_LIVE, unskipped } from "./refused.js";
6
+ /** Runs in every test worker: one client per worker, one session per test, and the matchers. */
7
+ const server = inject("channelstack");
8
+ // The plugin's `timeout` and `quietFor` are every SDK wait's defaults, not only the matchers':
9
+ // `thread.waitForReply()`, `latestReply()`, `delivery.settled()`, …
10
+ setWaitDefaults({ timeout: server.timeout, quietFor: server.quietFor });
11
+ let client;
12
+ const connected = () => {
13
+ // The worker attaches to the server the global setup found; it never starts one itself. The
14
+ // token is handed over directly, so nothing a test spawns inherits it.
15
+ client ??= connect({ mode: "attach", server: server.url, token: server.token });
16
+ return client;
17
+ };
18
+ /**
19
+ * What live mode can't do skips the test, with the reason, instead of failing it: a fake-only
20
+ * feature (`unsupported_in_live`), or one that needs a piece this run doesn't have (`action_requires`:
21
+ * the browser actor, proxy mode). The fake run never sees these refusals, so a
22
+ * test still runs in full on every PR (reality-matrix.md, "What this means for test authors").
23
+ */
24
+ /** Each running test's `skip`, by its session: the refused request names the session, so concurrent tests skip only themselves. */
25
+ const skips = new Map();
26
+ onRefusal((error, path) => {
27
+ const session = /^\/sessions\/([^/]+)/.exec(path)?.[1];
28
+ if (session === undefined || unskipped.has(session))
29
+ return;
30
+ const skip = skips.get(session);
31
+ if (skip && SKIPS_LIVE.has(error.code))
32
+ skip(`live: ${error.message}`);
33
+ });
34
+ /**
35
+ * A channel's fixture. Every test gets all four, so a channel that is refused live (its live
36
+ * backend isn't built, its credentials are set in part) must not touch the tests of the others:
37
+ * the refusal is kept, and comes when the test first uses that channel. It skips the test or fails
38
+ * it, as the refusal would have.
39
+ */
40
+ function channelFixture(open) {
41
+ return async ({ channelstackSession: session, skip }, use) => {
42
+ let fixture;
43
+ // A refusal while setting up is kept for the test's first use of that channel.
44
+ unskipped.add(session.sessionId);
45
+ try {
46
+ fixture = await open(session);
47
+ }
48
+ catch (error) {
49
+ const refuse = () => {
50
+ if (error instanceof ChannelstackError && SKIPS_LIVE.has(error.code))
51
+ skip(`live: ${error.message}`);
52
+ throw error;
53
+ };
54
+ fixture = refusedFixture(refuse);
55
+ }
56
+ finally {
57
+ unskipped.delete(session.sessionId);
58
+ }
59
+ await use(fixture);
60
+ };
61
+ }
62
+ const auto = { auto: true };
63
+ /**
64
+ * A test fails when the fake had to stop a bot loop in its session (decision 0027): apps that
65
+ * answer each other without end are a bug, whether or not the test asserted anything. Where the
66
+ * server can't tell (live, without proxy mode), there is nothing to report.
67
+ */
68
+ async function failOnLoops(session) {
69
+ let loops;
70
+ try {
71
+ // Unreported: a refusal here must not turn the test into a skip with the wrong reason.
72
+ loops = (await unreportedRefusals(() => session.quiet())).loops;
73
+ }
74
+ catch (error) {
75
+ if (error instanceof ChannelstackError)
76
+ return;
77
+ throw error;
78
+ }
79
+ if (loops !== undefined && loops.length > 0)
80
+ throw new Error(`The fake stopped ${loops.join("; ")}`);
81
+ }
82
+ test.override({
83
+ channelstackSession: [
84
+ async ({ task, skip }, use) => {
85
+ const session = await (await connected()).openSession();
86
+ // A fixture path in a test (`files: "./fixtures/web"`) starts from the test file, like an import.
87
+ setFilesRoot(dirname(task.file.filepath));
88
+ skips.set(session.sessionId, skip);
89
+ let problems = [];
90
+ try {
91
+ await use(session);
92
+ await failOnLoops(session);
93
+ }
94
+ finally {
95
+ skips.delete(session.sessionId);
96
+ // What went wrong with the model fails the test even if it asserted nothing about it
97
+ // (decision 0029): read before the session, and its record, are gone.
98
+ problems = await session
99
+ .quiet()
100
+ .then((state) => state.problems)
101
+ .catch(() => []);
102
+ // Closing the session removes everything the test created. Disposing never fails a test.
103
+ await session[Symbol.asyncDispose]();
104
+ }
105
+ if (problems.length > 0)
106
+ throw new Error(`The model calls of this test went wrong:\n${problems.map((problem) => ` ${problem}`).join("\n")}`);
107
+ },
108
+ auto,
109
+ ],
110
+ channelstack: [
111
+ async ({ channelstackSession: session }, use) => {
112
+ const channelstack = await connected();
113
+ await use({
114
+ url: server.url,
115
+ sessionId: session.sessionId,
116
+ env: () => channelstack.env(),
117
+ quiet: (options = {}) => untilQuiet(() => session.quiet(), options),
118
+ reset: () => channelstack.reset(),
119
+ });
120
+ },
121
+ auto,
122
+ ],
123
+ // One fixture per channel, by its name: `slack` opens `session.slack()`; and one per provider (`openai`).
124
+ ...Object.fromEntries([...CHANNEL_NAMES, ...PROVIDER_NAMES].map((name) => [name, [channelFixture((session) => session[name]()), auto]])),
125
+ });
126
+ expect.extend(
127
+ // Vitest types a matcher's `this` as its own state; the adapter only needs `isNot` from it.
128
+ Object.fromEntries(Object.keys(matchers).map((name) => [
129
+ name,
130
+ function (subject, ...args) {
131
+ const state = {
132
+ isNot: this.isNot === true,
133
+ timeout: server.timeout,
134
+ quietFor: server.quietFor,
135
+ ...(this.equals ? { equals: this.equals } : {}),
136
+ };
137
+ return matchers[name](state, subject, ...args);
138
+ },
139
+ ])));
@@ -0,0 +1,14 @@
1
+ /**
2
+ * `channelstack` and `channelstack/client`: the generated client the test integrations are built
3
+ * on, and its types (`import type { SlackUser } from "channelstack"`).
4
+ */
5
+ export { CHANNEL_NAMES, type ChannelFixtures, PROVIDER_NAMES, type ProviderFixtures } from "./generated/channels.ts";
6
+ export * from "./generated/index.ts";
7
+ export { type Duration, parseDuration } from "./runtime/duration.ts";
8
+ export { type FileInput, type MediaSource, readMedia, setFilesRoot } from "./runtime/files.ts";
9
+ export { type InSession, QUIET } from "./runtime/client.ts";
10
+ export type { Responder } from "./runtime/responder.ts";
11
+ export type { LaunchOptions } from "./runtime/launcher.ts";
12
+ export { ChannelstackError, onRefusal, unreportedRefusals } from "./runtime/transport.ts";
13
+ export type * from "./types.ts";
14
+ export { fromMatches, ModelProblemError, type QuietOptions, type QuietState, setWaitDefaults, textMatches, untilQuiet, type WaitDefaults, type WaitOptions, WaitTimeoutError, waitDefaults, waitFor, } from "./runtime/wait.ts";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `channelstack` and `channelstack/client`: the generated client the test integrations are built
3
+ * on, and its types (`import type { SlackUser } from "channelstack"`).
4
+ */
5
+ export { CHANNEL_NAMES, PROVIDER_NAMES } from "./generated/channels.js";
6
+ export * from "./generated/index.js";
7
+ export { parseDuration } from "./runtime/duration.js";
8
+ export { readMedia, setFilesRoot } from "./runtime/files.js";
9
+ export { QUIET } from "./runtime/client.js";
10
+ export { ChannelstackError, onRefusal, unreportedRefusals } from "./runtime/transport.js";
11
+ export { fromMatches, ModelProblemError, setWaitDefaults, textMatches, untilQuiet, WaitTimeoutError, waitDefaults, waitFor, } from "./runtime/wait.js";
@@ -0,0 +1,32 @@
1
+ import type * as G from "./index.ts";
2
+ type Option<Method extends (...args: never[]) => unknown> = NonNullable<Parameters<Method>[0]>;
3
+ /** Every channel, by name, in the server's order. */
4
+ export declare const CHANNEL_NAMES: readonly ["slack", "github", "linear", "notion", "resend", "google", "discord"];
5
+ export type ChannelKind = (typeof CHANNEL_NAMES)[number];
6
+ /** Each channel's fixture, by its name: what a test framework hands a test. */
7
+ export interface ChannelFixtures {
8
+ slack: G.SlackFixture;
9
+ github: G.GitHubFixture;
10
+ linear: G.LinearFixture;
11
+ notion: G.NotionFixture;
12
+ resend: G.ResendFixture;
13
+ google: G.GoogleFixture;
14
+ discord: G.DiscordFixture;
15
+ }
16
+ /** The fakes the agent calls that aren't channels (the fake OpenAI), by name. */
17
+ export declare const PROVIDER_NAMES: readonly ["openai"];
18
+ export type ProviderKind = (typeof PROVIDER_NAMES)[number];
19
+ /** Each provider's fixture, by its name: what a test framework hands a test besides the channels. */
20
+ export interface ProviderFixtures {
21
+ openai: G.OpenAIFixture;
22
+ }
23
+ export type User = G.SlackUser | G.GitHubUser | G.LinearUser | G.NotionUser | G.ResendUser | G.GoogleUser | G.DiscordUser;
24
+ export type App = G.SlackApp | G.GitHubApp | G.LinearApp | G.NotionIntegration | G.ResendApp | G.GoogleApp | G.DiscordApp;
25
+ export type Resource = G.SlackConversation | G.GitHubRepo | G.LinearTeam | G.NotionPage | G.NotionDatabase | G.ResendThread | G.GoogleThread | G.DiscordChannel;
26
+ export type AnyDelivery = G.SlackDelivery | G.GitHubDelivery | G.LinearDelivery | G.NotionDelivery | G.ResendDelivery | G.GoogleDelivery | G.DiscordDelivery;
27
+ export type DeliveryFilter = Option<G.SlackFixture["deliveries"]> | Option<G.GitHubFixture["deliveries"]> | Option<G.LinearFixture["deliveries"]> | Option<G.NotionFixture["deliveries"]> | Option<G.ResendFixture["deliveries"]> | Option<G.GoogleFixture["deliveries"]> | Option<G.DiscordFixture["deliveries"]>;
28
+ export type ApiCall = G.SlackApiCall | G.GitHubApiCall | G.LinearApiCall | G.NotionApiCall | G.ResendApiCall | G.GoogleApiCall | G.DiscordApiCall;
29
+ export type Transcript = G.SlackTranscript | G.GitHubTranscript | G.LinearTranscript | G.NotionTranscript | G.ResendTranscript | G.GoogleTranscript | G.DiscordTranscript;
30
+ /** What every channel fixture has to inspect what the agent did: the fixture whose deliveries are `TDelivery`. */
31
+ export type Inspectable<TDelivery extends AnyDelivery = AnyDelivery> = TDelivery extends G.SlackDelivery ? G.SlackFixture : TDelivery extends G.GitHubDelivery ? G.GitHubFixture : TDelivery extends G.LinearDelivery ? G.LinearFixture : TDelivery extends G.NotionDelivery ? G.NotionFixture : TDelivery extends G.ResendDelivery ? G.ResendFixture : TDelivery extends G.GoogleDelivery ? G.GoogleFixture : TDelivery extends G.DiscordDelivery ? G.DiscordFixture : never;
32
+ export {};
@@ -0,0 +1,4 @@
1
+ /** Every channel, by name, in the server's order. */
2
+ export const CHANNEL_NAMES = ["slack", "github", "linear", "notion", "resend", "google", "discord"];
3
+ /** The fakes the agent calls that aren't channels (the fake OpenAI), by name. */
4
+ export const PROVIDER_NAMES = ["openai"];