jive-agentic-workflow-comment-answering 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 (41) hide show
  1. package/README.md +101 -2
  2. package/dist/comment-answering.js +12 -0
  3. package/dist/comment-answering.js.map +1 -0
  4. package/dist/generated/commentAnswering.js +36 -0
  5. package/dist/generated/commentAnswering.js.map +1 -0
  6. package/dist/generated/workflows.js +6 -0
  7. package/dist/generated/workflows.js.map +1 -0
  8. package/dist/index.js +8 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/rules.js +77 -0
  11. package/dist/rules.js.map +1 -0
  12. package/dist/steps/answer.js +49 -0
  13. package/dist/steps/answer.js.map +1 -0
  14. package/dist/steps/check-reply.js +23 -0
  15. package/dist/steps/check-reply.js.map +1 -0
  16. package/dist/steps/decide.js +26 -0
  17. package/dist/steps/decide.js.map +1 -0
  18. package/dist/steps/mark-seen.js +15 -0
  19. package/dist/steps/mark-seen.js.map +1 -0
  20. package/dist/steps/post-reply.js +27 -0
  21. package/dist/steps/post-reply.js.map +1 -0
  22. package/dist/steps/shared.js +25 -0
  23. package/dist/steps/shared.js.map +1 -0
  24. package/dist/task.js +25 -0
  25. package/dist/task.js.map +1 -0
  26. package/dist/testing/harness.js +153 -0
  27. package/dist/testing/harness.js.map +1 -0
  28. package/package.json +33 -4
  29. package/src/comment-answering.ts +12 -0
  30. package/src/generated/commentAnswering.ts +36 -0
  31. package/src/generated/workflows.ts +7 -0
  32. package/src/index.ts +7 -0
  33. package/src/rules.ts +97 -0
  34. package/src/steps/answer.ts +50 -0
  35. package/src/steps/check-reply.ts +22 -0
  36. package/src/steps/decide.ts +26 -0
  37. package/src/steps/mark-seen.ts +15 -0
  38. package/src/steps/post-reply.ts +27 -0
  39. package/src/steps/shared.ts +32 -0
  40. package/src/task.ts +28 -0
  41. package/src/testing/harness.ts +166 -0
