notsleep 1.0.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 (93) hide show
  1. package/.env.example +117 -0
  2. package/LICENSE +21 -0
  3. package/README.md +341 -0
  4. package/dashboard/index.html +893 -0
  5. package/dist/agent.d.ts +162 -0
  6. package/dist/agent.d.ts.map +1 -0
  7. package/dist/agent.js +1064 -0
  8. package/dist/agent.js.map +1 -0
  9. package/dist/api.d.ts +10 -0
  10. package/dist/api.d.ts.map +1 -0
  11. package/dist/api.js +39 -0
  12. package/dist/api.js.map +1 -0
  13. package/dist/checkpoint.d.ts +52 -0
  14. package/dist/checkpoint.d.ts.map +1 -0
  15. package/dist/checkpoint.js +234 -0
  16. package/dist/checkpoint.js.map +1 -0
  17. package/dist/config.d.ts +24 -0
  18. package/dist/config.d.ts.map +1 -0
  19. package/dist/config.js +265 -0
  20. package/dist/config.js.map +1 -0
  21. package/dist/context.d.ts +15 -0
  22. package/dist/context.d.ts.map +1 -0
  23. package/dist/context.js +237 -0
  24. package/dist/context.js.map +1 -0
  25. package/dist/cost.d.ts +26 -0
  26. package/dist/cost.d.ts.map +1 -0
  27. package/dist/cost.js +161 -0
  28. package/dist/cost.js.map +1 -0
  29. package/dist/dashboard.d.ts +96 -0
  30. package/dist/dashboard.d.ts.map +1 -0
  31. package/dist/dashboard.js +292 -0
  32. package/dist/dashboard.js.map +1 -0
  33. package/dist/fileconfig.d.ts +129 -0
  34. package/dist/fileconfig.d.ts.map +1 -0
  35. package/dist/fileconfig.js +391 -0
  36. package/dist/fileconfig.js.map +1 -0
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +882 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/init.d.ts +9 -0
  42. package/dist/init.d.ts.map +1 -0
  43. package/dist/init.js +221 -0
  44. package/dist/init.js.map +1 -0
  45. package/dist/logger.d.ts +33 -0
  46. package/dist/logger.d.ts.map +1 -0
  47. package/dist/logger.js +150 -0
  48. package/dist/logger.js.map +1 -0
  49. package/dist/modelsdev.d.ts +63 -0
  50. package/dist/modelsdev.d.ts.map +1 -0
  51. package/dist/modelsdev.js +265 -0
  52. package/dist/modelsdev.js.map +1 -0
  53. package/dist/parallel.d.ts +54 -0
  54. package/dist/parallel.d.ts.map +1 -0
  55. package/dist/parallel.js +139 -0
  56. package/dist/parallel.js.map +1 -0
  57. package/dist/pool.d.ts +17 -0
  58. package/dist/pool.d.ts.map +1 -0
  59. package/dist/pool.js +53 -0
  60. package/dist/pool.js.map +1 -0
  61. package/dist/provider.d.ts +60 -0
  62. package/dist/provider.d.ts.map +1 -0
  63. package/dist/provider.js +396 -0
  64. package/dist/provider.js.map +1 -0
  65. package/dist/registry.d.ts +60 -0
  66. package/dist/registry.d.ts.map +1 -0
  67. package/dist/registry.js +153 -0
  68. package/dist/registry.js.map +1 -0
  69. package/dist/safety.d.ts +21 -0
  70. package/dist/safety.d.ts.map +1 -0
  71. package/dist/safety.js +118 -0
  72. package/dist/safety.js.map +1 -0
  73. package/dist/schema.d.ts +9 -0
  74. package/dist/schema.d.ts.map +1 -0
  75. package/dist/schema.js +166 -0
  76. package/dist/schema.js.map +1 -0
  77. package/dist/test-dashboard.d.ts +5 -0
  78. package/dist/test-dashboard.d.ts.map +1 -0
  79. package/dist/test-dashboard.js +202 -0
  80. package/dist/test-dashboard.js.map +1 -0
  81. package/dist/tools.d.ts +27 -0
  82. package/dist/tools.d.ts.map +1 -0
  83. package/dist/tools.js +1212 -0
  84. package/dist/tools.js.map +1 -0
  85. package/dist/types.d.ts +180 -0
  86. package/dist/types.d.ts.map +1 -0
  87. package/dist/types.js +14 -0
  88. package/dist/types.js.map +1 -0
  89. package/dist/worktree.d.ts +50 -0
  90. package/dist/worktree.d.ts.map +1 -0
  91. package/dist/worktree.js +186 -0
  92. package/dist/worktree.js.map +1 -0
  93. package/package.json +67 -0
