@zetaloop/chappie 0.3.2 → 0.4.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/src/state.ts CHANGED
@@ -5,8 +5,7 @@ import type { QuestionAnswer, QuestionRecord } from "./questions.ts";
5
5
 
6
6
  interface StateFile {
7
7
  bindings?: Record<string, string>;
8
- joins?: Record<string, string[]>;
9
- workflows?: Record<string, string>;
8
+ codes?: Record<string, string>;
10
9
  deliveries?: DeliveryRecord[];
11
10
  questions?: QuestionRecord[];
12
11
  }
@@ -15,8 +14,7 @@ export class State {
15
14
  readonly #path: string;
16
15
  readonly #temporaryPath: string;
17
16
  readonly #bindings = new Map<string, string>();
18
- readonly #joins = new Map<string, Set<string>>();
19
- readonly #workflows = new Map<string, string>();
17
+ readonly #codes = new Map<string, string>();
20
18
  readonly #deliveries = new Map<string, DeliveryRecord>();
21
19
  readonly #questions = new Map<string, QuestionRecord>();
22
20
  #writes = Promise.resolve();
@@ -38,11 +36,8 @@ export class State {
38
36
  for (const [chatId, sessionId] of Object.entries(state.bindings ?? {})) {
39
37
  if (typeof sessionId === "string") this.#bindings.set(chatId, sessionId);
40
38
  }
41
- for (const [chatId, ids] of Object.entries(state.joins ?? {})) {
42
- this.#joins.set(chatId, new Set(ids));
43
- }
44
- for (const [chatId, id] of Object.entries(state.workflows ?? {})) {
45
- this.#workflows.set(chatId, id);
39
+ for (const [sessionId, code] of Object.entries(state.codes ?? {})) {
40
+ this.#codes.set(sessionId, code);
46
41
  }
47
42
  for (const delivery of state.deliveries ?? []) {
48
43
  if (delivery?.id) this.#deliveries.set(delivery.id, delivery);
@@ -51,12 +46,13 @@ export class State {
51
46
  this.#questions.set(question.id, question);
52
47
  }
53
48
 
54
- workflow(chatId: string): string | undefined {
55
- return this.#workflows.get(chatId);
49
+ code(sessionId: string): string | undefined {
50
+ return this.#codes.get(sessionId);
56
51
  }
57
52
 
58
- setWorkflow(chatId: string, id: string): Promise<void> {
59
- this.#workflows.set(chatId, id);
53
+ setCode(sessionId: string, code: string | undefined): Promise<void> {
54
+ if (code === undefined) this.#codes.delete(sessionId);
55
+ else this.#codes.set(sessionId, code);
60
56
  return this.#save();
61
57
  }
62
58
 
@@ -72,21 +68,9 @@ export class State {
72
68
  return counts;
73
69
  }
74
70
 
75
- async join(
76
- chatId: string,
77
- sessionId: string,
78
- workflowId: string | undefined,
79
- ): Promise<boolean> {
80
- const changed = this.#bindings.get(chatId) !== sessionId;
81
- const workflows = changed
82
- ? new Set<string>()
83
- : (this.#joins.get(chatId) ?? new Set<string>());
84
- if (!changed && (!workflowId || workflows.has(workflowId))) return false;
71
+ bind(chatId: string, sessionId: string): Promise<void> {
85
72
  this.#bindings.set(chatId, sessionId);
86
- if (workflowId) workflows.add(workflowId);
87
- this.#joins.set(chatId, workflows);
88
- await this.#save();
89
- return true;
73
+ return this.#save();
90
74
  }
91
75
 
92
76
  deliveries(chatId: string): DeliveryRecord[] {
@@ -179,13 +163,10 @@ export class State {
179
163
  #save(): Promise<void> {
180
164
  const saved = this.#writes.then(async () => {
181
165
  const bindings = Object.fromEntries(this.#bindings);
182
- const joins = Object.fromEntries(
183
- [...this.#joins].map(([chatId, ids]) => [chatId, [...ids]]),
184
- );
185
- const workflows = Object.fromEntries(this.#workflows);
166
+ const codes = Object.fromEntries(this.#codes);
186
167
  await writeFile(
187
168
  this.#temporaryPath,
188
- `${JSON.stringify({ bindings, joins, workflows, deliveries: [...this.#deliveries.values()], questions: [...this.#questions.values()] }, null, 2)}\n`,
169
+ `${JSON.stringify({ bindings, codes, deliveries: [...this.#deliveries.values()], questions: [...this.#questions.values()] }, null, 2)}\n`,
189
170
  {
190
171
  mode: 0o600,
191
172
  },
package/src/tools.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  createWriteToolDefinition,
7
7
  } from "@earendil-works/pi-coding-agent";
8
8
  import { fromJsonSchema } from "@modelcontextprotocol/server";
9
+ import type { Initialization } from "./broker.ts";
9
10
  import { toolResultsContent } from "./delivery.ts";
10
11
  import type { SessionInput } from "./ipc.ts";
11
12
  import { contentWithImageReferences } from "./resources.ts";
@@ -38,10 +39,18 @@ export function toolResult(
38
39
  sessionId: string,
39
40
  cwd: string,
40
41
  inputs: SessionInput[] = [],
42
+ initialization?: Initialization,
41
43
  ) {
42
44
  return {
43
45
  content: [
44
- { type: "text" as const, text: JSON.stringify({ sessionId, cwd }) },
46
+ {
47
+ type: "text" as const,
48
+ text: JSON.stringify({
49
+ sessionId,
50
+ cwd,
51
+ ...(initialization ? { initialization } : {}),
52
+ }),
53
+ },
45
54
  ...toolResultsContent(toolResults, sessionId),
46
55
  ...inputContent(inputs),
47
56
  ],