@archships/dim-agent-sdk 0.0.1

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 (78) hide show
  1. package/README.md +69 -0
  2. package/dist/agent-core/Agent.d.ts +29 -0
  3. package/dist/agent-core/LoopRunner.d.ts +30 -0
  4. package/dist/agent-core/MessageFactory.d.ts +20 -0
  5. package/dist/agent-core/ModelTurnCollector.d.ts +22 -0
  6. package/dist/agent-core/Session.d.ts +48 -0
  7. package/dist/agent-core/TerminationPolicy.d.ts +14 -0
  8. package/dist/agent-core/ToolExecutor.d.ts +27 -0
  9. package/dist/agent-core/agent-types.d.ts +26 -0
  10. package/dist/agent-core/createModel.d.ts +2 -0
  11. package/dist/agent-core/errors.d.ts +5 -0
  12. package/dist/agent-core/index.d.ts +13 -0
  13. package/dist/agent-core/session-state.d.ts +17 -0
  14. package/dist/agent-core/tool-call.d.ts +9 -0
  15. package/dist/context/AutoContextManager.d.ts +21 -0
  16. package/dist/context/index.d.ts +3 -0
  17. package/dist/context/types.d.ts +19 -0
  18. package/dist/contracts/common.d.ts +19 -0
  19. package/dist/contracts/content-normalize.d.ts +6 -0
  20. package/dist/contracts/content.d.ts +16 -0
  21. package/dist/contracts/event.d.ts +30 -0
  22. package/dist/contracts/index.d.ts +9 -0
  23. package/dist/contracts/message.d.ts +32 -0
  24. package/dist/contracts/model.d.ts +73 -0
  25. package/dist/contracts/state.d.ts +14 -0
  26. package/dist/contracts/tool-normalize.d.ts +3 -0
  27. package/dist/contracts/tool.d.ts +40 -0
  28. package/dist/index.d.ts +16 -0
  29. package/dist/index.js +2997 -0
  30. package/dist/persistence/FileStateStore.d.ts +14 -0
  31. package/dist/persistence/InMemoryStateStore.d.ts +9 -0
  32. package/dist/persistence/SnapshotCodec.d.ts +20 -0
  33. package/dist/persistence/index.d.ts +6 -0
  34. package/dist/persistence/store.d.ts +7 -0
  35. package/dist/plugin-host/HookPipeline.d.ts +7 -0
  36. package/dist/plugin-host/PluginHost.d.ts +36 -0
  37. package/dist/plugin-host/helpers.d.ts +3 -0
  38. package/dist/plugin-host/index.d.ts +4 -0
  39. package/dist/plugin-host/types.d.ts +1 -0
  40. package/dist/providers/anthropic/adapter.d.ts +13 -0
  41. package/dist/providers/anthropic/mapper.d.ts +28 -0
  42. package/dist/providers/gemini/adapter.d.ts +12 -0
  43. package/dist/providers/gemini/mapper.d.ts +30 -0
  44. package/dist/providers/index.d.ts +8 -0
  45. package/dist/providers/openai/adapter.d.ts +12 -0
  46. package/dist/providers/openai/mapper.d.ts +15 -0
  47. package/dist/providers/openai-responses/adapter.d.ts +12 -0
  48. package/dist/providers/openai-responses/mapper.d.ts +40 -0
  49. package/dist/providers/shared/http-error.d.ts +7 -0
  50. package/dist/providers/shared/provider-state.d.ts +4 -0
  51. package/dist/providers/shared/reasoning.d.ts +14 -0
  52. package/dist/providers/shared/tool-call.d.ts +5 -0
  53. package/dist/providers/shared/usage.d.ts +2 -0
  54. package/dist/services/ExecGateway.d.ts +14 -0
  55. package/dist/services/FileSystemGateway.d.ts +35 -0
  56. package/dist/services/GitGateway.d.ts +17 -0
  57. package/dist/services/ModelGateway.d.ts +7 -0
  58. package/dist/services/NetworkGateway.d.ts +6 -0
  59. package/dist/services/PermissionGateway.d.ts +11 -0
  60. package/dist/services/activity.d.ts +10 -0
  61. package/dist/services/index.d.ts +10 -0
  62. package/dist/services/permissions.d.ts +17 -0
  63. package/dist/services/types.d.ts +77 -0
  64. package/dist/tools/BaseTool.d.ts +6 -0
  65. package/dist/tools/ToolRegistry.d.ts +9 -0
  66. package/dist/tools/builtins/EditTool.d.ts +6 -0
  67. package/dist/tools/builtins/ExecTool.d.ts +6 -0
  68. package/dist/tools/builtins/ReadTool.d.ts +6 -0
  69. package/dist/tools/builtins/WriteTool.d.ts +6 -0
  70. package/dist/tools/builtins/index.d.ts +4 -0
  71. package/dist/tools/builtins/utils.d.ts +6 -0
  72. package/dist/tools/index.d.ts +4 -0
  73. package/dist/tools/result.d.ts +3 -0
  74. package/dist/utils/guards.d.ts +2 -0
  75. package/dist/utils/id.d.ts +1 -0
  76. package/dist/utils/json.d.ts +3 -0
  77. package/dist/utils/usage.d.ts +3 -0
  78. package/package.json +54 -0
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # @archships/dim-agent-sdk
2
+
3
+ An agent-first TypeScript SDK with canonical multi-provider contracts, session/tool loop, hook-based plugins, runtime gateways, and builtin local coding tools.
4
+
5
+ ## Install
6
+
7
+ Recommended runtime: Node.js `>=18`.
8
+
9
+ ```bash
10
+ npm install @archships/dim-agent-sdk
11
+ ```
12
+
13
+ ## Quick start
14
+
15
+ ```ts
16
+ import {
17
+ createAgent,
18
+ createModel,
19
+ createOpenAIAdapter,
20
+ } from '@archships/dim-agent-sdk'
21
+
22
+ const model = createModel(createOpenAIAdapter({
23
+ apiKey: process.env.OPENAI_API_KEY,
24
+ baseUrl: 'https://api.openai.com/v1',
25
+ defaultModel: 'gpt-4o-mini',
26
+ }))
27
+
28
+ const agent = createAgent({
29
+ model,
30
+ cwd: process.cwd(),
31
+ })
32
+
33
+ const session = await agent.createSession({
34
+ systemPrompt: 'You are a coding agent. Use tools when needed.',
35
+ })
36
+
37
+ const message = await session.send('Create hello.txt in the current directory and write hello world into it.')
38
+ console.log(message.content)
39
+ ```
40
+
41
+ ## Manual
42
+
43
+ - Developer manual: [docs/manual/README.md](../../docs/manual/README.md)
44
+ - Chinese guide: [docs/manual/zh/README.md](../../docs/manual/zh/README.md)
45
+ - English guide: [docs/manual/en/README.md](../../docs/manual/en/README.md)
46
+
47
+ ## Included core capabilities
48
+
49
+ - Canonical content / message / tool / model / state contracts
50
+ - `createAgent()` -> `Agent` -> `Session`
51
+ - Session stream events: `text_delta`, optional `thinking_delta`, `tool_call`, `tool_result`, `done`
52
+ - Provider adapters: `openai-compatible`, `openai-responses`, `anthropic`, `gemini`
53
+ - Builtin tools: `read`, `write`, `edit`, `exec`
54
+ - Hook-first plugin integration
55
+ - Runtime gateways: file system, git, exec, network, model
56
+ - In-memory and file-based persistence
57
+
58
+ ## Provider notes
59
+
60
+ - `createOpenAIAdapter()`: OpenAI-compatible Chat Completions style; no official reasoning channel mapping
61
+ - `createOpenAIResponsesAdapter()`: official OpenAI Responses API; maps reasoning summaries into `thinking_delta`
62
+ - `createAnthropicAdapter()`: maps Claude thinking blocks into `thinking_delta`
63
+ - `createGeminiAdapter()`: maps Gemini thought parts into `thinking_delta`
64
+
65
+ ## Demo
66
+
67
+ A repository demo is available at `packages/dim-agent-sdk/demo/openai-tools.ts`.
68
+ It uses `createOpenAIAdapter()` against the OpenAI-compatible Chat API and exercises the builtin tools through one session.
69
+ It is intended for local repository development rather than package consumers.
@@ -0,0 +1,29 @@
1
+ import type { SessionSnapshot } from '../contracts';
2
+ import type { StateStore } from '../persistence/store';
3
+ import { PluginHost } from '../plugin-host/PluginHost';
4
+ import type { DimPlugin } from '@archships/dim-plugin-api';
5
+ import type { AgentServices, PermissionSpec } from '../services';
6
+ import { ToolRegistry } from '../tools';
7
+ import { Session } from './Session';
8
+ import type { AgentOptions, CreateSessionOptions } from './agent-types';
9
+ export declare class Agent {
10
+ readonly id: string;
11
+ readonly pluginHost: PluginHost;
12
+ readonly services: AgentServices;
13
+ readonly tools: ToolRegistry;
14
+ readonly permissions?: PermissionSpec;
15
+ private readonly options;
16
+ private readonly messageFactory;
17
+ private readonly contextManager;
18
+ private readonly tracker;
19
+ private readonly permissionGateway;
20
+ constructor(options: AgentOptions);
21
+ createSession(options?: CreateSessionOptions): Promise<Session>;
22
+ restoreSession(sessionId: string): Promise<Session | null>;
23
+ sessionFromSnapshot(snapshot: SessionSnapshot): Session;
24
+ }
25
+ export interface CreateAgentOptions extends AgentOptions {
26
+ plugins?: DimPlugin[];
27
+ stateStore?: StateStore;
28
+ }
29
+ export declare function createAgent(options: CreateAgentOptions): Agent;
@@ -0,0 +1,30 @@
1
+ import type { AssistantMessage, MessageContentInput, SessionStreamEvent } from '../contracts';
2
+ import type { ContextManager } from '../context';
3
+ import type { PluginHost } from '../plugin-host/PluginHost';
4
+ import type { MessageFactory } from './MessageFactory';
5
+ import type { ModelTurnCollector } from './ModelTurnCollector';
6
+ import type { RuntimeSessionState } from './session-state';
7
+ import type { TerminationPolicy } from './TerminationPolicy';
8
+ import type { ToolExecutor } from './ToolExecutor';
9
+ export interface LoopRunnerDependencies {
10
+ messageFactory: MessageFactory;
11
+ turnCollector: ModelTurnCollector;
12
+ toolExecutor: ToolExecutor;
13
+ terminationPolicy: TerminationPolicy;
14
+ contextManager: ContextManager;
15
+ pluginHost?: PluginHost;
16
+ }
17
+ export interface LoopRunnerRunOptions {
18
+ signal?: AbortSignal;
19
+ onDone?: (message: AssistantMessage) => Promise<void> | void;
20
+ }
21
+ export declare class LoopRunner {
22
+ private readonly messageFactory;
23
+ private readonly turnCollector;
24
+ private readonly toolExecutor;
25
+ private readonly terminationPolicy;
26
+ private readonly contextManager;
27
+ private readonly pluginHost?;
28
+ constructor(dependencies: LoopRunnerDependencies);
29
+ run(state: RuntimeSessionState, input: MessageContentInput, options?: LoopRunnerRunOptions): AsyncGenerator<SessionStreamEvent, AssistantMessage, void>;
30
+ }
@@ -0,0 +1,20 @@
1
+ import type { AssistantMessage, CallToolResult, MessageContentInput, ModelStopReason, SystemMessage, ToolCall, ToolMessage, UserMessage } from '../contracts';
2
+ export interface CreateAssistantMessageInput {
3
+ text: string;
4
+ thinking?: string;
5
+ toolCalls: ToolCall[];
6
+ stopReason: ModelStopReason;
7
+ metadata?: Record<string, unknown>;
8
+ }
9
+ export interface MessageFactory {
10
+ createSystemMessage(input: MessageContentInput): SystemMessage;
11
+ createUserMessage(input: MessageContentInput): UserMessage;
12
+ createAssistantMessage(input: CreateAssistantMessageInput): AssistantMessage;
13
+ createToolMessage(toolCall: ToolCall, result: CallToolResult): ToolMessage;
14
+ }
15
+ export declare class DefaultMessageFactory implements MessageFactory {
16
+ createSystemMessage(input: MessageContentInput): SystemMessage;
17
+ createUserMessage(input: MessageContentInput): UserMessage;
18
+ createAssistantMessage(input: CreateAssistantMessageInput): AssistantMessage;
19
+ createToolMessage(toolCall: ToolCall, result: CallToolResult): ToolMessage;
20
+ }
@@ -0,0 +1,22 @@
1
+ import type { ModelStopReason, ModelStreamEvent, SessionStreamEvent, ToolCall, Usage } from '../contracts';
2
+ export interface CollectModelTurnOptions {
3
+ sessionId: string;
4
+ requestId: string;
5
+ events: AsyncIterable<ModelStreamEvent>;
6
+ onEvent?: (event: ModelStreamEvent) => Promise<void> | void;
7
+ }
8
+ export interface ModelTurnResult {
9
+ requestId: string;
10
+ text: string;
11
+ thinking?: string;
12
+ toolCalls: ToolCall[];
13
+ stopReason: ModelStopReason;
14
+ usage?: Usage;
15
+ assistantMetadata?: Record<string, unknown>;
16
+ }
17
+ export interface ModelTurnCollector {
18
+ collect(options: CollectModelTurnOptions): AsyncGenerator<SessionStreamEvent, ModelTurnResult, void>;
19
+ }
20
+ export declare class DefaultModelTurnCollector implements ModelTurnCollector {
21
+ collect(options: CollectModelTurnOptions): AsyncGenerator<SessionStreamEvent, ModelTurnResult, void>;
22
+ }
@@ -0,0 +1,48 @@
1
+ import type { AssistantMessage, Message, MessageContentInput, ModelClient, ModelRef, SessionSnapshot, SessionStreamEvent, Usage } from '../contracts';
2
+ import type { ContextManager } from '../context';
3
+ import type { StateStore } from '../persistence/store';
4
+ import type { PluginHost } from '../plugin-host/PluginHost';
5
+ import type { AgentServices } from '../services';
6
+ import type { ToolRegistry } from '../tools/ToolRegistry';
7
+ export interface SessionOptions {
8
+ id?: string;
9
+ model: ModelClient;
10
+ modelRef?: ModelRef;
11
+ tools?: ToolRegistry;
12
+ stateStore?: StateStore;
13
+ maxIterations?: number;
14
+ cwd?: string;
15
+ metadata?: Record<string, unknown>;
16
+ reasoning?: Record<string, unknown>;
17
+ messages?: Message[];
18
+ usage?: Usage;
19
+ createdAt?: number;
20
+ updatedAt?: number;
21
+ pluginHost?: PluginHost;
22
+ contextManager: ContextManager;
23
+ services: AgentServices;
24
+ }
25
+ export declare class Session {
26
+ readonly id: string;
27
+ readonly createdAt: number;
28
+ private readonly stateStore?;
29
+ private readonly state;
30
+ private readonly pluginHost?;
31
+ private readonly contextManager;
32
+ private readonly services;
33
+ constructor(options: SessionOptions);
34
+ get messages(): Message[];
35
+ get usage(): Usage;
36
+ get updatedAt(): number;
37
+ getCwd(): string | undefined;
38
+ setCwd(cwd: string): void;
39
+ save(): Promise<void>;
40
+ toSnapshot(): SessionSnapshot;
41
+ send(input: MessageContentInput, options?: {
42
+ signal?: AbortSignal;
43
+ }): Promise<AssistantMessage>;
44
+ stream(input: MessageContentInput, options?: {
45
+ signal?: AbortSignal;
46
+ }): AsyncGenerator<SessionStreamEvent, AssistantMessage, void>;
47
+ private createLoopRunner;
48
+ }
@@ -0,0 +1,14 @@
1
+ import type { SessionErrorPayload, ToolCall } from '../contracts';
2
+ export type TerminationDecision = {
3
+ type: 'done';
4
+ } | {
5
+ type: 'continue';
6
+ };
7
+ export interface TerminationPolicy {
8
+ afterAssistantTurn(toolCalls: ToolCall[]): TerminationDecision;
9
+ onMaxIterationsExceeded(maxIterations: number, requestId?: string): SessionErrorPayload;
10
+ }
11
+ export declare class DefaultTerminationPolicy implements TerminationPolicy {
12
+ afterAssistantTurn(toolCalls: ToolCall[]): TerminationDecision;
13
+ onMaxIterationsExceeded(maxIterations: number, requestId?: string): SessionErrorPayload;
14
+ }
@@ -0,0 +1,27 @@
1
+ import type { CallToolResult, ToolCall } from '../contracts';
2
+ import type { PluginHost } from '../plugin-host/PluginHost';
3
+ import type { AgentServices } from '../services';
4
+ import type { ToolRegistry } from '../tools/ToolRegistry';
5
+ export interface ToolExecutorExecuteOptions {
6
+ signal?: AbortSignal;
7
+ cwd?: string;
8
+ }
9
+ export interface ToolExecutor {
10
+ execute(toolCall: ToolCall, options?: ToolExecutorExecuteOptions): Promise<CallToolResult>;
11
+ }
12
+ export interface ToolExecutorOptions {
13
+ sessionId: string;
14
+ tools?: ToolRegistry;
15
+ metadata?: Record<string, unknown>;
16
+ services: AgentServices;
17
+ pluginHost?: PluginHost;
18
+ }
19
+ export declare class DefaultToolExecutor implements ToolExecutor {
20
+ private readonly sessionId;
21
+ private readonly tools?;
22
+ private readonly metadata?;
23
+ private readonly services;
24
+ private readonly pluginHost?;
25
+ constructor(options: ToolExecutorOptions);
26
+ execute(toolCall: ToolCall, options?: ToolExecutorExecuteOptions): Promise<CallToolResult>;
27
+ }
@@ -0,0 +1,26 @@
1
+ import type { Message, MessageContentInput, ModelClient, Tool } from '../contracts';
2
+ import type { ContextManager } from '../context';
3
+ import type { StateStore } from '../persistence/store';
4
+ import type { DimPlugin } from '@archships/dim-plugin-api';
5
+ import type { PermissionSpec } from '../services';
6
+ import type { ToolRegistry } from '../tools/ToolRegistry';
7
+ export interface AgentOptions {
8
+ model: ModelClient;
9
+ tools?: ToolRegistry | Tool[];
10
+ plugins?: DimPlugin[];
11
+ stateStore?: StateStore;
12
+ maxIterations?: number;
13
+ cwd?: string;
14
+ metadata?: Record<string, unknown>;
15
+ permissions?: PermissionSpec;
16
+ contextManager?: ContextManager;
17
+ includeBuiltinTools?: boolean;
18
+ reasoning?: Record<string, unknown>;
19
+ }
20
+ export interface CreateSessionOptions {
21
+ sessionId?: string;
22
+ cwd?: string;
23
+ metadata?: Record<string, unknown>;
24
+ messages?: Message[];
25
+ systemPrompt?: MessageContentInput;
26
+ }
@@ -0,0 +1,2 @@
1
+ import type { ConfiguredModelAdapter, ModelClient } from '../contracts/model';
2
+ export declare function createModel(adapter: ConfiguredModelAdapter): ModelClient;
@@ -0,0 +1,5 @@
1
+ import type { SessionErrorPayload } from '../contracts/common';
2
+ export declare class SessionExecutionError extends Error {
3
+ readonly payload: SessionErrorPayload;
4
+ constructor(payload: SessionErrorPayload);
5
+ }
@@ -0,0 +1,13 @@
1
+ export { Agent, createAgent } from './Agent';
2
+ export { createModel } from './createModel';
3
+ export { Session } from './Session';
4
+ export { SessionExecutionError } from './errors';
5
+ export type { AgentOptions, CreateSessionOptions } from './agent-types';
6
+ export type { CreateAgentOptions } from './Agent';
7
+ export type { LoopRunnerDependencies, LoopRunnerRunOptions } from './LoopRunner';
8
+ export type { CreateAssistantMessageInput, MessageFactory } from './MessageFactory';
9
+ export type { CollectModelTurnOptions, ModelTurnCollector, ModelTurnResult } from './ModelTurnCollector';
10
+ export type { RuntimeSessionState } from './session-state';
11
+ export type { SessionOptions } from './Session';
12
+ export type { TerminationDecision, TerminationPolicy } from './TerminationPolicy';
13
+ export type { ToolExecutor, ToolExecutorExecuteOptions, ToolExecutorOptions } from './ToolExecutor';
@@ -0,0 +1,17 @@
1
+ import type { Message, ModelClient, ModelRef, Usage } from '../contracts';
2
+ import type { ToolRegistry } from '../tools/ToolRegistry';
3
+ export interface RuntimeSessionState {
4
+ id: string;
5
+ model: ModelClient;
6
+ modelRef: ModelRef;
7
+ tools?: ToolRegistry;
8
+ maxIterations: number;
9
+ cwd?: string;
10
+ metadata?: Record<string, unknown>;
11
+ reasoning?: Record<string, unknown>;
12
+ messages: Message[];
13
+ usage: Usage;
14
+ createdAt: number;
15
+ updatedAt: number;
16
+ }
17
+ export declare function touchRuntimeSessionState(state: RuntimeSessionState): void;
@@ -0,0 +1,9 @@
1
+ import type { ToolCall } from '../contracts/tool';
2
+ export interface ToolCallDraft {
3
+ callId: string;
4
+ toolName: string;
5
+ argsText: string;
6
+ }
7
+ export declare function startToolCall(callId: string, toolName: string): ToolCallDraft;
8
+ export declare function appendToolCallArgsDelta(draft: ToolCallDraft, delta: string): ToolCallDraft;
9
+ export declare function finalizeToolCall(draft: ToolCallDraft): ToolCall;
@@ -0,0 +1,21 @@
1
+ import type { PluginHost } from '../plugin-host/PluginHost';
2
+ import type { AgentServices } from '../services';
3
+ import type { ContextItem, ContextManager, ContextResolveOptions } from './types';
4
+ export interface AutoContextManagerOptions {
5
+ services: AgentServices;
6
+ pluginHost?: PluginHost;
7
+ maxItems?: number;
8
+ maxChars?: number;
9
+ }
10
+ export declare class AutoContextManager implements ContextManager {
11
+ private readonly services;
12
+ private readonly pluginHost?;
13
+ private readonly maxItems;
14
+ private readonly maxChars;
15
+ constructor(options: AutoContextManagerOptions);
16
+ resolve(options: ContextResolveOptions): Promise<ContextItem[]>;
17
+ format(items: ContextItem[]): string[];
18
+ private resolveExplicitFiles;
19
+ private resolveRecentFiles;
20
+ private resolveGitState;
21
+ }
@@ -0,0 +1,3 @@
1
+ export { AutoContextManager } from './AutoContextManager';
2
+ export type { AutoContextManagerOptions } from './AutoContextManager';
3
+ export type { ContextItem, ContextManager, ContextResolveOptions } from './types';
@@ -0,0 +1,19 @@
1
+ import type { Message, MessageContentInput } from '../contracts';
2
+ export interface ContextItem {
3
+ id: string;
4
+ type: 'cwd' | 'file' | 'git_status' | 'git_diff' | 'text';
5
+ title: string;
6
+ content: string;
7
+ priority: number;
8
+ source: string;
9
+ }
10
+ export interface ContextResolveOptions {
11
+ sessionId: string;
12
+ input: MessageContentInput;
13
+ messages: Message[];
14
+ cwd?: string;
15
+ }
16
+ export interface ContextManager {
17
+ resolve(options: ContextResolveOptions): Promise<ContextItem[]>;
18
+ format(items: ContextItem[]): string[];
19
+ }
@@ -0,0 +1,19 @@
1
+ export interface Usage {
2
+ promptTokens: number;
3
+ completionTokens: number;
4
+ totalTokens: number;
5
+ }
6
+ export interface ModelErrorPayload {
7
+ code: string;
8
+ message: string;
9
+ status?: number;
10
+ retryable?: boolean;
11
+ details?: Record<string, unknown>;
12
+ }
13
+ export interface SessionErrorPayload extends ModelErrorPayload {
14
+ requestId?: string;
15
+ }
16
+ export type JsonValue = string | number | boolean | null | JsonObject | JsonValue[];
17
+ export interface JsonObject {
18
+ [key: string]: JsonValue;
19
+ }
@@ -0,0 +1,6 @@
1
+ import type { ContentBlock, ImageContent, MessageContentInput, NormalizedContent, TextContent } from './content';
2
+ export declare function isTextContent(value: unknown): value is TextContent;
3
+ export declare function isImageContent(value: unknown): value is ImageContent;
4
+ export declare function isContentBlock(value: unknown): value is ContentBlock;
5
+ export declare function normalizeContent(input: MessageContentInput): NormalizedContent;
6
+ export declare function contentToText(content: NormalizedContent): string;
@@ -0,0 +1,16 @@
1
+ export interface TextContent {
2
+ type: 'text';
3
+ text: string;
4
+ annotations?: Record<string, unknown>;
5
+ _meta?: Record<string, unknown>;
6
+ }
7
+ export interface ImageContent {
8
+ type: 'image';
9
+ data: string;
10
+ mimeType: string;
11
+ annotations?: Record<string, unknown>;
12
+ _meta?: Record<string, unknown>;
13
+ }
14
+ export type ContentBlock = TextContent | ImageContent;
15
+ export type MessageContentInput = string | ContentBlock | ContentBlock[];
16
+ export type NormalizedContent = ContentBlock[];
@@ -0,0 +1,30 @@
1
+ import type { SessionErrorPayload, Usage } from './common';
2
+ import type { AssistantMessage } from './message';
3
+ import type { CallToolResult, ToolCall } from './tool';
4
+ export type SessionStreamEvent = {
5
+ type: 'text_delta';
6
+ sessionId: string;
7
+ delta: string;
8
+ } | {
9
+ type: 'thinking_delta';
10
+ sessionId: string;
11
+ delta: string;
12
+ } | {
13
+ type: 'tool_call';
14
+ sessionId: string;
15
+ toolCall: ToolCall;
16
+ } | {
17
+ type: 'tool_result';
18
+ sessionId: string;
19
+ toolCallId: string;
20
+ result: CallToolResult;
21
+ } | {
22
+ type: 'done';
23
+ sessionId: string;
24
+ message: AssistantMessage;
25
+ usage?: Usage;
26
+ } | {
27
+ type: 'error';
28
+ sessionId: string;
29
+ error: SessionErrorPayload;
30
+ };
@@ -0,0 +1,9 @@
1
+ export * from './common';
2
+ export * from './content';
3
+ export * from './content-normalize';
4
+ export * from './event';
5
+ export * from './message';
6
+ export * from './model';
7
+ export * from './state';
8
+ export * from './tool';
9
+ export * from './tool-normalize';
@@ -0,0 +1,32 @@
1
+ import type { NormalizedContent } from './content';
2
+ import type { ModelStopReason } from './model';
3
+ import type { ToolCall } from './tool';
4
+ export type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
5
+ export interface BaseMessage {
6
+ id: string;
7
+ role: MessageRole;
8
+ content: NormalizedContent;
9
+ createdAt: number;
10
+ metadata?: Record<string, unknown>;
11
+ }
12
+ export interface SystemMessage extends BaseMessage {
13
+ role: 'system';
14
+ }
15
+ export interface UserMessage extends BaseMessage {
16
+ role: 'user';
17
+ name?: string;
18
+ }
19
+ export interface AssistantMessage extends BaseMessage {
20
+ role: 'assistant';
21
+ thinking?: string;
22
+ toolCalls?: ToolCall[];
23
+ stopReason?: ModelStopReason;
24
+ }
25
+ export interface ToolMessage extends BaseMessage {
26
+ role: 'tool';
27
+ toolCallId: string;
28
+ toolName: string;
29
+ structuredContent?: Record<string, unknown>;
30
+ isError?: boolean;
31
+ }
32
+ export type Message = SystemMessage | UserMessage | AssistantMessage | ToolMessage;
@@ -0,0 +1,73 @@
1
+ import type { ModelErrorPayload, Usage } from './common';
2
+ import type { Message } from './message';
3
+ import type { ToolDefinition } from './tool';
4
+ export interface ModelRef {
5
+ provider: string;
6
+ modelId: string;
7
+ }
8
+ export type ModelStopReason = 'final' | 'tool_call' | 'length' | 'error' | 'cancelled';
9
+ export interface ModelRequest {
10
+ requestId?: string;
11
+ model: ModelRef;
12
+ messages: Message[];
13
+ tools?: ToolDefinition[];
14
+ maxOutputTokens?: number;
15
+ temperature?: number;
16
+ topP?: number;
17
+ stop?: string[];
18
+ reasoning?: Record<string, unknown>;
19
+ metadata?: Record<string, string | number | boolean | null>;
20
+ signal?: AbortSignal;
21
+ }
22
+ export type ModelStreamEvent = {
23
+ type: 'response_start';
24
+ requestId: string;
25
+ model: ModelRef;
26
+ } | {
27
+ type: 'text_delta';
28
+ requestId: string;
29
+ delta: string;
30
+ } | {
31
+ type: 'thinking_delta';
32
+ requestId: string;
33
+ delta: string;
34
+ } | {
35
+ type: 'tool_call_start';
36
+ requestId: string;
37
+ callId: string;
38
+ toolName: string;
39
+ } | {
40
+ type: 'tool_call_args_delta';
41
+ requestId: string;
42
+ callId: string;
43
+ delta: string;
44
+ } | {
45
+ type: 'tool_call_end';
46
+ requestId: string;
47
+ callId: string;
48
+ } | {
49
+ type: 'response_end';
50
+ requestId: string;
51
+ stopReason: ModelStopReason;
52
+ usage?: Usage;
53
+ assistantMetadata?: Record<string, unknown>;
54
+ } | {
55
+ type: 'error';
56
+ requestId: string;
57
+ error: ModelErrorPayload;
58
+ terminal?: boolean;
59
+ };
60
+ export interface ModelAdapter {
61
+ readonly provider: string;
62
+ stream(request: ModelRequest): AsyncIterable<ModelStreamEvent>;
63
+ }
64
+ export interface ConfiguredModelAdapter extends ModelAdapter {
65
+ readonly defaultModel: ModelRef;
66
+ }
67
+ export interface ModelClient {
68
+ readonly adapter: ModelAdapter;
69
+ readonly model: ModelRef;
70
+ stream(request: Omit<ModelRequest, 'model'> & {
71
+ model?: ModelRef;
72
+ }): AsyncIterable<ModelStreamEvent>;
73
+ }
@@ -0,0 +1,14 @@
1
+ import type { Usage } from './common';
2
+ import type { Message } from './message';
3
+ import type { ModelRef } from './model';
4
+ export interface SessionSnapshot {
5
+ schemaVersion: 1;
6
+ sessionId: string;
7
+ model?: ModelRef;
8
+ cwd?: string;
9
+ messages: Message[];
10
+ usage?: Usage;
11
+ createdAt: number;
12
+ updatedAt: number;
13
+ metadata?: Record<string, unknown>;
14
+ }
@@ -0,0 +1,3 @@
1
+ import type { CallToolResult, ToolInputSchema } from './tool';
2
+ export declare function normalizeToolSchema(schema: ToolInputSchema): ToolInputSchema;
3
+ export declare function normalizeToolResult(result: CallToolResult): CallToolResult;
@@ -0,0 +1,40 @@
1
+ import type { JsonObject } from './common';
2
+ import type { NormalizedContent } from './content';
3
+ import type { AgentServices } from '../services/types';
4
+ export type JsonSchema = Record<string, unknown>;
5
+ export interface ToolInputSchema extends JsonSchema {
6
+ type: 'object';
7
+ properties?: Record<string, JsonSchema>;
8
+ required?: string[];
9
+ additionalProperties?: boolean | JsonSchema;
10
+ }
11
+ export interface ToolDefinition {
12
+ name: string;
13
+ description: string;
14
+ inputSchema: ToolInputSchema;
15
+ }
16
+ export interface ToolCall {
17
+ id: string;
18
+ type: 'function';
19
+ function: {
20
+ name: string;
21
+ arguments: Record<string, unknown>;
22
+ };
23
+ rawArgumentsText?: string;
24
+ }
25
+ export interface CallToolResult {
26
+ content: NormalizedContent;
27
+ structuredContent?: JsonObject;
28
+ isError?: boolean;
29
+ }
30
+ export interface ToolExecutionContext {
31
+ sessionId: string;
32
+ cwd?: string;
33
+ signal?: AbortSignal;
34
+ metadata?: Record<string, unknown>;
35
+ services?: AgentServices;
36
+ }
37
+ export interface Tool {
38
+ readonly definition: ToolDefinition;
39
+ execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<CallToolResult>;
40
+ }