@@ -0,0 +1,162 @@
1
+ import { AgentConfig, Message, RunStats, ToolDefinition } from './types';
2
+ import { Logger } from './logger';
3
+ import { DashboardServer } from './dashboard';
4
+ /**
5
+ * Make an arbitrary slice of the message history a VALID conversation:
6
+ * - every `tool` message has a matching preceding `assistant` tool_call
7
+ * - every `assistant` tool_call has all of its `tool` results present
8
+ * - no leading `tool` message (which would be an orphaned result)
9
+ *
10
+ * Runs to a fixed point because removing a dangling assistant can orphan a
11
+ * tool result, and removing an orphaned tool result can leave an assistant
12
+ * with fewer results. Used by the context-truncation fallback.
13
+ */
14
+ export declare function sanitizeMessageWindow(window: Message[]): Message[];
15
+ export declare class Agent {
16
+ private config;
17
+ private provider;
18
+ private logger;
19
+ private checkpoint;
20
+ private tools;
21
+ private messages;
22
+ private stats;
23
+ private taskDescription;
24
+ private progress;
25
+ private running;
26
+ private taskComplete;
27
+ private completionSummary;
28
+ private paused;
29
+ /** Shared task queue - tools can push to this, agent reads from it */
30
+ private taskQueue;
31
+ private currentTaskId;
32
+ /**
33
+ * The value of stats.totalIterations when the CURRENT task started. Used to
34
+ * enforce maxIterationsPerTask: if a task runs longer than the per-task
35
+ * budget without calling task_complete, the agent auto-advances to the next
36
+ * queued task so an open-ended task can't starve the queue forever.
37
+ */
38
+ private currentTaskStartIteration;
39
+ /** Optional dashboard for web UI */
40
+ private dashboard;
41
+ /**
42
+ * Exact prompt-token count reported by the provider on the last chat call.
43
+ * This is the real API measurement of the context size at that moment —
44
+ * NOT a chars/4 guess. 0 until the first response arrives.
45
+ */
46
+ private lastPromptTokens;
47
+ /**
48
+ * How many messages had been sent when lastPromptTokens was measured.
49
+ * Messages appended after this index (the assistant reply + tool results)
50
+ * haven't been counted by the API yet, so only that small tail is estimated.
51
+ */
52
+ private lastPromptMessageCount;
53
+ /**
54
+ * Real completion-token count from the last chat call (API-reported).
55
+ */
56
+ private lastCompletionTokens;
57
+ /**
58
+ * Number of CONSECUTIVE failed iterations. Reset to 0 after any success.
59
+ * The agent stops only when this hits the limit — meaning it's genuinely
60
+ * stuck — rather than on a lifetime error total, which would kill a healthy
61
+ * long run that has merely accumulated transient errors over hours/days.
62
+ */
63
+ private consecutiveErrors;
64
+ private static readonly MAX_CONSECUTIVE_ERRORS;
65
+ constructor(config: AgentConfig, logger: Logger);
66
+ /**
67
+ * Register a custom tool.
68
+ */
69
+ addTool(tool: ToolDefinition): void;
70
+ /**
71
+ * Attach a dashboard server for web UI.
72
+ */
73
+ setDashboard(dashboard: DashboardServer): void;
74
+ private handleDashboardCommand;
75
+ /** Push current state to dashboard */
76
+ private broadcastState;
77
+ /**
78
+ * Run the agent on a task. This is the main entry point.
79
+ *
80
+ * SINGLE MODE (default):
81
+ * Runs until task_complete, maxIterations, or unrecoverable error.
82
+ *
83
+ * CONTINUOUS MODE (config.continuous = true):
84
+ * After task_complete, picks next task from queue. If queue is empty,
85
+ * asks the LLM to generate follow-up tasks. Only stops when:
86
+ * - STOP file is detected (config.stopFile)
87
+ * - Ctrl+C / SIGINT
88
+ * - maxIterations reached
89
+ * - Agent explicitly decides there is nothing left to do
90
+ */
91
+ run(taskDescription: string, resumeFromCheckpoint?: boolean): Promise<{
92
+ success: boolean;
93
+ summary: string;
94
+ stats: RunStats;
95
+ }>;
96
+ private initializeMessages;
97
+ private getModelName;
98
+ /**
99
+ * Auto-commit the changes produced by a completed task. LOCAL ONLY — this
100
+ * never pushes to a remote. Stages all changes and commits with a message
101
+ * derived from the task summary. If there is nothing to commit (e.g. the
102
+ * task made no file changes), it silently does nothing.
103
+ */
104
+ private autoCommitTask;
105
+ /** Run a git command via execFile (argv array, no shell). Resolves stdout. */
106
+ private runGitQuiet;
107
+ /**
108
+ * Check if the agent should stop (STOP file, SIGINT already handled).
109
+ */
110
+ private shouldStop;
111
+ /**
112
+ * Mark the current task as complete in the queue.
113
+ */
114
+ private markCurrentTaskComplete;
115
+ /**
116
+ * Get the next task to work on. Checks queue first, then asks LLM to generate tasks.
117
+ */
118
+ private getNextTask;
119
+ /**
120
+ * Process an LLM response: display content, execute tool calls.
121
+ */
122
+ private processResponse;
123
+ /**
124
+ * Approximate token count for a slice of messages.
125
+ *
126
+ * Uses chars/4 for content plus ~4 tokens per-message overhead (role,
127
+ * delimiters, name field, etc. — the real overhead varies by provider
128
+ * but 4 is a reasonable average). This is a FALLBACK; whenever the API
129
+ * has reported a real prompt_tokens count we prefer that.
130
+ */
131
+ private approxTokens;
132
+ /**
133
+ * Best available measure of the CURRENT context size in tokens.
134
+ *
135
+ * Prefers the real number the API reported (lastPromptTokens) and only
136
+ * estimates the small tail of messages appended since that measurement.
137
+ * Before the first API response, falls back to a chars/4 estimate.
138
+ *
139
+ * IMPORTANT: The tail typically contains the assistant reply (whose real
140
+ * token count is `lastCompletionTokens`) PLUS tool-result messages. We
141
+ * use the real completion count for the assistant message and only
142
+ * estimate the tool-result messages, avoiding double-counting.
143
+ */
144
+ private currentContextTokens;
145
+ /**
146
+ * Manage context window size. When the conversation gets too long,
147
+ * ask the LLM to summarize progress so we can compress the history.
148
+ */
149
+ private manageContext;
150
+ /** Delegate to the standalone exported function. */
151
+ private sanitizeMessageWindow;
152
+ /**
153
+ * Save current state as a checkpoint.
154
+ */
155
+ private saveCheckpoint;
156
+ /**
157
+ * Build a final summary of everything accomplished.
158
+ */
159
+ private buildFinalSummary;
160
+ private sleep;
161
+ }
162
+ //# sourceMappingURL=agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,WAAW,EAA2B,OAAO,EAAc,QAAQ,EAAE,cAAc,EAAyB,MAAM,SAAS,CAAC;AAErI,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,OAAO,EAAE,eAAe,EAAoB,MAAM,aAAa,CAAC;AAKhE;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,EAAE,CAyClE;AAED,qBAAa,KAAK;IAChB,OAAO,CAAC,MAA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