@@ -0,0 +1,166 @@
1
+ import { AgentRunner, FileStore, type GraphLine, type LineEnd, lineOutOfStep, type TaskFile, type Travelled, WorkflowComments } from "@jive-sh/workflow-platform"
2
+ import { Effect, Layer, Option, Schema } from "effect"
3
+ import { commentAnswering } from "../generated/commentAnswering"
4
+ import type { CommentFields } from "../task"
5
+
6
+ type Comment = WorkflowComments.WorkflowComment
7
+
8
+ /**
9
+ * A source's comments in memory, behaving as the organization's server does: one reply per conversation and key, a
10
+ * reaction put on or taken off once, and an edit only of the agent's own reply. It records every write.
11
+ */
12
+ export class FakeComments implements WorkflowComments.IWorkflowComments {
13
+ readonly comments: Array<Comment & { readonly conversation: string; readonly key?: string }> = [];
14
+ readonly writes: Array<string> = []
15
+ private next = 1
16
+
17
+ constructor(readonly place = { workflow: "skillRefinement", version: { kind: "current" } as const, element: "step:judge", link: "https://jive.sh/comments", graph: { name: "skillRefinement" } }) {}
18
+
19
+ /** A person's comment, or an agent's post, in the conversation `conversation` (a new one when absent). */
20
+ add(input: { readonly author: Comment["author"]; readonly text: string; readonly conversation?: string }): string {
21
+ const id = `c${this.next++}`
22
+ this.comments.push({ id, author: input.author, text: input.text, createdAt: this.next, reactions: [], conversation: input.conversation ?? id })
23
+ return id
24
+ }
25
+
26
+ personReacts(input: { readonly comment: string; readonly emoji: string }) {
27
+ const comment = this.find(input.comment)
28
+ this.replace({ ...comment, reactions: [...comment.reactions, { emoji: input.emoji, by: { kind: "person", account: "alex" } }] })
29
+ }
30
+
31
+ find(id: string) {
32
+ const comment = this.comments.find((one) => one.id === id)
33
+ if (comment === undefined) throw new Error(`no comment ${id}`)
34
+ return comment
35
+ }
36
+
37
+ private replace(comment: FakeComments["comments"][number]) {
38
+ this.comments[this.comments.findIndex((one) => one.id === comment.id)] = comment
39
+ }
40
+
41
+ *get(input: { readonly comment: string }) {
42
+ const comment = this.find(input.comment)
43
+ const strip = ({ conversation: _conversation, key: _key, ...rest }: FakeComments["comments"][number]): Comment => rest
44
+ return {
45
+ place: this.place,
46
+ conversation: comment.conversation,
47
+ comment: strip(comment),
48
+ comments: this.comments.filter((one) => one.conversation === comment.conversation).map(strip),
49
+ }
50
+ }
51
+
52
+ *react(input: { readonly comment: string; readonly agent: string; readonly emoji: string; readonly on: boolean }) {
53
+ const comment = this.find(input.comment)
54
+ const has = comment.reactions.some((r) => r.emoji === input.emoji && r.by.kind === "agent" && r.by.agent === input.agent)
55
+ if (has === input.on) return
56
+ this.writes.push(`${input.on ? "react" : "unreact"} ${input.emoji} ${input.comment}`)
57
+ this.replace({
58
+ ...comment,
59
+ reactions: input.on
60
+ ? [...comment.reactions, { emoji: input.emoji, by: { kind: "agent", agent: input.agent } }]
61
+ : comment.reactions.filter((r) => !(r.emoji === input.emoji && r.by.kind === "agent" && r.by.agent === input.agent)),
62
+ })
63
+ }
64
+
65
+ *reply(input: { readonly conversation: string; readonly agent: string; readonly text: string; readonly key: string }) {
66
+ const before = this.comments.find((one) => one.conversation === input.conversation && one.key === input.key)
67
+ if (before !== undefined) return { comment: before.id, created: false }
68
+ const id = `c${this.next++}`
69
+ this.comments.push({ id, author: { kind: "agent", agent: input.agent, workflow: "commentAnswering", run: "run" }, text: input.text, createdAt: this.next, reactions: [], conversation: input.conversation, key: input.key })
70
+ this.writes.push(`reply ${id}`)
71
+ return { comment: id, created: true }
72
+ }
73
+
74
+ *edit(input: { readonly comment: string; readonly text: string }) {
75
+ const comment = this.find(input.comment)
76
+ if (comment.author.kind !== "agent") return yield* Effect.fail(new WorkflowComments.WorkflowCommentsCallFailed({ call: "edit", cause: "not the agent's" }))
77
+ this.writes.push(`edit ${input.comment}`)
78
+ this.replace({ ...comment, text: input.text, editedAt: this.next++ })
79
+ }
80
+ }
81
+
82
+ /** An agent that answers each call with the next of `outputs`, recording what it was asked. */
83
+ export class ScriptedAgent implements AgentRunner.IAgentRunner {
84
+ readonly asked: Array<AgentRunner.AgentRunInput> = []
85
+ constructor(private readonly outputs: ReadonlyArray<string>) {}
86
+ *run(input: AgentRunner.AgentRunInput) {
87
+ this.asked.push(input)
88
+ const output = this.outputs[Math.min(this.asked.length - 1, this.outputs.length - 1)] ?? ""
89
+ return {
90
+ output,
91
+ agent: "scripted",
92
+ transcript: { name: "transcript.json", ref: "t" },
93
+ stoppedAtTurnLimit: false,
94
+ spend: { fresh: 0, cacheWrite: 0, cacheRead: 0, output: 0, requests: 1, costUsd: 0 },
95
+ }
96
+ }
97
+ }
98
+
99
+ class MemoryFiles implements FileStore.IFileStore {
100
+ private readonly bytes = new Map<string, Uint8Array>()
101
+ private next = 1;
102
+ *write(input: { readonly name: string; readonly bytes: Uint8Array }) {
103
+ const ref = `f${this.next++}`
104
+ this.bytes.set(ref, input.bytes)
105
+ return { name: input.name, ref }
106
+ }
107
+ *read(file: TaskFile) {
108
+ const found = this.bytes.get(file.ref)
109
+ if (found === undefined) return yield* Effect.fail(new FileStore.FileStoreError({ file: file.name, cause: "missing" }))
110
+ return found
111
+ }
112
+ }
113
+
114
+ /**
115
+ * One task through the compiled workflow in memory: each step's own code runs on the fakes, and the platform's own
116
+ * `lineOutOfStep` decides what comes next from the workflow's lines, their limits included. `runTwice` runs a step's code twice in a row, as a run whose report was
117
+ * lost is run again, to show it writes nothing twice.
118
+ */
119
+ export const runTask = async (input: {
120
+ readonly comments: FakeComments
121
+ readonly agent: ScriptedAgent
122
+ readonly fields: Pick<CommentFields, "source" | "comment" | "event">
123
+ readonly runTwice?: string
124
+ }) => {
125
+ const files = new MemoryFiles()
126
+ const layer = Layer.mergeAll(
127
+ Layer.succeed(WorkflowComments.WorkflowComments, input.comments),
128
+ Layer.succeed(AgentRunner.AgentRunner, input.agent),
129
+ Layer.succeed(FileStore.FileStore, files),
130
+ )
131
+ const decodeFields = Schema.decodeUnknownSync(commentAnswering.task.fields)
132
+ let fields = decodeFields(input.fields)
133
+ let taskFiles: Array<TaskFile> = []
134
+ const notes: Array<{ workflow: string; step: string; note: string; at: number }> = []
135
+ const trace: Array<string> = []
136
+ const lines: ReadonlyArray<GraphLine> = commentAnswering.graph.lines
137
+ let at: LineEnd = lines.find((line) => "event" in line.from)!.to
138
+ let travelled: Travelled = []
139
+ for (let turn = 0; turn < 50 && "step" in at; turn++) {
140
+ const name: string = at.step
141
+ const step = commentAnswering.runnableSteps[name]!
142
+ trace.push(name)
143
+ const run = (): Promise<{ endState: string; fields?: Partial<CommentFields>; files?: ReadonlyArray<{ name: string; bytes: Uint8Array }>; note?: string }> =>
144
+ Effect.runPromise(
145
+ Effect.provide(
146
+ Effect.gen(() => step.run({ taskId: "task-1", fields, files: taskFiles, notes, lastFingerprint: Option.none() })),
147
+ layer,
148
+ ) as Effect.Effect<{ endState: string; fields?: Partial<CommentFields>; files?: ReadonlyArray<{ name: string; bytes: Uint8Array }>; note?: string }, unknown, never>,
149
+ )
150
+ if (input.runTwice === name) await run()
151
+ const outcome = await run()
152
+ if (outcome.fields !== undefined) fields = { ...fields, ...outcome.fields }
153
+ for (const file of outcome.files ?? []) {
154
+ const written = await Effect.runPromise(Effect.gen(() => files.write(file)))
155
+ taskFiles = [...taskFiles.filter((one) => one.name !== file.name), written]
156
+ }
157
+ if (outcome.note !== undefined) notes.push({ workflow: "commentAnswering", step: name, note: outcome.note, at: turn })
158
+ const next = lineOutOfStep({ graph: commentAnswering.graph, step: name, endState: outcome.endState, travelled })
159
+ if (Option.isNone(next)) throw new Error(`no line out of ${name} for ${outcome.endState}`)
160
+ if (Option.isSome(next.value.limitReached)) notes.push({ workflow: "commentAnswering", step: name, note: `ended ${next.value.endState}: line ${next.value.limitReached.value.id} reached its limit`, at: turn })
161
+ travelled = [...next.value.travelled]
162
+ at = next.value.line.to
163
+ }
164
+ if (!("end" in at)) throw new Error("the task never ended")
165
+ return { end: at.end, trace, fields, notes }
166
+ }