@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,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public attachment shape forwarded from HTTP/UI → agent.run.
|
|
3
|
+
* MVP: image-only, base64 inline OR pre-uploaded URL.
|
|
4
|
+
* Validation enforced both client-side (chat-ui) and server-side (http-handlers).
|
|
5
|
+
*/
|
|
6
|
+
export type AttachmentInput = {
|
|
7
|
+
readonly type: "image"
|
|
8
|
+
readonly mediaType: string
|
|
9
|
+
/** Either `data` (raw base64, no `data:` prefix) OR `url` (http/https/file/data:). Exactly one. */
|
|
10
|
+
readonly data?: string
|
|
11
|
+
readonly url?: string
|
|
12
|
+
readonly filename?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type AttachmentErrorCode =
|
|
16
|
+
| "attachment_too_large"
|
|
17
|
+
| "attachment_unsupported_type"
|
|
18
|
+
| "attachment_invalid"
|
|
19
|
+
| "attachment_too_many"
|
|
20
|
+
|
|
21
|
+
export type ValidateAttachmentsConfig = {
|
|
22
|
+
readonly imageMimes?: ReadonlySet<string>
|
|
23
|
+
readonly maxBytesPerFile?: number
|
|
24
|
+
readonly maxBytesTotal?: number
|
|
25
|
+
readonly maxCount?: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ValidateOk = { readonly ok: true; readonly value: ReadonlyArray<AttachmentInput> }
|
|
29
|
+
export type ValidateErr = {
|
|
30
|
+
readonly ok: false
|
|
31
|
+
readonly code: AttachmentErrorCode
|
|
32
|
+
readonly message: string
|
|
33
|
+
}
|
|
34
|
+
export type ValidateResult = ValidateOk | ValidateErr
|
|
35
|
+
|
|
36
|
+
export const DEFAULT_IMAGE_MIMES: ReadonlySet<string> = new Set(["image/png", "image/jpeg", "image/webp", "image/gif"])
|
|
37
|
+
export const DEFAULT_MAX_BYTES_PER_FILE = 8 * 1024 * 1024
|
|
38
|
+
export const DEFAULT_MAX_BYTES_TOTAL = 16 * 1024 * 1024
|
|
39
|
+
export const DEFAULT_MAX_COUNT = 4
|
|
40
|
+
|
|
41
|
+
const base64ByteLength = (b64: string): number => {
|
|
42
|
+
// Strip optional data URL prefix `data:<mime>;base64,`.
|
|
43
|
+
const idx = b64.indexOf("base64,")
|
|
44
|
+
const body = idx >= 0 ? b64.slice(idx + "base64,".length) : b64
|
|
45
|
+
const padding = body.endsWith("==") ? 2 : body.endsWith("=") ? 1 : 0
|
|
46
|
+
return Math.max(0, Math.floor((body.length * 3) / 4) - padding)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const isHttpOrFile = (s: string) =>
|
|
50
|
+
s.startsWith("http://") || s.startsWith("https://") || s.startsWith("file://") || s.startsWith("data:")
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Validate raw attachments. Pure function — no I/O. First failure short-circuits.
|
|
54
|
+
*
|
|
55
|
+
* Order: count → per-item (mime, exactly-one-of, size) → total size.
|
|
56
|
+
*/
|
|
57
|
+
export const validateAttachments = (
|
|
58
|
+
raw: unknown,
|
|
59
|
+
config?: ValidateAttachmentsConfig,
|
|
60
|
+
): ValidateResult => {
|
|
61
|
+
if (raw === undefined || raw === null) return { ok: true, value: [] }
|
|
62
|
+
if (!Array.isArray(raw))
|
|
63
|
+
return { ok: false, code: "attachment_invalid", message: "attachments must be an array" }
|
|
64
|
+
|
|
65
|
+
const imageMimes = config?.imageMimes ?? DEFAULT_IMAGE_MIMES
|
|
66
|
+
const maxBytesPerFile = config?.maxBytesPerFile ?? DEFAULT_MAX_BYTES_PER_FILE
|
|
67
|
+
const maxBytesTotal = config?.maxBytesTotal ?? DEFAULT_MAX_BYTES_TOTAL
|
|
68
|
+
const maxCount = config?.maxCount ?? DEFAULT_MAX_COUNT
|
|
69
|
+
|
|
70
|
+
if (raw.length > maxCount)
|
|
71
|
+
return {
|
|
72
|
+
ok: false,
|
|
73
|
+
code: "attachment_too_many",
|
|
74
|
+
message: `Too many attachments (max ${maxCount}, received ${raw.length})`,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const out: AttachmentInput[] = []
|
|
78
|
+
let totalBytes = 0
|
|
79
|
+
|
|
80
|
+
for (let i = 0; i < raw.length; i++) {
|
|
81
|
+
const item = raw[i]
|
|
82
|
+
if (!item || typeof item !== "object")
|
|
83
|
+
return { ok: false, code: "attachment_invalid", message: `attachments[${i}] must be an object` }
|
|
84
|
+
|
|
85
|
+
const obj = item as Record<string, unknown>
|
|
86
|
+
if (obj.type !== "image")
|
|
87
|
+
return {
|
|
88
|
+
ok: false,
|
|
89
|
+
code: "attachment_unsupported_type",
|
|
90
|
+
message: `attachments[${i}].type must be "image" (received ${String(obj.type)})`,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const mediaType = obj.mediaType
|
|
94
|
+
if (typeof mediaType !== "string" || mediaType.length === 0)
|
|
95
|
+
return { ok: false, code: "attachment_invalid", message: `attachments[${i}].mediaType is required` }
|
|
96
|
+
if (!imageMimes.has(mediaType))
|
|
97
|
+
return {
|
|
98
|
+
ok: false,
|
|
99
|
+
code: "attachment_unsupported_type",
|
|
100
|
+
message: `Unsupported attachment type: ${mediaType}`,
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const data = typeof obj.data === "string" ? obj.data : undefined
|
|
104
|
+
const url = typeof obj.url === "string" ? obj.url : undefined
|
|
105
|
+
|
|
106
|
+
if (!data && !url)
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
code: "attachment_invalid",
|
|
110
|
+
message: `attachments[${i}] requires either "data" (base64) or "url"`,
|
|
111
|
+
}
|
|
112
|
+
if (data && url)
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
code: "attachment_invalid",
|
|
116
|
+
message: `attachments[${i}] must not set both "data" and "url"`,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (url && !isHttpOrFile(url))
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
code: "attachment_invalid",
|
|
123
|
+
message: `attachments[${i}].url must be http(s)/file/data scheme`,
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (data) {
|
|
127
|
+
const bytes = base64ByteLength(data)
|
|
128
|
+
if (bytes === 0)
|
|
129
|
+
return { ok: false, code: "attachment_invalid", message: `attachments[${i}].data is empty or malformed` }
|
|
130
|
+
if (bytes > maxBytesPerFile)
|
|
131
|
+
return {
|
|
132
|
+
ok: false,
|
|
133
|
+
code: "attachment_too_large",
|
|
134
|
+
message: `Attachment ${obj.filename ?? `#${i}`} exceeds ${maxBytesPerFile} bytes (got ${bytes})`,
|
|
135
|
+
}
|
|
136
|
+
totalBytes += bytes
|
|
137
|
+
if (totalBytes > maxBytesTotal)
|
|
138
|
+
return {
|
|
139
|
+
ok: false,
|
|
140
|
+
code: "attachment_too_large",
|
|
141
|
+
message: `Total attachment bytes exceed ${maxBytesTotal} (got ${totalBytes})`,
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
out.push({
|
|
146
|
+
type: "image",
|
|
147
|
+
mediaType,
|
|
148
|
+
data,
|
|
149
|
+
url,
|
|
150
|
+
filename: typeof obj.filename === "string" ? obj.filename : undefined,
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return { ok: true, value: out }
|
|
155
|
+
}
|
package/src/bus/bus.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Effect, PubSub, Stream } from "effect"
|
|
2
|
+
import type { Message, SessionID } from "../session/schema"
|
|
3
|
+
import type { AgentEvent } from "../events"
|
|
4
|
+
|
|
5
|
+
// Bus event union — type-tagged for filtering
|
|
6
|
+
export type SessionStartedEvent = {
|
|
7
|
+
readonly type: "session-started"
|
|
8
|
+
readonly sessionId: SessionID | string
|
|
9
|
+
readonly agentName: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type SessionMessageEvent = {
|
|
13
|
+
readonly type: "session-message"
|
|
14
|
+
readonly sessionId: SessionID | string
|
|
15
|
+
readonly message: Message
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type SessionIdleEvent = {
|
|
19
|
+
readonly type: "session-idle"
|
|
20
|
+
readonly sessionId: SessionID | string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type ToolExecutingEvent = {
|
|
24
|
+
readonly type: "tool-executing"
|
|
25
|
+
readonly sessionId: SessionID | string
|
|
26
|
+
readonly callID: string
|
|
27
|
+
readonly name: string
|
|
28
|
+
readonly input: unknown
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ToolCompletedEvent = {
|
|
32
|
+
readonly type: "tool-completed"
|
|
33
|
+
readonly sessionId: SessionID | string
|
|
34
|
+
readonly callID: string
|
|
35
|
+
readonly name: string
|
|
36
|
+
readonly output: unknown
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type AgentEventBusEvent = {
|
|
40
|
+
readonly type: "agent-event"
|
|
41
|
+
readonly sessionId: SessionID | string
|
|
42
|
+
readonly event: AgentEvent
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type BusEvent =
|
|
46
|
+
| SessionStartedEvent
|
|
47
|
+
| SessionMessageEvent
|
|
48
|
+
| SessionIdleEvent
|
|
49
|
+
| ToolExecutingEvent
|
|
50
|
+
| ToolCompletedEvent
|
|
51
|
+
| AgentEventBusEvent
|
|
52
|
+
|
|
53
|
+
export type Bus = {
|
|
54
|
+
readonly publish: (event: BusEvent) => Effect.Effect<void>
|
|
55
|
+
readonly subscribe: <K extends BusEvent["type"]>(
|
|
56
|
+
type: K,
|
|
57
|
+
) => Stream.Stream<Extract<BusEvent, { type: K }>>
|
|
58
|
+
readonly subscribeAll: () => Stream.Stream<BusEvent>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const makeBus = (): Bus => {
|
|
62
|
+
const pubsubEffect = PubSub.unbounded<BusEvent>()
|
|
63
|
+
let cached: PubSub.PubSub<BusEvent> | undefined
|
|
64
|
+
|
|
65
|
+
const get = (): Effect.Effect<PubSub.PubSub<BusEvent>> => {
|
|
66
|
+
if (cached) return Effect.succeed(cached)
|
|
67
|
+
return pubsubEffect.pipe(
|
|
68
|
+
Effect.tap((p) => {
|
|
69
|
+
cached = p
|
|
70
|
+
return Effect.void
|
|
71
|
+
}),
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const publish = (event: BusEvent): Effect.Effect<void> =>
|
|
76
|
+
Effect.flatMap(get(), (pubsub) => Effect.asVoid(PubSub.publish(pubsub, event)))
|
|
77
|
+
|
|
78
|
+
const subscribeAll = (): Stream.Stream<BusEvent> =>
|
|
79
|
+
Stream.unwrap(Effect.map(get(), (pubsub) => Stream.fromPubSub(pubsub)))
|
|
80
|
+
|
|
81
|
+
const subscribe = <K extends BusEvent["type"]>(
|
|
82
|
+
type: K,
|
|
83
|
+
): Stream.Stream<Extract<BusEvent, { type: K }>> =>
|
|
84
|
+
subscribeAll().pipe(
|
|
85
|
+
Stream.filter((e): e is Extract<BusEvent, { type: K }> => e.type === type),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
return { publish, subscribe, subscribeAll }
|
|
89
|
+
}
|
package/src/bus/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./bus"
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Tool, ToolFailure, type ToolSchema } from "@tinyweb-agent-framework/llm"
|
|
2
|
+
import { Effect, Schema } from "effect"
|
|
3
|
+
|
|
4
|
+
export type ToolExecuteAsync<Params, Success> = (input: Params) => Promise<Success>
|
|
5
|
+
export type ToolExecuteEffect<Params, Success> = (
|
|
6
|
+
input: Params,
|
|
7
|
+
) => Effect.Effect<Success, ToolFailure>
|
|
8
|
+
|
|
9
|
+
export type DefineToolConfig<P extends ToolSchema<any>, S extends ToolSchema<any>> = {
|
|
10
|
+
readonly name?: string
|
|
11
|
+
readonly description: string
|
|
12
|
+
readonly parameters: P
|
|
13
|
+
readonly success?: S
|
|
14
|
+
readonly execute:
|
|
15
|
+
| ToolExecuteAsync<Schema.Schema.Type<P>, Schema.Schema.Type<S>>
|
|
16
|
+
| ToolExecuteEffect<Schema.Schema.Type<P>, Schema.Schema.Type<S>>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const wrapExecute = <Params, Success>(
|
|
20
|
+
execute: ToolExecuteAsync<Params, Success> | ToolExecuteEffect<Params, Success>,
|
|
21
|
+
) =>
|
|
22
|
+
(input: Params): Effect.Effect<Success, ToolFailure> => {
|
|
23
|
+
const result = execute(input)
|
|
24
|
+
if (Effect.isEffect(result)) return result as Effect.Effect<Success, ToolFailure>
|
|
25
|
+
return Effect.tryPromise({
|
|
26
|
+
try: () => result as Promise<Success>,
|
|
27
|
+
catch: (cause) => new ToolFailure({ message: cause instanceof Error ? cause.message : String(cause) }),
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function defineTool<P extends ToolSchema<any>, S extends ToolSchema<any> = typeof Schema.Unknown>(
|
|
32
|
+
config: DefineToolConfig<P, S>,
|
|
33
|
+
) {
|
|
34
|
+
const success = (config.success ?? Schema.Unknown) as S
|
|
35
|
+
return Tool.make({
|
|
36
|
+
description: config.description,
|
|
37
|
+
parameters: config.parameters,
|
|
38
|
+
success,
|
|
39
|
+
execute: wrapExecute(config.execute),
|
|
40
|
+
})
|
|
41
|
+
}
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { LLMEvent } from "@tinyweb-agent-framework/llm"
|
|
2
|
+
|
|
3
|
+
export type TextDeltaEvent = {
|
|
4
|
+
readonly type: "text-delta"
|
|
5
|
+
readonly id: string
|
|
6
|
+
readonly text: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type TextEndEvent = {
|
|
10
|
+
readonly type: "text-end"
|
|
11
|
+
readonly id: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type ReasoningDeltaEvent = {
|
|
15
|
+
readonly type: "reasoning-delta"
|
|
16
|
+
readonly id: string
|
|
17
|
+
readonly text: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ToolCallEvent = {
|
|
21
|
+
readonly type: "tool-call"
|
|
22
|
+
readonly id: string
|
|
23
|
+
readonly name: string
|
|
24
|
+
readonly input: unknown
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type ToolResultEvent = {
|
|
28
|
+
readonly type: "tool-result"
|
|
29
|
+
readonly id: string
|
|
30
|
+
readonly name: string
|
|
31
|
+
readonly result: unknown
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type ToolErrorEvent = {
|
|
35
|
+
readonly type: "tool-error"
|
|
36
|
+
readonly id: string
|
|
37
|
+
readonly name: string
|
|
38
|
+
readonly message: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type StepFinishEvent = {
|
|
42
|
+
readonly type: "step-finish"
|
|
43
|
+
readonly index: number
|
|
44
|
+
readonly reason: string
|
|
45
|
+
readonly usage?: unknown
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type FinishEvent = {
|
|
49
|
+
readonly type: "finish"
|
|
50
|
+
readonly reason: string
|
|
51
|
+
readonly usage?: unknown
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type ErrorEvent = {
|
|
55
|
+
readonly type: "error"
|
|
56
|
+
readonly message: string
|
|
57
|
+
readonly retryable?: boolean
|
|
58
|
+
/**
|
|
59
|
+
* Optional structured code for client-side branching (e.g.
|
|
60
|
+
* `"attachment_too_large"`, `"attachment_unsupported_type"`).
|
|
61
|
+
* Free-form string — consumers should treat unknown codes as generic errors.
|
|
62
|
+
*/
|
|
63
|
+
readonly code?: string
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Source of an auto-compaction (context-condense) pass.
|
|
68
|
+
*
|
|
69
|
+
* - `preflight` — the agent estimated the upcoming prompt would exceed the
|
|
70
|
+
* usable budget and ran `compact()` BEFORE the provider call. Cheaper and
|
|
71
|
+
* more controllable.
|
|
72
|
+
* - `overflow-retry` — the provider returned `ContextOverflowReason`; the
|
|
73
|
+
* agent ran `compact()` and retried the same turn once. Surfaced so UIs
|
|
74
|
+
* can show "context overflow — recovering…" copy distinct from a regular
|
|
75
|
+
* pre-flight condense.
|
|
76
|
+
* - `manual` — RESERVED. Not emitted yet (no manual trigger exists), but
|
|
77
|
+
* reserved on the discriminated union so adding a "Condense Now" button
|
|
78
|
+
* later doesn't break consumers' exhaustive switches.
|
|
79
|
+
*/
|
|
80
|
+
export type CondenseTrigger = "preflight" | "overflow-retry" | "manual"
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Emitted IMMEDIATELY before `compact()` runs. UI consumers should render a
|
|
84
|
+
* spinner/pill — compaction can take 2–5s on a long thread because it makes
|
|
85
|
+
* its own LLM call.
|
|
86
|
+
*/
|
|
87
|
+
export type ContextCondenseStartEvent = {
|
|
88
|
+
readonly type: "context-condense-start"
|
|
89
|
+
readonly trigger: CondenseTrigger
|
|
90
|
+
/** Estimated prompt tokens the agent would have sent without compacting. */
|
|
91
|
+
readonly estimatedTokens?: number
|
|
92
|
+
/** The usable budget (`limits.context − reserved`) that the estimate exceeded. */
|
|
93
|
+
readonly budgetTokens?: number
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Emitted after a SUCCESSFUL compaction. Carries the summariser's output plus
|
|
98
|
+
* before/after token counts and (when the route exposes it) the call cost.
|
|
99
|
+
* This is the persistent, replay-friendly record of what happened.
|
|
100
|
+
*/
|
|
101
|
+
export type ContextCondenseEvent = {
|
|
102
|
+
readonly type: "context-condense"
|
|
103
|
+
readonly trigger: CondenseTrigger
|
|
104
|
+
/** Compaction summary text — already truncated if the summariser overflowed. */
|
|
105
|
+
readonly summary: string
|
|
106
|
+
/** ID of the persisted compaction marker user message — cross-references `sessions.messages(...)`. */
|
|
107
|
+
readonly markerId: string
|
|
108
|
+
/** Approximate input tokens BEFORE compaction (estimateTokens of the head slice). */
|
|
109
|
+
readonly prevTokens?: number
|
|
110
|
+
/** Approximate input tokens AFTER compaction (estimateTokens of `[marker, ...tail]`). */
|
|
111
|
+
readonly newTokens?: number
|
|
112
|
+
/** USD cost of the summariser LLM call when the route surfaces it. */
|
|
113
|
+
readonly cost?: number
|
|
114
|
+
/** Model id used for the summariser (today: same as the main agent model). */
|
|
115
|
+
readonly modelId?: string
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Emitted when `compact()` itself errored or, on the `overflow-retry` path,
|
|
120
|
+
* when the post-compaction retry still overflowed (`retryExhausted: true`).
|
|
121
|
+
*
|
|
122
|
+
* Distinct from the generic `error` event so UIs can render it inline next
|
|
123
|
+
* to the condense spinner without tearing down the assistant turn.
|
|
124
|
+
*/
|
|
125
|
+
export type ContextCondenseErrorEvent = {
|
|
126
|
+
readonly type: "context-condense-error"
|
|
127
|
+
readonly trigger: CondenseTrigger
|
|
128
|
+
readonly message: string
|
|
129
|
+
/** True ONLY on the second consecutive overflow — replaces the old "compaction retry exhausted" message. */
|
|
130
|
+
readonly retryExhausted?: boolean
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export type AgentEvent =
|
|
134
|
+
| TextDeltaEvent
|
|
135
|
+
| TextEndEvent
|
|
136
|
+
| ReasoningDeltaEvent
|
|
137
|
+
| ToolCallEvent
|
|
138
|
+
| ToolResultEvent
|
|
139
|
+
| ToolErrorEvent
|
|
140
|
+
| StepFinishEvent
|
|
141
|
+
| FinishEvent
|
|
142
|
+
| ErrorEvent
|
|
143
|
+
| ContextCondenseStartEvent
|
|
144
|
+
| ContextCondenseEvent
|
|
145
|
+
| ContextCondenseErrorEvent
|
|
146
|
+
|
|
147
|
+
export const fromLLMEvent = (event: LLMEvent): AgentEvent | undefined => {
|
|
148
|
+
if (event.type === "text-delta") return { type: "text-delta", id: event.id, text: event.text }
|
|
149
|
+
if (event.type === "text-end") return { type: "text-end", id: event.id }
|
|
150
|
+
if (event.type === "reasoning-delta") return { type: "reasoning-delta", id: event.id, text: event.text }
|
|
151
|
+
if (event.type === "tool-call") return { type: "tool-call", id: event.id, name: event.name, input: event.input }
|
|
152
|
+
if (event.type === "tool-result")
|
|
153
|
+
return { type: "tool-result", id: event.id, name: event.name, result: event.result.value }
|
|
154
|
+
if (event.type === "tool-error")
|
|
155
|
+
return { type: "tool-error", id: event.id, name: event.name, message: event.message }
|
|
156
|
+
if (event.type === "step-finish")
|
|
157
|
+
return { type: "step-finish", index: event.index, reason: event.reason, usage: event.usage }
|
|
158
|
+
if (event.type === "request-finish") return { type: "finish", reason: event.reason, usage: event.usage }
|
|
159
|
+
if (event.type === "provider-error")
|
|
160
|
+
return { type: "error", message: event.message, retryable: event.retryable }
|
|
161
|
+
return undefined
|
|
162
|
+
}
|