@link-assistant/agent 0.0.8

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 (133) hide show
  1. package/EXAMPLES.md +383 -0
  2. package/LICENSE +24 -0
  3. package/MODELS.md +95 -0
  4. package/README.md +388 -0
  5. package/TOOLS.md +134 -0
  6. package/package.json +89 -0
  7. package/src/agent/agent.ts +150 -0
  8. package/src/agent/generate.txt +75 -0
  9. package/src/auth/index.ts +64 -0
  10. package/src/bun/index.ts +96 -0
  11. package/src/bus/global.ts +10 -0
  12. package/src/bus/index.ts +119 -0
  13. package/src/cli/bootstrap.js +41 -0
  14. package/src/cli/bootstrap.ts +17 -0
  15. package/src/cli/cmd/agent.ts +165 -0
  16. package/src/cli/cmd/cmd.ts +5 -0
  17. package/src/cli/cmd/export.ts +88 -0
  18. package/src/cli/cmd/mcp.ts +80 -0
  19. package/src/cli/cmd/models.ts +58 -0
  20. package/src/cli/cmd/run.ts +359 -0
  21. package/src/cli/cmd/stats.ts +276 -0
  22. package/src/cli/error.ts +27 -0
  23. package/src/command/index.ts +73 -0
  24. package/src/command/template/initialize.txt +10 -0
  25. package/src/config/config.ts +705 -0
  26. package/src/config/markdown.ts +41 -0
  27. package/src/file/ripgrep.ts +391 -0
  28. package/src/file/time.ts +38 -0
  29. package/src/file/watcher.ts +75 -0
  30. package/src/file.ts +6 -0
  31. package/src/flag/flag.ts +19 -0
  32. package/src/format/formatter.ts +248 -0
  33. package/src/format/index.ts +137 -0
  34. package/src/global/index.ts +52 -0
  35. package/src/id/id.ts +72 -0
  36. package/src/index.js +371 -0
  37. package/src/mcp/index.ts +289 -0
  38. package/src/patch/index.ts +622 -0
  39. package/src/project/bootstrap.ts +22 -0
  40. package/src/project/instance.ts +67 -0
  41. package/src/project/project.ts +105 -0
  42. package/src/project/state.ts +65 -0
  43. package/src/provider/models-macro.ts +11 -0
  44. package/src/provider/models.ts +98 -0
  45. package/src/provider/opencode.js +47 -0
  46. package/src/provider/provider.ts +636 -0
  47. package/src/provider/transform.ts +241 -0
  48. package/src/server/project.ts +48 -0
  49. package/src/server/server.ts +249 -0
  50. package/src/session/agent.js +204 -0
  51. package/src/session/compaction.ts +249 -0
  52. package/src/session/index.ts +380 -0
  53. package/src/session/message-v2.ts +758 -0
  54. package/src/session/message.ts +189 -0
  55. package/src/session/processor.ts +356 -0
  56. package/src/session/prompt/anthropic-20250930.txt +166 -0
  57. package/src/session/prompt/anthropic.txt +105 -0
  58. package/src/session/prompt/anthropic_spoof.txt +1 -0
  59. package/src/session/prompt/beast.txt +147 -0
  60. package/src/session/prompt/build-switch.txt +5 -0
  61. package/src/session/prompt/codex.txt +318 -0
  62. package/src/session/prompt/copilot-gpt-5.txt +143 -0
  63. package/src/session/prompt/gemini.txt +155 -0
  64. package/src/session/prompt/grok-code.txt +1 -0
  65. package/src/session/prompt/plan.txt +8 -0
  66. package/src/session/prompt/polaris.txt +107 -0
  67. package/src/session/prompt/qwen.txt +109 -0
  68. package/src/session/prompt/summarize-turn.txt +5 -0
  69. package/src/session/prompt/summarize.txt +10 -0
  70. package/src/session/prompt/title.txt +25 -0
  71. package/src/session/prompt.ts +1390 -0
  72. package/src/session/retry.ts +53 -0
  73. package/src/session/revert.ts +108 -0
  74. package/src/session/status.ts +75 -0
  75. package/src/session/summary.ts +179 -0
  76. package/src/session/system.ts +138 -0
  77. package/src/session/todo.ts +36 -0
  78. package/src/snapshot/index.ts +197 -0
  79. package/src/storage/storage.ts +226 -0
  80. package/src/tool/bash.ts +193 -0
  81. package/src/tool/bash.txt +121 -0
  82. package/src/tool/batch.ts +173 -0
  83. package/src/tool/batch.txt +28 -0
  84. package/src/tool/codesearch.ts +123 -0
  85. package/src/tool/codesearch.txt +12 -0
  86. package/src/tool/edit.ts +604 -0
  87. package/src/tool/edit.txt +10 -0
  88. package/src/tool/glob.ts +65 -0
  89. package/src/tool/glob.txt +6 -0
  90. package/src/tool/grep.ts +116 -0
  91. package/src/tool/grep.txt +8 -0
  92. package/src/tool/invalid.ts +17 -0
  93. package/src/tool/ls.ts +110 -0
  94. package/src/tool/ls.txt +1 -0
  95. package/src/tool/multiedit.ts +46 -0
  96. package/src/tool/multiedit.txt +41 -0
  97. package/src/tool/patch.ts +188 -0
  98. package/src/tool/patch.txt +1 -0
  99. package/src/tool/read.ts +201 -0
  100. package/src/tool/read.txt +12 -0
  101. package/src/tool/registry.ts +87 -0
  102. package/src/tool/task.ts +126 -0
  103. package/src/tool/task.txt +60 -0
  104. package/src/tool/todo.ts +39 -0
  105. package/src/tool/todoread.txt +14 -0
  106. package/src/tool/todowrite.txt +167 -0
  107. package/src/tool/tool.ts +66 -0
  108. package/src/tool/webfetch.ts +171 -0
  109. package/src/tool/webfetch.txt +14 -0
  110. package/src/tool/websearch.ts +133 -0
  111. package/src/tool/websearch.txt +11 -0
  112. package/src/tool/write.ts +33 -0
  113. package/src/tool/write.txt +8 -0
  114. package/src/util/binary.ts +41 -0
  115. package/src/util/context.ts +25 -0
  116. package/src/util/defer.ts +12 -0
  117. package/src/util/error.ts +54 -0
  118. package/src/util/eventloop.ts +20 -0
  119. package/src/util/filesystem.ts +69 -0
  120. package/src/util/fn.ts +11 -0
  121. package/src/util/iife.ts +3 -0
  122. package/src/util/keybind.ts +79 -0
  123. package/src/util/lazy.ts +11 -0
  124. package/src/util/locale.ts +39 -0
  125. package/src/util/lock.ts +98 -0
  126. package/src/util/log.ts +177 -0
  127. package/src/util/queue.ts +19 -0
  128. package/src/util/rpc.ts +42 -0
  129. package/src/util/scrap.ts +10 -0
  130. package/src/util/signal.ts +12 -0
  131. package/src/util/timeout.ts +14 -0
  132. package/src/util/token.ts +7 -0
  133. package/src/util/wildcard.ts +54 -0
