appsdoor-laptop 0.1.0 → 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.
package/dist/Loop.js CHANGED
@@ -21,23 +21,39 @@ const promptOf = (pickup) => pickup.notes.trim() === ""
21
21
  : `${pickup.title}\n\n${pickup.notes}`;
22
22
  /** The state a Pickup's coding thread should reach. */
23
23
  const targetOf = (pickup) => pickup.wanted === "closed" ? "finished" : pickup.wanted;
24
- /** Whether the harness has something to do for the Pickup. A failed one waits for a member's Retry. */
24
+ /**
25
+ * Whether the harness has something to do for the Pickup. A failed one
26
+ * waits for a member's Retry; one waiting for the member runs as it is.
27
+ */
25
28
  const needsWork = (pickup) => !(pickup.state === "failed" && pickup.wanted === "running") &&
26
- (pickup.state !== targetOf(pickup) ||
29
+ ((pickup.state === "waiting" ? "running" : pickup.state) !==
30
+ targetOf(pickup) ||
27
31
  (pickup.wanted === "running" && pickup.messages.length > 0));
28
32
  export const runLoop = (config, key, harnesses) => Effect.gen(function* () {
29
33
  const status = makeStatus();
30
34
  const report = (input) => Channel.report(config.api, key, { operation: "reportPickup", input });
31
35
  const watchers = new Map();
32
- /** Posts the thread's replies to its Pickup's card for as long as it runs. */
36
+ /**
37
+ * For as long as the thread runs, follows its turns: one starting reads
38
+ * running, and one ending reads waiting, after its reply is posted to
39
+ * the Pickup's card.
40
+ */
33
41
  const watch = (pickup, thread) => Effect.gen(function* () {
34
42
  const harness = harnesses.get(pickup.harness);
35
43
  if (watchers.has(thread) || harness === undefined)
36
44
  return;
37
- const fiber = yield* harness.replies(thread).pipe(Stream.runForEach((text) => Channel.report(config.api, key, {
45
+ const postReply = (text) => Channel.report(config.api, key, {
38
46
  operation: "postReply",
39
47
  input: { pickup: pickup.id, text },
40
- }).pipe(Effect.catch((error) => status.failed(pickup.title, `Its reply wasn't posted: ${messageOf(error)}`)))), Effect.forkDetach);
48
+ }).pipe(Effect.catch((error) => status.failed(pickup.title, `Its reply wasn't posted: ${messageOf(error)}`)));
49
+ const fiber = yield* harness.turns(thread).pipe(Stream.runForEach((turn) => Effect.gen(function* () {
50
+ if (turn._tag === "Waiting" && turn.reply !== null)
51
+ yield* postReply(turn.reply);
52
+ yield* report({
53
+ pickup: pickup.id,
54
+ state: turn._tag === "Waiting" ? "waiting" : "running",
55
+ }).pipe(Effect.catch((error) => status.failed(pickup.title, `Appsdoor didn't take the report: ${messageOf(error)}`)));
56
+ })), Effect.forkDetach);
41
57
  watchers.set(thread, fiber);
42
58
  });
43
59
  const unwatch = (thread) => Effect.gen(function* () {
@@ -96,7 +112,7 @@ export const runLoop = (config, key, harnesses) => Effect.gen(function* () {
96
112
  }),
97
113
  });
98
114
  }).pipe(Effect.catch((error) => status.failed(pickup.title, `Appsdoor didn't take the report: ${messageOf(error)}`)));
99
- /** Brings every Pickup along, then keeps exactly the running threads' replies watched. */
115
+ /** Brings every Pickup along, then keeps exactly the running threads' turns followed. */
100
116
  const follow = (pickups) => Effect.gen(function* () {
101
117
  for (const pickup of pickups)
102
118
  yield* work(pickup);
@@ -1,4 +1,9 @@
1
1
  import { Context, Data } from "effect";
2
+ export const working = { _tag: "Working" };
3
+ export const waiting = (reply) => ({
4
+ _tag: "Waiting",
5
+ reply: reply === null || reply.trim() === "" ? null : reply,
6
+ });
2
7
  /** The harness couldn't do what was asked; `reason` is shown to the member as is. */
3
8
  export class HarnessFailed extends Data.TaggedError("HarnessFailed") {
4
9
  }
@@ -1,6 +1,6 @@
1
1
  import { getSessionInfo, query, } from "@anthropic-ai/claude-agent-sdk";
2
2
  import { Effect, Layer, Queue, Stream } from "effect";
3
- import { continueMessage, Harness, HarnessFailed, } from "./Harness.js";
3
+ import { continueMessage, Harness, HarnessFailed, waiting, working, } from "./Harness.js";
4
4
  const userMessage = (text) => ({
5
5
  type: "user",
6
6
  message: { role: "user", content: text },
@@ -44,13 +44,13 @@ const failed = (error) => new HarnessFailed({
44
44
  });
45
45
  export const ClaudeCodeHarness = Layer.effect(Harness, Effect.gen(function* () {
46
46
  const sessions = new Map();
47
- const replyQueues = new Map();
47
+ const turnQueues = new Map();
48
48
  const queueOf = (thread) => Effect.gen(function* () {
49
- const known = replyQueues.get(thread);
49
+ const known = turnQueues.get(thread);
50
50
  if (known !== undefined)
51
51
  return known;
52
52
  const queue = yield* Queue.unbounded();
53
- replyQueues.set(thread, queue);
53
+ turnQueues.set(thread, queue);
54
54
  return queue;
55
55
  });
56
56
  /** Opens the session, new or resumed, and reads its turns in the background. */
@@ -65,13 +65,22 @@ export const ClaudeCodeHarness = Layer.effect(Harness, Effect.gen(function* () {
65
65
  });
66
66
  const session = { query: running, push: input.push, end: input.end };
67
67
  sessions.set(thread, session);
68
- const replies = yield* queueOf(thread);
69
- yield* Stream.fromAsyncIterable(running, failed).pipe(Stream.runForEach((message) => message.type === "result" &&
70
- message.subtype === "success" &&
71
- !message.is_error &&
72
- message.result.trim() !== ""
73
- ? Queue.offer(replies, message.result)
74
- : Effect.void), Effect.ignore, Effect.ensuring(Effect.sync(() => {
68
+ const turns = yield* queueOf(thread);
69
+ // A turn starts with its first message and ends with its result;
70
+ // only a successful one leaves the thread waiting.
71
+ let busy = false;
72
+ yield* Stream.fromAsyncIterable(running, failed).pipe(Stream.runForEach((message) => {
73
+ if (message.type !== "result") {
74
+ if (busy)
75
+ return Effect.void;
76
+ busy = true;
77
+ return Queue.offer(turns, working);
78
+ }
79
+ busy = false;
80
+ return message.subtype === "success" && !message.is_error
81
+ ? Queue.offer(turns, waiting(message.result))
82
+ : Effect.void;
83
+ }), Effect.ignore, Effect.ensuring(Effect.sync(() => {
75
84
  if (sessions.get(thread) === session)
76
85
  sessions.delete(thread);
77
86
  })), Effect.forkDetach);
@@ -131,6 +140,6 @@ export const ClaudeCodeHarness = Layer.effect(Harness, Effect.gen(function* () {
131
140
  session.push(message);
132
141
  return { thread, link: null, state: "running" };
133
142
  }),
134
- replies: (thread) => Stream.unwrap(Effect.map(queueOf(thread), Stream.fromQueue)),
143
+ turns: (thread) => Stream.unwrap(Effect.map(queueOf(thread), Stream.fromQueue)),
135
144
  });
136
145
  }));
@@ -2,7 +2,7 @@ import { Codex } from "@openai/codex-sdk";
2
2
  import { Deferred, Effect, Layer, Queue, Stream } from "effect";
3
3
  import { runCommand } from "../command.js";
4
4
  import { readJson, writeJson } from "../Home.js";
5
- import { continueMessage, Harness, HarnessFailed, } from "./Harness.js";
5
+ import { continueMessage, Harness, HarnessFailed, waiting, working, } from "./Harness.js";
6
6
  /*
7
7
  * Codex through `@openai/codex-sdk`. Codex names its own threads, so the
8
8
  * thread each Pickup started is remembered in `<home>/codex-threads.json`
@@ -18,19 +18,19 @@ const failed = (error) => new HarnessFailed({
18
18
  export const CodexHarness = Layer.effect(Harness, Effect.gen(function* () {
19
19
  const codex = new Codex();
20
20
  const live = new Map();
21
- const replyQueues = new Map();
21
+ const turnQueues = new Map();
22
22
  const queueOf = (thread) => Effect.gen(function* () {
23
- const known = replyQueues.get(thread);
23
+ const known = turnQueues.get(thread);
24
24
  if (known !== undefined)
25
25
  return known;
26
26
  const queue = yield* Queue.unbounded();
27
- replyQueues.set(thread, queue);
27
+ turnQueues.set(thread, queue);
28
28
  return queue;
29
29
  });
30
30
  /**
31
31
  * Runs one turn after the ones already queued on `session`; `named` hears
32
32
  * the thread's id once Codex gives it. The final agent message of a
33
- * completed turn becomes a reply.
33
+ * completed turn becomes its reply.
34
34
  */
35
35
  const queueTurn = (session, text, named) => {
36
36
  session.turns = session.turns
@@ -40,20 +40,22 @@ export const CodexHarness = Layer.effect(Harness, Effect.gen(function* () {
40
40
  const { events } = await session.thread.runStreamed(text, {
41
41
  signal: session.abort.signal,
42
42
  });
43
- let last;
43
+ let last = null;
44
+ const tell = (turn) => {
45
+ const id = session.thread.id;
46
+ if (id !== null)
47
+ Effect.runSync(Queue.offer(Effect.runSync(queueOf(id)), turn));
48
+ };
44
49
  for await (const event of events) {
45
50
  if (event.type === "thread.started")
46
51
  named(event.thread_id);
52
+ if (event.type === "turn.started")
53
+ tell(working);
47
54
  if (event.type === "item.completed" &&
48
55
  event.item.type === "agent_message")
49
56
  last = event.item.text;
50
- if (event.type === "turn.completed" &&
51
- last !== undefined &&
52
- last.trim() !== "") {
53
- const id = session.thread.id;
54
- if (id !== null)
55
- Effect.runSync(Queue.offer(Effect.runSync(queueOf(id)), last));
56
- }
57
+ if (event.type === "turn.completed")
58
+ tell(waiting(last));
57
59
  }
58
60
  })
59
61
  .catch(() => {
@@ -134,6 +136,6 @@ export const CodexHarness = Layer.effect(Harness, Effect.gen(function* () {
134
136
  state: run.want === "closed" ? "finished" : "stopped",
135
137
  };
136
138
  }),
137
- replies: (thread) => Stream.unwrap(Effect.map(queueOf(thread), Stream.fromQueue)),
139
+ turns: (thread) => Stream.unwrap(Effect.map(queueOf(thread), Stream.fromQueue)),
138
140
  });
139
141
  }));
@@ -1,6 +1,6 @@
1
1
  import { Effect, Layer, Queue, Stream } from "effect";
2
2
  import { readJson, writeJson } from "../Home.js";
3
- import { continueMessage, Harness, HarnessFailed, } from "./Harness.js";
3
+ import { continueMessage, Harness, HarnessFailed, waiting, working, } from "./Harness.js";
4
4
  const logName = "fake-harness.json";
5
5
  const failsStart = process.env.APPSDOOR_LAPTOP_FAKE_FAIL === "start";
6
6
  const record = (change) => Effect.gen(function* () {
@@ -13,7 +13,8 @@ const record = (change) => Effect.gen(function* () {
13
13
  yield* writeJson(logName, change(log));
14
14
  });
15
15
  export const FakeHarness = Layer.effect(Harness, Effect.gen(function* () {
16
- // Replies wait in their thread's queue until the loop reads them.
16
+ // Each message is a turn, answered at once; its events wait in the
17
+ // thread's queue until the loop reads them.
17
18
  const queues = new Map();
18
19
  const queueOf = (thread) => Effect.gen(function* () {
19
20
  const known = queues.get(thread);
@@ -24,7 +25,9 @@ export const FakeHarness = Layer.effect(Harness, Effect.gen(function* () {
24
25
  return queue;
25
26
  });
26
27
  const turn = (thread, message) => Effect.gen(function* () {
27
- yield* Queue.offer(yield* queueOf(thread), `Fake reply to: ${message}`);
28
+ const queue = yield* queueOf(thread);
29
+ yield* Queue.offer(queue, working);
30
+ yield* Queue.offer(queue, waiting(`Fake reply to: ${message}`));
28
31
  });
29
32
  const send = (thread, messages) => Effect.forEach(messages, (text) => Effect.gen(function* () {
30
33
  yield* record((log) => ({
@@ -88,6 +91,6 @@ export const FakeHarness = Layer.effect(Harness, Effect.gen(function* () {
88
91
  return { thread, link: null, state: "finished" };
89
92
  }
90
93
  }),
91
- replies: (thread) => Stream.unwrap(Effect.map(queueOf(thread), Stream.fromQueue)),
94
+ turns: (thread) => Stream.unwrap(Effect.map(queueOf(thread), Stream.fromQueue)),
92
95
  });
93
96
  }));
@@ -4,7 +4,7 @@ import path from "node:path";
4
4
  import { Effect, Layer, Queue, Schedule, Stream } from "effect";
5
5
  import { McpClient, McpProtocol } from "effect/ai";
6
6
  import { FetchHttpClient } from "effect/http";
7
- import { continueMessage, Harness, HarnessFailed, } from "./Harness.js";
7
+ import { continueMessage, Harness, HarnessFailed, waiting, working, } from "./Harness.js";
8
8
  import { approveInT3Code, readApproval, reconnectHint } from "./t3-approval.js";
9
9
  /*
10
10
  * T3 Code through its local MCP server (`/mcp` on the port in
@@ -158,29 +158,44 @@ export const T3CodeHarness = (laptopName) => {
158
158
  return { thread, link, state: "finished" };
159
159
  }));
160
160
  /**
161
- * Waits on the thread's runs and, for each one that completes, reads its
162
- * last assistant message. A run already finished when watching starts is
163
- * not replied again.
161
+ * Waits on the thread's runs: one running has started (from Appsdoor or
162
+ * the T3 Code app); one paused on a question or an approval, or one
163
+ * that completes, leaves the thread waiting, the completed one with its
164
+ * last assistant message. A run already finished when watching starts
165
+ * is not told again.
164
166
  */
165
- const watch = (thread, reply) => Effect.gen(function* () {
167
+ const watch = (thread, tell) => Effect.gen(function* () {
166
168
  const read = (limit) => connected((call) => call("t3_thread_read", { threadId: thread, view: "messages", limit })).pipe(Effect.retry(Schedule.spaced("10 seconds")));
167
169
  const answered = new Set();
170
+ // What the thread was last told to be, so each change is told once.
171
+ let told = "";
172
+ const tellOnce = (key, turn) => {
173
+ if (told === key)
174
+ return;
175
+ told = key;
176
+ tell(turn);
177
+ };
168
178
  const { thread: detail } = (yield* read(1));
169
179
  if (detail.activeRunId === null && detail.latestRunId !== null)
170
180
  answered.add(detail.latestRunId);
171
181
  while (true) {
172
182
  const waited = yield* connected((call) => call("t3_thread_wait", { threadId: thread, timeoutMs: 45_000 })).pipe(Effect.retry(Schedule.spaced("10 seconds")));
173
183
  const run = waited.runId;
184
+ // A run asking the member a question or an approval waits too.
185
+ if (run !== null && waited.status === "running")
186
+ tellOnce(`${run}:running`, working);
187
+ if (run !== null && waited.status === "waiting")
188
+ tellOnce(`${run}:waiting`, waiting(null));
174
189
  if (run !== null &&
175
190
  waited.status === "completed" &&
176
191
  !answered.has(run)) {
177
192
  answered.add(run);
193
+ told = `${run}:completed`;
178
194
  const { items } = (yield* read(20));
179
195
  const last = items.find((item) => item.runId === run &&
180
196
  item.type === "assistant_message" &&
181
197
  (item.text ?? "").trim() !== "");
182
- if (last?.text)
183
- reply(last.text);
198
+ tell(waiting(last?.text ?? null));
184
199
  }
185
200
  // A thread with no running turn answers at once; don't spin on it.
186
201
  if (waited.timedOut !== true)
@@ -200,8 +215,8 @@ export const T3CodeHarness = (laptopName) => {
200
215
  return { _tag: "Ready" };
201
216
  }),
202
217
  converge,
203
- replies: (thread) => Stream.callback((queue) => Effect.forkScoped(watch(thread, (text) => {
204
- Queue.offerUnsafe(queue, text);
218
+ turns: (thread) => Stream.callback((queue) => Effect.forkScoped(watch(thread, (turn) => {
219
+ Queue.offerUnsafe(queue, turn);
205
220
  }))),
206
221
  }));
207
222
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appsdoor-laptop",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "The Appsdoor laptop app: cards from your boards start coding threads in your harness.",
5
5
  "license": "UNLICENSED",
6
6
  "homepage": "https://appsdoor.io",