@tinyweb_dev/agent-framework-agent 0.1.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/LICENSE +21 -0
- package/README.md +279 -0
- package/package.json +41 -0
- package/src/agent.ts +317 -0
- package/src/attachments.ts +155 -0
- package/src/bus/bus.ts +89 -0
- package/src/bus/index.ts +1 -0
- package/src/define-tool.ts +41 -0
- package/src/events.ts +162 -0
- package/src/http-handlers.ts +251 -0
- package/src/index.ts +120 -0
- package/src/loop-guard.ts +99 -0
- package/src/mcp/client.ts +110 -0
- package/src/mcp/index.ts +1 -0
- package/src/permission.ts +72 -0
- package/src/plugin/apply.ts +89 -0
- package/src/plugin/index.ts +3 -0
- package/src/plugin/loader.ts +42 -0
- package/src/plugin/types.ts +134 -0
- package/src/prompt/index.ts +90 -0
- package/src/prompt/objective.ts +104 -0
- package/src/prompt/response-style.ts +59 -0
- package/src/prompt/tool-use-guidelines.ts +57 -0
- package/src/prompt/tool-use.ts +27 -0
- package/src/runtime.ts +22 -0
- package/src/session/adapter.ts +37 -0
- package/src/session/compaction.ts +160 -0
- package/src/session/index.ts +11 -0
- package/src/session/memory.ts +154 -0
- package/src/session/overflow.ts +50 -0
- package/src/session/postgres.ts +330 -0
- package/src/session/processor.ts +500 -0
- package/src/session/replay.ts +140 -0
- package/src/session/schema.ts +140 -0
- package/src/session/sqlite.ts +281 -0
- package/src/session-api.ts +103 -0
- package/src/skill/index.ts +113 -0
- package/src/sse-handler.ts +67 -0
- package/src/transcript.ts +126 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transcript helpers for consumers persisting their own chat history
|
|
3
|
+
* (e.g. a Next.js app with a `messages` table) on top of the framework's
|
|
4
|
+
* `AgentEvent` stream.
|
|
5
|
+
*
|
|
6
|
+
* Why this lives in `@tinyweb-agent-framework/agent`:
|
|
7
|
+
*
|
|
8
|
+
* The conventional "every assistant turn must call a tool" prompt regime
|
|
9
|
+
* uses [`display_response`](../../tool/src/display_response.ts:1) from
|
|
10
|
+
* `@tinyweb-agent-framework/tool` as the escape hatch for plain prose.
|
|
11
|
+
* When the model invokes `display_response` it emits NO `text-delta` —
|
|
12
|
+
* the user-facing reply lives entirely inside the tool call's
|
|
13
|
+
* `input.response`. App-level code that only watches `text-delta` ends
|
|
14
|
+
* up with an empty assistant string and skips persisting the turn, so
|
|
15
|
+
* the transcript disappears after reload (bug 0003).
|
|
16
|
+
*
|
|
17
|
+
* `collectAssistantText` centralises the fallback so every consumer gets
|
|
18
|
+
* it for free without re-implementing the convention.
|
|
19
|
+
*/
|
|
20
|
+
import type { AgentEvent } from "./events"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Configuration for [`collectAssistantText`](./transcript.ts:1).
|
|
24
|
+
*
|
|
25
|
+
* All fields are optional and have framework-friendly defaults. Override
|
|
26
|
+
* `displayResponseToolName` if you mounted the say tool under a different
|
|
27
|
+
* identifier via [`displayResponse({ name })`](../../tool/src/display_response.ts:58).
|
|
28
|
+
*/
|
|
29
|
+
export type CollectAssistantTextOptions = {
|
|
30
|
+
/**
|
|
31
|
+
* Tool name to treat as the "final user-facing message" fallback.
|
|
32
|
+
* Default: `"display_response"`. Pass an array to recognise multiple
|
|
33
|
+
* aliases (e.g. a legacy `"agent_say"` deployment).
|
|
34
|
+
*/
|
|
35
|
+
readonly displayResponseToolName?: string | ReadonlyArray<string>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Stateful accumulator: feed it one [`AgentEvent`](./events.ts:1) at a
|
|
40
|
+
* time and call `text()` at the end to read the final assistant text.
|
|
41
|
+
*
|
|
42
|
+
* Designed for streaming use inside an `await…of` loop where the caller
|
|
43
|
+
* already has to iterate events for other reasons (UI streaming, logging,
|
|
44
|
+
* etc.). Use [`collectAssistantText`](./transcript.ts:1) for the simpler
|
|
45
|
+
* "given an array, give me the text" case.
|
|
46
|
+
*/
|
|
47
|
+
export type AssistantTextAccumulator = {
|
|
48
|
+
/** Apply one event to the accumulator. Safe to call with any AgentEvent. */
|
|
49
|
+
readonly push: (event: AgentEvent) => void
|
|
50
|
+
/**
|
|
51
|
+
* The final assistant text resolved with this precedence:
|
|
52
|
+
* 1. `text-delta` content (model produced plain prose).
|
|
53
|
+
* 2. `display_response` tool-call `input.response` (the escape hatch).
|
|
54
|
+
* 3. Empty string (model produced neither).
|
|
55
|
+
*/
|
|
56
|
+
readonly text: () => string
|
|
57
|
+
/**
|
|
58
|
+
* `true` once at least one `text-delta` event has been observed. Useful
|
|
59
|
+
* for callers that want to decide "did the model speak plainly OR did
|
|
60
|
+
* it fall back to display_response" without re-deriving from the text.
|
|
61
|
+
*/
|
|
62
|
+
readonly hasPlainText: () => boolean
|
|
63
|
+
/**
|
|
64
|
+
* The captured display_response fallback, if any. Distinct from `text()`
|
|
65
|
+
* so callers persisting structured transcripts can keep the two signals
|
|
66
|
+
* separate.
|
|
67
|
+
*/
|
|
68
|
+
readonly displayResponseMessage: () => string | undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const toNameSet = (n: string | ReadonlyArray<string> | undefined): ReadonlySet<string> => {
|
|
72
|
+
if (n === undefined) return new Set(["display_response"])
|
|
73
|
+
if (typeof n === "string") return new Set([n])
|
|
74
|
+
return new Set(n)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Create a stateful accumulator. Prefer this when consuming events as a
|
|
79
|
+
* stream; prefer [`collectAssistantText`](./transcript.ts:1) for arrays.
|
|
80
|
+
*/
|
|
81
|
+
export const makeAssistantTextAccumulator = (
|
|
82
|
+
options: CollectAssistantTextOptions = {},
|
|
83
|
+
): AssistantTextAccumulator => {
|
|
84
|
+
const names = toNameSet(options.displayResponseToolName)
|
|
85
|
+
let buffer = ""
|
|
86
|
+
let sayMessage: string | undefined
|
|
87
|
+
return {
|
|
88
|
+
push(event) {
|
|
89
|
+
if (event.type === "text-delta") {
|
|
90
|
+
buffer += event.text
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
if (event.type === "tool-call" && names.has(event.name)) {
|
|
94
|
+
const input = event.input as { response?: unknown } | null | undefined
|
|
95
|
+
const msg = input && typeof input.response === "string" ? input.response : undefined
|
|
96
|
+
if (msg && msg.length > 0) sayMessage = msg
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
text() {
|
|
100
|
+
if (buffer.length > 0) return buffer
|
|
101
|
+
return sayMessage ?? ""
|
|
102
|
+
},
|
|
103
|
+
hasPlainText() {
|
|
104
|
+
return buffer.length > 0
|
|
105
|
+
},
|
|
106
|
+
displayResponseMessage() {
|
|
107
|
+
return sayMessage
|
|
108
|
+
},
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Reduce a finite collection of events into the final assistant text.
|
|
114
|
+
*
|
|
115
|
+
* Equivalent to `events.forEach(acc.push); return acc.text()` — provided
|
|
116
|
+
* as a one-liner for the common "I already have the events array" case
|
|
117
|
+
* (tests, fully-buffered handlers, audit replay).
|
|
118
|
+
*/
|
|
119
|
+
export const collectAssistantText = (
|
|
120
|
+
events: Iterable<AgentEvent>,
|
|
121
|
+
options: CollectAssistantTextOptions = {},
|
|
122
|
+
): string => {
|
|
123
|
+
const acc = makeAssistantTextAccumulator(options)
|
|
124
|
+
for (const e of events) acc.push(e)
|
|
125
|
+
return acc.text()
|
|
126
|
+
}
|