@@ -0,0 +1,380 @@
1
+ import { Decimal } from "decimal.js"
2
+ import z from "zod"
3
+ import { type LanguageModelUsage, type ProviderMetadata } from "ai"
4
+ import { Bus } from "../bus"
5
+ import { Config } from "../config/config"
6
+ import { Flag } from "../flag/flag"
7
+ import { Identifier } from "../id/id"
8
+ import type { ModelsDev } from "../provider/models"
9
+ import { Storage } from "../storage/storage"
10
+ import { Log } from "../util/log"
11
+ import { MessageV2 } from "./message-v2"
12
+ import { Instance } from "../project/instance"
13
+ import { SessionPrompt } from "./prompt"
14
+ import { fn } from "../util/fn"
15
+ import { Command } from "../command"
16
+ import { Snapshot } from "../snapshot"
17
+
18
+ export namespace Session {
19
+ const log = Log.create({ service: "session" })
20
+
21
+ const parentTitlePrefix = "New session - "
22
+ const childTitlePrefix = "Child session - "
23
+
24
+ function createDefaultTitle(isChild = false) {
25
+ return (isChild ? childTitlePrefix : parentTitlePrefix) + new Date().toISOString()
26
+ }
27
+
28
+ export function isDefaultTitle(title: string) {
29
+ return new RegExp(
30
+ `^(${parentTitlePrefix}|${childTitlePrefix})\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$`,
31
+ ).test(title)
32
+ }
33
+
34
+ export const Info = z
35
+ .object({
36
+ id: Identifier.schema("session"),
37
+ projectID: z.string(),
38
+ directory: z.string(),
39
+ parentID: Identifier.schema("session").optional(),
40
+ summary: z
41
+ .object({
42
+ additions: z.number(),
43
+ deletions: z.number(),
44
+ files: z.number(),
45
+ diffs: Snapshot.FileDiff.array().optional(),
46
+ })
47
+ .optional(),
48
+ // share field removed - no sharing support
49
+ title: z.string(),
50
+ version: z.string(),
51
+ time: z.object({
52
+ created: z.number(),
53
+ updated: z.number(),
54
+ compacting: z.number().optional(),
55
+ }),
56
+ revert: z
57
+ .object({
58
+ messageID: z.string(),
59
+ partID: z.string().optional(),
60
+ snapshot: z.string().optional(),
61
+ diff: z.string().optional(),
62
+ })
63
+ .optional(),
64
+ })
65
+ .meta({
66
+ ref: "Session",
67
+ })
68
+ export type Info = z.output<typeof Info>
69
+
70
+ // ShareInfo removed - share not supported
71
+
72
+ export const Event = {
73
+ Created: Bus.event(
74
+ "session.created",
75
+ z.object({
76
+ info: Info,
77
+ }),
78
+ ),
79
+ Updated: Bus.event(
80
+ "session.updated",
81
+ z.object({
82
+ info: Info,
83
+ }),
84
+ ),
85
+ Deleted: Bus.event(
86
+ "session.deleted",
87
+ z.object({
88
+ info: Info,
89
+ }),
90
+ ),
91
+ Diff: Bus.event(
92
+ "session.diff",
93
+ z.object({
94
+ sessionID: z.string(),
95
+ diff: Snapshot.FileDiff.array(),
96
+ }),
97
+ ),
98
+ Error: Bus.event(
99
+ "session.error",
100
+ z.object({
101
+ sessionID: z.string().optional(),
102
+ error: MessageV2.Assistant.shape.error,
103
+ }),
104
+ ),
105
+ }
106
+
107
+ export const create = fn(
108
+ z
109
+ .object({
110
+ parentID: Identifier.schema("session").optional(),
111
+ title: z.string().optional(),
112
+ })
113
+ .optional(),
114
+ async (input) => {
115
+ return createNext({
116
+ parentID: input?.parentID,
117
+ directory: Instance.directory,
118
+ title: input?.title,
119
+ })
120
+ },
121
+ )
122
+
123
+ export const fork = fn(
124
+ z.object({
125
+ sessionID: Identifier.schema("session"),
126
+ messageID: Identifier.schema("message").optional(),
127
+ }),
128
+ async (input) => {
129
+ const session = await createNext({
130
+ directory: Instance.directory,
131
+ })
132
+ const msgs = await messages({ sessionID: input.sessionID })
133
+ for (const msg of msgs) {
134
+ if (input.messageID && msg.info.id >= input.messageID) break
135
+ const cloned = await updateMessage({
136
+ ...msg.info,
137
+ sessionID: session.id,
138
+ id: Identifier.ascending("message"),
139
+ })
140
+
141
+ for (const part of msg.parts) {
142
+ await updatePart({
143
+ ...part,
144
+ id: Identifier.ascending("part"),
145
+ messageID: cloned.id,
146
+ sessionID: session.id,
147
+ })
148
+ }
149
+ }
150
+ return session
151
+ },
152
+ )
153
+
154
+ export const touch = fn(Identifier.schema("session"), async (sessionID) => {
155
+ await update(sessionID, (draft) => {
156
+ draft.time.updated = Date.now()
157
+ })
158
+ })
159
+
160
+ export async function createNext(input: { id?: string; title?: string; parentID?: string; directory: string }) {
161
+ const result: Info = {
162
+ id: Identifier.descending("session", input.id),
163
+ version: "agent-cli-1.0.0",
164
+ projectID: Instance.project.id,
165
+ directory: input.directory,
166
+ parentID: input.parentID,
167
+ title: input.title ?? createDefaultTitle(!!input.parentID),
168
+ time: {
169
+ created: Date.now(),
170
+ updated: Date.now(),
171
+ },
172
+ }
173
+ log.info("created", result)
174
+ await Storage.write(["session", Instance.project.id, result.id], result)
175
+ Bus.publish(Event.Created, {
176
+ info: result,
177
+ })
178
+ // Share not supported - removed auto-sharing
179
+ Bus.publish(Event.Updated, {
180
+ info: result,
181
+ })
182
+ return result
183
+ }
184
+
185
+ export const get = fn(Identifier.schema("session"), async (id) => {
186
+ const read = await Storage.read<Info>(["session", Instance.project.id, id])
187
+ return read as Info
188
+ })
189
+
190
+ // getShare, share, unshare removed - share not supported
191
+
192
+ export async function update(id: string, editor: (session: Info) => void) {
193
+ const project = Instance.project
194
+ const result = await Storage.update<Info>(["session", project.id, id], (draft) => {
195
+ editor(draft)
196
+ draft.time.updated = Date.now()
197
+ })
198
+ Bus.publish(Event.Updated, {
199
+ info: result,
200
+ })
201
+ return result
202
+ }
203
+
204
+ export const diff = fn(Identifier.schema("session"), async (sessionID) => {
205
+ const diffs = await Storage.read<Snapshot.FileDiff[]>(["session_diff", sessionID])
206
+ return diffs ?? []
207
+ })
208
+
209
+ export const messages = fn(
210
+ z.object({
211
+ sessionID: Identifier.schema("session"),
212
+ limit: z.number().optional(),
213
+ }),
214
+ async (input) => {
215
+ const result = [] as MessageV2.WithParts[]
216
+ for await (const msg of MessageV2.stream(input.sessionID)) {
217
+ if (input.limit && result.length >= input.limit) break
218
+ result.push(msg)
219
+ }
220
+ result.reverse()
221
+ return result
222
+ },
223
+ )
224
+
225
+ export async function* list() {
226
+ const project = Instance.project
227
+ for (const item of await Storage.list(["session", project.id])) {
228
+ yield Storage.read<Info>(item)
229
+ }
230
+ }
231
+
232
+ export const children = fn(Identifier.schema("session"), async (parentID) => {
233
+ const project = Instance.project
234
+ const result = [] as Session.Info[]
235
+ for (const item of await Storage.list(["session", project.id])) {
236
+ const session = await Storage.read<Info>(item)
237
+ if (session.parentID !== parentID) continue
238
+ result.push(session)
239
+ }
240
+ return result
241
+ })
242
+
243
+ export const remove = fn(Identifier.schema("session"), async (sessionID) => {
244
+ const project = Instance.project
245
+ try {
246
+ const session = await get(sessionID)
247
+ for (const child of await children(sessionID)) {
248
+ await remove(child.id)
249
+ }
250
+ // unshare removed - share not supported
251
+ for (const msg of await Storage.list(["message", sessionID])) {
252
+ for (const part of await Storage.list(["part", msg.at(-1)!])) {
253
+ await Storage.remove(part)
254
+ }
255
+ await Storage.remove(msg)
256
+ }
257
+ await Storage.remove(["session", project.id, sessionID])
258
+ Bus.publish(Event.Deleted, {
259
+ info: session,
260
+ })
261
+ } catch (e) {
262
+ log.error(e)
263
+ }
264
+ })
265
+
266
+ export const updateMessage = fn(MessageV2.Info, async (msg) => {
267
+ await Storage.write(["message", msg.sessionID, msg.id], msg)
268
+ Bus.publish(MessageV2.Event.Updated, {
269
+ info: msg,
270
+ })
271
+ return msg
272
+ })
273
+
274
+ export const removeMessage = fn(
275
+ z.object({
276
+ sessionID: Identifier.schema("session"),
277
+ messageID: Identifier.schema("message"),
278
+ }),
279
+ async (input) => {
280
+ await Storage.remove(["message", input.sessionID, input.messageID])
281
+ Bus.publish(MessageV2.Event.Removed, {
282
+ sessionID: input.sessionID,
283
+ messageID: input.messageID,
284
+ })
285
+ return input.messageID
286
+ },
287
+ )
288
+
289
+ const UpdatePartInput = z.union([
290
+ MessageV2.Part,
291
+ z.object({
292
+ part: MessageV2.TextPart,
293
+ delta: z.string(),
294
+ }),
295
+ z.object({
296
+ part: MessageV2.ReasoningPart,
297
+ delta: z.string(),
298
+ }),
299
+ ])
300
+
301
+ export const updatePart = fn(UpdatePartInput, async (input) => {
302
+ const part = "delta" in input ? input.part : input
303
+ const delta = "delta" in input ? input.delta : undefined
304
+ await Storage.write(["part", part.messageID, part.id], part)
305
+ Bus.publish(MessageV2.Event.PartUpdated, {
306
+ part,
307
+ delta,
308
+ })
309
+ return part
310
+ })
311
+
312
+ export const getUsage = fn(
313
+ z.object({
314
+ model: z.custom<ModelsDev.Model>(),
315
+ usage: z.custom<LanguageModelUsage>(),
316
+ metadata: z.custom<ProviderMetadata>().optional(),
317
+ }),
318
+ (input) => {
319
+ const cachedInputTokens = input.usage.cachedInputTokens ?? 0
320
+ const excludesCachedTokens = !!(input.metadata?.["anthropic"] || input.metadata?.["bedrock"])
321
+ const adjustedInputTokens = excludesCachedTokens
322
+ ? (input.usage.inputTokens ?? 0)
323
+ : (input.usage.inputTokens ?? 0) - cachedInputTokens
324
+
325
+ const tokens = {
326
+ input: adjustedInputTokens,
327
+ output: input.usage.outputTokens ?? 0,
328
+ reasoning: input.usage?.reasoningTokens ?? 0,
329
+ cache: {
330
+ write: (input.metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
331
+ // @ts-expect-error
332
+ input.metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
333
+ 0) as number,
334
+ read: cachedInputTokens,
335
+ },
336
+ }
337
+
338
+ const costInfo =
339
+ input.model.cost?.context_over_200k && tokens.input + tokens.cache.read > 200_000
340
+ ? input.model.cost.context_over_200k
341
+ : input.model.cost
342
+ return {
343
+ cost: new Decimal(0)
344
+ .add(new Decimal(tokens.input).mul(costInfo?.input ?? 0).div(1_000_000))
345
+ .add(new Decimal(tokens.output).mul(costInfo?.output ?? 0).div(1_000_000))
346
+ .add(new Decimal(tokens.cache.read).mul(costInfo?.cache_read ?? 0).div(1_000_000))
347
+ .add(new Decimal(tokens.cache.write).mul(costInfo?.cache_write ?? 0).div(1_000_000))
348
+ // TODO: update models.dev to have better pricing model, for now:
349
+ // charge reasoning tokens at the same rate as output tokens
350
+ .add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
351
+ .toNumber(),
352
+ tokens,
353
+ }
354
+ },
355
+ )
356
+
357
+ export class BusyError extends Error {
358
+ constructor(public readonly sessionID: string) {
359
+ super(`Session ${sessionID} is busy`)
360
+ }
361
+ }
362
+
363
+ export const initialize = fn(
364
+ z.object({
365
+ sessionID: Identifier.schema("session"),
366
+ modelID: z.string(),
367
+ providerID: z.string(),
368
+ messageID: Identifier.schema("message"),
369
+ }),
370
+ async (input) => {
371
+ await SessionPrompt.command({
372
+ sessionID: input.sessionID,
373
+ messageID: input.messageID,
374
+ model: input.providerID + "/" + input.modelID,
375
+ command: Command.Default.INIT,
376
+ arguments: "",
377
+ })
378
+ },
379
+ )
380
+ }